blob: c61667377f3a3d066a354ea4d7b55b1ab0884474 [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"
31#include "compiler/dex/quick/codegen.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>
36#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080037#include <llvm/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
Logan Chien67645d82012-08-17 09:10:54 +080083 bool changed_;
84
TDYa1275e869b62012-07-25 00:45:39 -070085 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070086 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070087 // Constant for GBC expansion
88 //----------------------------------------------------------------------------
89 enum IntegerShiftKind {
90 kIntegerSHL,
91 kIntegerSHR,
92 kIntegerUSHR,
93 };
94
95 private:
96 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070097 // Helper function for GBC expansion
98 //----------------------------------------------------------------------------
99
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700100 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
101 llvm::CallInst& inst);
102
TDYa1275e869b62012-07-25 00:45:39 -0700103 uint64_t LV2UInt(llvm::Value* lv) {
104 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
105 }
106
107 int64_t LV2SInt(llvm::Value* lv) {
108 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
109 }
110
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700111 private:
112 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
113 // Refactor these utility functions from MethodCompiler to avoid forking.
114
Logan Chien67645d82012-08-17 09:10:54 +0800115 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
116
117 void RewriteFunction();
118
119 void RewriteBasicBlock(llvm::BasicBlock* original_block);
120
121 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
122 llvm::BasicBlock* new_basic_block);
123
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700124
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800125 // Sign or zero extend category 1 types < 32bits in size to 32bits.
126 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
127
128 // Truncate category 1 types from 32bits to the given JType size.
129 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
130
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700131 //----------------------------------------------------------------------------
132 // Dex cache code generation helper function
133 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700134 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700135
136 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
137
138 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
139
140 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
141
142 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
143
144 //----------------------------------------------------------------------------
145 // Code generation helper function
146 //----------------------------------------------------------------------------
147 llvm::Value* EmitLoadMethodObjectAddr();
148
149 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
150
151 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
152
153 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
154 llvm::Value* this_addr);
155
156 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
157 llvm::Value* index_value,
158 JType elem_jty);
159
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100160 //----------------------------------------------------------------------------
161 // Invoke helper function
162 //----------------------------------------------------------------------------
163 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
164
165 //----------------------------------------------------------------------------
166 // Inlining helper functions
167 //----------------------------------------------------------------------------
168 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
169
170 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
171 llvm::Value** result, bool is_empty);
172
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700173 private:
174 //----------------------------------------------------------------------------
175 // Expand Greenland intrinsics
176 //----------------------------------------------------------------------------
177 void Expand_TestSuspend(llvm::CallInst& call_inst);
178
TDYa1279a129452012-07-19 03:10:08 -0700179 void Expand_MarkGCCard(llvm::CallInst& call_inst);
180
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700181 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
182
183 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
184
185 void Expand_LockObject(llvm::Value* obj);
186
187 void Expand_UnlockObject(llvm::Value* obj);
188
189 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
190 llvm::Value* index_value,
191 JType elem_jty);
192
193 void Expand_ArrayPut(llvm::Value* new_value,
194 llvm::Value* array_addr,
195 llvm::Value* index_value,
196 JType elem_jty);
197
198 void Expand_FilledNewArray(llvm::CallInst& call_inst);
199
200 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
201 llvm::Value* is_volatile_value,
202 llvm::Value* object_addr,
203 JType field_jty);
204
205 void Expand_IPutFast(llvm::Value* field_offset_value,
206 llvm::Value* is_volatile_value,
207 llvm::Value* object_addr,
208 llvm::Value* new_value,
209 JType field_jty);
210
211 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
212 llvm::Value* field_offset_value,
213 llvm::Value* is_volatile_value,
214 JType field_jty);
215
216 void Expand_SPutFast(llvm::Value* static_storage_addr,
217 llvm::Value* field_offset_value,
218 llvm::Value* is_volatile_value,
219 llvm::Value* new_value,
220 JType field_jty);
221
222 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
223
224 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
225
226 llvm::Value*
227 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
228
229 llvm::Value*
230 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
231 llvm::Value* this_addr);
232
233 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
234
TDYa1274ec8ccd2012-08-11 07:04:57 -0700235 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700236
TDYa127ce4cc0d2012-11-18 16:59:53 -0800237 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700238
TDYa1278e950c12012-11-02 09:58:19 -0700239 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
240
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700241 void Expand_PopShadowFrame();
242
243 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
244
TDYa127a1b21852012-07-23 03:20:39 -0700245 //----------------------------------------------------------------------------
246 // Quick
247 //----------------------------------------------------------------------------
248
249 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
250 llvm::Value* src2_value,
251 bool gt_bias);
252
253 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
254
255 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
256 llvm::Value* cmp_lt);
257
TDYa127f71bf5a2012-07-29 20:09:52 -0700258 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700259 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
260
TDYa1275e869b62012-07-25 00:45:39 -0700261 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
262 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
263
TDYa1275a26d442012-07-26 18:58:38 -0700264 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
265 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
266
267 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
268 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
269
TDYa127f71bf5a2012-07-29 20:09:52 -0700270 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
271 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
272
273 void Expand_MonitorEnter(llvm::CallInst& call_inst);
274 void Expand_MonitorExit(llvm::CallInst& call_inst);
275
276 void Expand_HLCheckCast(llvm::CallInst& call_inst);
277 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
278
279 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
280
281 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
282
283 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
284 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
285 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
286 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
287
288 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
289 llvm::Value* array_length_value,
290 uint32_t type_idx,
291 bool is_filled_new_array);
292
293 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700294 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700295 llvm::Value* this_addr,
296 uint32_t dex_pc,
297 bool is_fast_path);
298
TDYa1275e869b62012-07-25 00:45:39 -0700299 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
300
301 void EmitUpdateDexPC(uint32_t dex_pc);
302
303 void EmitGuard_DivZeroException(uint32_t dex_pc,
304 llvm::Value* denominator,
305 JType op_jty);
306
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100307 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
308 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700309
310 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
311 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100312 llvm::Value* index,
313 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700314
Ian Rogers8e696052013-03-04 09:00:40 -0800315 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700316
317 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
318
319 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
320 const char* postfix);
321
322 int32_t GetTryItemOffset(uint32_t dex_pc);
323
324 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
325
326 llvm::BasicBlock* GetUnwindBasicBlock();
327
328 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
329
330 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
331
Logan Chien75e4b602012-07-23 14:24:12 -0700332 //----------------------------------------------------------------------------
333 // Expand Arithmetic Helper Intrinsics
334 //----------------------------------------------------------------------------
335
336 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
337 llvm::Value* src2_value,
338 IntegerShiftKind kind,
339 JType op_jty);
340
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700341 public:
342 static char ID;
343
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700344 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800345 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700346 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
347 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700348 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800349 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800350 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800351 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700352
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700353 bool runOnFunction(llvm::Function& func);
354
355 private:
Logan Chien67645d82012-08-17 09:10:54 +0800356 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700357
358 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
359 llvm::CallInst& call_inst);
360
361};
362
363char GBCExpanderPass::ID = 0;
364
365bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800366 VLOG(compiler) << "GBC expansion on " << func.getName().str();
367
TDYa127b672d1e2012-06-28 21:21:45 -0700368 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800369 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700370 return false;
371 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700372
Logan Chien67645d82012-08-17 09:10:54 +0800373 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700374 shadow_frame_ = NULL;
375 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700376 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800377 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700378
Ian Rogers89756f22013-03-04 16:40:02 -0800379 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
380 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700381 basic_block_unwind_ = NULL;
382 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
383 bb_iter != bb_end;
384 ++bb_iter) {
385 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
386 continue;
387 }
388 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
389 basic_blocks_[dex_pc] = bb_iter;
390 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700391
Logan Chien67645d82012-08-17 09:10:54 +0800392 // Insert stack overflow check
393 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700394
Logan Chien67645d82012-08-17 09:10:54 +0800395 // Rewrite the intrinsics
396 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700397
398 VERIFY_LLVM_FUNCTION(func);
399
Logan Chien67645d82012-08-17 09:10:54 +0800400 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401}
402
Logan Chien67645d82012-08-17 09:10:54 +0800403void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
404 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700405
Logan Chien67645d82012-08-17 09:10:54 +0800406 llvm::BasicBlock::iterator inst_iter = original_block->begin();
407 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700408
Logan Chien67645d82012-08-17 09:10:54 +0800409 while (inst_iter != inst_end) {
410 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
411 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700412
Logan Chien67645d82012-08-17 09:10:54 +0800413 if (call_inst) {
414 llvm::Function* callee_func = call_inst->getCalledFunction();
415 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
416 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700417
Logan Chien67645d82012-08-17 09:10:54 +0800418 if (intr_id == IntrinsicHelper::UnknownId) {
419 // This is not intrinsic call. Skip this instruction.
420 ++inst_iter;
421 continue;
422 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700423
Logan Chien67645d82012-08-17 09:10:54 +0800424 // Rewrite the intrinsic and change the function
425 changed_ = true;
426 irb_.SetInsertPoint(inst_iter);
427
428 // Expand the intrinsic
429 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
430 inst_iter->replaceAllUsesWith(new_value);
431 }
432
433 // Remove the old intrinsic call instruction
434 llvm::BasicBlock::iterator old_inst = inst_iter++;
435 old_inst->eraseFromParent();
436
437 // Splice the instruction to the new basic block
438 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
439 if (next_basic_block != curr_basic_block) {
440 next_basic_block->getInstList().splice(
441 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
442 inst_iter, inst_end);
443 curr_basic_block = next_basic_block;
444 inst_end = curr_basic_block->end();
445 }
446 }
447}
448
449
450void GBCExpanderPass::RewriteFunction() {
451 size_t num_basic_blocks = func_->getBasicBlockList().size();
452 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
453 // because we will create new basic block while expanding the intrinsics.
454 // We only want to iterate through the input basic blocks.
455
TDYa127aa558872012-08-16 05:11:07 -0700456 landing_pad_phi_mapping_.clear();
457
Logan Chien67645d82012-08-17 09:10:54 +0800458 for (llvm::Function::iterator bb_iter = func_->begin();
459 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700460 // Set insert point to current basic block.
461 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800462
TDYa12755e5e6c2012-09-11 15:14:42 -0700463 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700464
Logan Chien67645d82012-08-17 09:10:54 +0800465 // Rewrite the basic block
466 RewriteBasicBlock(bb_iter);
467
468 // Update the phi-instructions in the successor basic block
469 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
470 if (last_block != bb_iter) {
471 UpdatePhiInstruction(bb_iter, last_block);
472 }
473 }
TDYa127aa558872012-08-16 05:11:07 -0700474
475 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
476 HandlerPHIMap handler_phi;
477 // Iterate every used landing pad basic block
478 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
479 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
480 if (lbb == NULL) {
481 continue;
482 }
483
484 llvm::TerminatorInst* term_inst = lbb->getTerminator();
485 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
486 = landing_pad_phi_mapping_[lbb];
487 irb_.SetInsertPoint(lbb->begin());
488
489 // Iterate every succeeding basic block (catch block)
490 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
491 succ_iter != succ_end; ++succ_iter) {
492 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
493
494 // Iterate every phi instructions in the succeeding basic block
495 for (llvm::BasicBlock::iterator
496 inst_iter = succ_basic_block->begin(),
497 inst_end = succ_basic_block->end();
498 inst_iter != inst_end; ++inst_iter) {
499 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
500
501 if (!phi) {
502 break; // Meet non-phi instruction. Done.
503 }
504
505 if (handler_phi[phi] == NULL) {
506 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
507 }
508
509 // Create new_phi in landing pad
510 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
511 // Insert all incoming value into new_phi by rewrite_pair
512 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
513 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
514 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
515 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
516 }
517 // Delete all incoming value from phi by rewrite_pair
518 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
519 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
520 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
521 if (old_bb_idx >= 0) {
522 phi->removeIncomingValue(old_bb_idx, false);
523 }
524 }
525 // Insert new_phi into new handler phi
526 handler_phi[phi]->addIncoming(new_phi, lbb);
527 }
528 }
529 }
530
531 // Replace all handler phi
532 // We can't just use the old handler phi, because some exception edges will disappear after we
533 // compute fast-path.
534 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
535 llvm::PHINode* old_phi = it->first;
536 llvm::PHINode* new_phi = it->second;
537 new_phi->insertBefore(old_phi);
538 old_phi->replaceAllUsesWith(new_phi);
539 old_phi->eraseFromParent();
540 }
Logan Chien67645d82012-08-17 09:10:54 +0800541}
542
543void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
544 llvm::BasicBlock* new_basic_block) {
545 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
546
547 if (!term_inst) {
548 return; // No terminating instruction in new_basic_block. Nothing to do.
549 }
550
551 // Iterate every succeeding basic block
552 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
553 succ_iter != succ_end; ++succ_iter) {
554 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
555
556 // Iterate every phi instructions in the succeeding basic block
557 for (llvm::BasicBlock::iterator
558 inst_iter = succ_basic_block->begin(),
559 inst_end = succ_basic_block->end();
560 inst_iter != inst_end; ++inst_iter) {
561 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
562
563 if (!phi) {
564 break; // Meet non-phi instruction. Done.
565 }
566
567 // Update the incoming block of this phi instruction
568 for (llvm::PHINode::block_iterator
569 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
570 ibb_iter != ibb_end; ++ibb_iter) {
571 if (*ibb_iter == old_basic_block) {
572 *ibb_iter = new_basic_block;
573 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700574 }
575 }
576 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700577}
578
579llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
580 llvm::CallInst& inst) {
581 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
582 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
583 // function, therefore only called function is needed to change.
584 unsigned num_args = inst.getNumArgOperands();
585
586 if (num_args <= 0) {
587 return irb_.CreateCall(irb_.GetRuntime(rt));
588 } else {
589 std::vector<llvm::Value*> args;
590 for (unsigned i = 0; i < num_args; i++) {
591 args.push_back(inst.getArgOperand(i));
592 }
593
594 return irb_.CreateCall(irb_.GetRuntime(rt), args);
595 }
596}
597
Logan Chien67645d82012-08-17 09:10:54 +0800598void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700599GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
600 llvm::Function* func = first_non_alloca->getParent()->getParent();
601 llvm::Module* module = func->getParent();
602
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700603 // Call llvm intrinsic function to get frame address.
604 llvm::Function* frameaddress =
605 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
606
607 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
608 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
609
610 // Cast i8* to int
611 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
612
613 // Get thread.stack_end_
614 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700615 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700616 irb_.getPtrEquivIntTy(),
617 kTBAARuntimeInfo);
618
619 // Check the frame address < thread.stack_end_ ?
620 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
621
622 llvm::BasicBlock* block_exception =
623 llvm::BasicBlock::Create(context_, "stack_overflow", func);
624
625 llvm::BasicBlock* block_continue =
626 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
627
628 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
629
630 // If stack overflow, throw exception.
631 irb_.SetInsertPoint(block_exception);
632 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
633
634 // Unwind.
635 llvm::Type* ret_type = func->getReturnType();
636 if (ret_type->isVoidTy()) {
637 irb_.CreateRetVoid();
638 } else {
639 // The return value is ignored when there's an exception. MethodCompiler
640 // returns zero value under the the corresponding return type in this case.
641 // GBCExpander returns LLVM undef value here for brevity
642 irb_.CreateRet(llvm::UndefValue::get(ret_type));
643 }
644
645 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700646}
647
TDYa127920be7c2012-09-10 17:13:22 -0700648llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700649 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
650
651 return irb_.LoadFromObjectOffset(method_object_addr,
652 offset.Int32Value(),
653 irb_.getJObjectTy(),
654 kTBAAConstJObject);
655}
656
657llvm::Value*
658GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
659 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800660 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700661
662 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
663
664 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
665}
666
667llvm::Value*
668GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
669 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800670 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700671
672 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
673
674 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
675}
676
677llvm::Value* GBCExpanderPass::
678EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
679 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800680 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700681
682 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
683
684 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
685}
686
687llvm::Value* GBCExpanderPass::
688EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
689 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800690 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700691
692 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
693
694 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
695}
696
697llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
698 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
699 return parent_func->arg_begin();
700}
701
702llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
703 // Load array length
704 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800705 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700706 irb_.getJIntTy(),
707 kTBAAConstJObject);
708
709}
710
711llvm::Value*
712GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
713 llvm::Value* callee_method_object_field_addr =
714 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
715
TDYa127ce4cc0d2012-11-18 16:59:53 -0800716 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700717}
718
719llvm::Value* GBCExpanderPass::
720EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
721 // Load class object of *this* pointer
722 llvm::Value* class_object_addr =
723 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800724 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700725 irb_.getJObjectTy(),
726 kTBAAConstJObject);
727
728 // Load vtable address
729 llvm::Value* vtable_addr =
730 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800731 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700732 irb_.getJObjectTy(),
733 kTBAAConstJObject);
734
735 // Load callee method object
736 llvm::Value* vtable_idx_value =
737 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
738
739 llvm::Value* method_field_addr =
740 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
741
742 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
743}
744
745// Emit Array GetElementPtr
746llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
747 llvm::Value* index_value,
748 JType elem_jty) {
749
750 int data_offset;
751 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800752 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
753 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700754 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800755 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700756 }
757
758 llvm::Constant* data_offset_value =
759 irb_.getPtrEquivInt(data_offset);
760
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800761 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700762
763 llvm::Value* array_data_addr =
764 irb_.CreatePtrDisp(array_addr, data_offset_value,
765 elem_type->getPointerTo());
766
767 return irb_.CreateGEP(array_data_addr, index_value);
768}
769
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100770llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
771 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
772 art::InvokeType invoke_type =
773 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
774 bool is_static = (invoke_type == art::kStatic);
775 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
776
777 // Load *this* actual parameter
778 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
779
780 // Compute invoke related information for compiler decision
781 int vtable_idx = -1;
782 uintptr_t direct_code = 0;
783 uintptr_t direct_method = 0;
784 bool is_fast_path = driver_->
785 ComputeInvokeInfo(callee_method_idx, dex_compilation_unit_,
786 invoke_type, vtable_idx, direct_code, direct_method);
787
788 // Load the method object
789 llvm::Value* callee_method_object_addr = NULL;
790
791 if (!is_fast_path) {
792 callee_method_object_addr =
793 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
794 this_addr, dex_pc, is_fast_path);
795 } else {
796 switch (invoke_type) {
797 case art::kStatic:
798 case art::kDirect:
799 if (direct_method != 0u &&
800 direct_method != static_cast<uintptr_t>(-1)) {
801 callee_method_object_addr =
802 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
803 irb_.getJObjectTy());
804 } else {
805 callee_method_object_addr =
806 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
807 }
808 break;
809
810 case art::kVirtual:
811 DCHECK(vtable_idx != -1);
812 callee_method_object_addr =
813 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
814 break;
815
816 case art::kSuper:
817 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
818 "the fast path.";
819 break;
820
821 case art::kInterface:
822 callee_method_object_addr =
823 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
824 invoke_type, this_addr,
825 dex_pc, is_fast_path);
826 break;
827 }
828 }
829
830 // Load the actual parameter
831 std::vector<llvm::Value*> args;
832
833 args.push_back(callee_method_object_addr); // method object for callee
834
835 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
836 args.push_back(call_inst.getArgOperand(i));
837 }
838
839 llvm::Value* code_addr;
840 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
841 callee_method_idx, is_static);
842 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
843 code_addr =
844 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
845 func_type->getPointerTo());
846 } else {
847 code_addr =
848 irb_.LoadFromObjectOffset(callee_method_object_addr,
849 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
850 func_type->getPointerTo(), kTBAARuntimeInfo);
851 }
852
853 // Invoke callee
854 EmitUpdateDexPC(dex_pc);
855 llvm::Value* retval = irb_.CreateCall(code_addr, args);
856 EmitGuard_ExceptionLandingPad(dex_pc);
857
858 return retval;
859}
860
861bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
862 llvm::Value** result) {
863 DCHECK(result != NULL);
864
865 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
866 std::string callee_method_name(
867 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
868
869 if (callee_method_name == "int java.lang.String.length()") {
870 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
871 false /* is_empty */);
872 }
873 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
874 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
875 true /* is_empty */);
876 }
877
878 *result = NULL;
879 return false;
880}
881
882bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
883 llvm::Value** result,
884 bool is_empty) {
885 art::InvokeType invoke_type =
886 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
887 DCHECK_NE(invoke_type, art::kStatic);
888 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
889
890 llvm::Value* this_object = call_inst.getArgOperand(3);
891 llvm::Value* string_count =
892 irb_.LoadFromObjectOffset(this_object,
893 art::mirror::String::CountOffset().Int32Value(),
894 irb_.getJIntTy(),
895 kTBAAConstJObject);
896 if (is_empty) {
897 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
898 irb_.getJInt(0));
899 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
900 irb_.getJBoolean(true),
901 irb_.getJBoolean(false));
902 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
903 *result = is_empty;
904 } else {
905 *result = string_count;
906 }
907 return true;
908}
909
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700910void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800911 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
912
913 llvm::Value* suspend_count =
914 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
915 irb_.getInt16Ty(),
916 kTBAARuntimeInfo);
917 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
918
919 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
920 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
921
922 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
923
924 irb_.SetInsertPoint(basic_block_suspend);
925 if (dex_pc != art::DexFile::kDexNoIndex) {
926 EmitUpdateDexPC(dex_pc);
927 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700928 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800929
930 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
931 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
932 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
933
934 irb_.SetInsertPoint(basic_block_exception);
935 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
936 if (ret_type->isVoidTy()) {
937 irb_.CreateRetVoid();
938 } else {
939 // The return value is ignored when there's an exception.
940 irb_.CreateRet(llvm::UndefValue::get(ret_type));
941 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800942
943 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700944 return;
945}
946
TDYa1279a129452012-07-19 03:10:08 -0700947void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700948 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700949 return;
950}
951
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700952llvm::Value*
953GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
954 uint32_t string_idx =
955 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
956
957 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
958
TDYa127ce4cc0d2012-11-18 16:59:53 -0800959 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700960}
961
962llvm::Value*
963GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
964 uint32_t type_idx =
965 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
966
967 llvm::Value* type_field_addr =
968 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
969
TDYa127ce4cc0d2012-11-18 16:59:53 -0800970 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700971}
972
973void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700974 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700975 return;
976}
977
978void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700979 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700980 return;
981}
982
983llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
984 llvm::Value* index_value,
985 JType elem_jty) {
986 llvm::Value* array_elem_addr =
987 EmitArrayGEP(array_addr, index_value, elem_jty);
988
989 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
990}
991
992void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
993 llvm::Value* array_addr,
994 llvm::Value* index_value,
995 JType elem_jty) {
996 llvm::Value* array_elem_addr =
997 EmitArrayGEP(array_addr, index_value, elem_jty);
998
999 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1000
1001 return;
1002}
1003
1004void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1005 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1006 llvm::Value* array = call_inst.getArgOperand(0);
1007
1008 uint32_t element_jty =
1009 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1010
1011 DCHECK(call_inst.getNumArgOperands() > 2);
1012 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1013
1014 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1015
1016 uint32_t alignment;
1017 llvm::Constant* elem_size;
1018 llvm::PointerType* field_type;
1019
1020 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1021 // as the element, thus we are only checking 2 cases: primitive int and
1022 // non-primitive type.
1023 if (is_elem_int_ty) {
1024 alignment = sizeof(int32_t);
1025 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1026 field_type = irb_.getJIntTy()->getPointerTo();
1027 } else {
1028 alignment = irb_.getSizeOfPtrEquivInt();
1029 elem_size = irb_.getSizeOfPtrEquivIntValue();
1030 field_type = irb_.getJObjectTy()->getPointerTo();
1031 }
1032
1033 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001034 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001035
1036 llvm::Value* data_field_addr =
1037 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1038
1039 for (unsigned i = 0; i < num_elements; ++i) {
1040 // Values to fill the array begin at the 3rd argument
1041 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1042
1043 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1044
1045 data_field_addr =
1046 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1047 }
1048
1049 return;
1050}
1051
1052llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1053 llvm::Value* /*is_volatile_value*/,
1054 llvm::Value* object_addr,
1055 JType field_jty) {
1056 int field_offset =
1057 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1058
1059 DCHECK_GE(field_offset, 0);
1060
1061 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001062 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001063
1064 field_offset_value = irb_.getPtrEquivInt(field_offset);
1065
1066 llvm::Value* field_addr =
1067 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1068
1069 // TODO: Check is_volatile. We need to generate atomic load instruction
1070 // when is_volatile is true.
1071 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1072}
1073
1074void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1075 llvm::Value* /* is_volatile_value */,
1076 llvm::Value* object_addr,
1077 llvm::Value* new_value,
1078 JType field_jty) {
1079 int field_offset =
1080 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1081
1082 DCHECK_GE(field_offset, 0);
1083
1084 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001085 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001086
1087 field_offset_value = irb_.getPtrEquivInt(field_offset);
1088
1089 llvm::Value* field_addr =
1090 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1091
1092 // TODO: Check is_volatile. We need to generate atomic store instruction
1093 // when is_volatile is true.
1094 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1095
1096 return;
1097}
1098
1099llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1100 llvm::Value* field_offset_value,
1101 llvm::Value* /*is_volatile_value*/,
1102 JType field_jty) {
1103 int field_offset =
1104 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1105
1106 DCHECK_GE(field_offset, 0);
1107
1108 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1109
1110 llvm::Value* static_field_addr =
1111 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001112 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001113
1114 // TODO: Check is_volatile. We need to generate atomic store instruction
1115 // when is_volatile is true.
1116 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1117}
1118
1119void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1120 llvm::Value* field_offset_value,
1121 llvm::Value* /* is_volatile_value */,
1122 llvm::Value* new_value,
1123 JType field_jty) {
1124 int field_offset =
1125 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1126
1127 DCHECK_GE(field_offset, 0);
1128
1129 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1130
1131 llvm::Value* static_field_addr =
1132 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001133 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001134
1135 // TODO: Check is_volatile. We need to generate atomic store instruction
1136 // when is_volatile is true.
1137 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1138
1139 return;
1140}
1141
1142llvm::Value*
1143GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1144 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001145 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001146 irb_.getJObjectTy(),
1147 kTBAAConstJObject);
1148}
1149
1150llvm::Value*
1151GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1152 uint32_t type_idx =
1153 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1154
1155 llvm::Value* storage_field_addr =
1156 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1157
TDYa127ce4cc0d2012-11-18 16:59:53 -08001158 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001159}
1160
1161llvm::Value*
1162GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1163 uint32_t callee_method_idx =
1164 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1165
1166 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1167}
1168
1169llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1170 llvm::Value* vtable_idx_value,
1171 llvm::Value* this_addr) {
1172 int vtable_idx =
1173 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1174
1175 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1176}
1177
1178llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1179 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1180 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1181 unsigned num_args = call_inst.getNumArgOperands();
1182 llvm::Type* ret_type = call_inst.getType();
1183
1184 // Determine the function type of the callee method
1185 std::vector<llvm::Type*> args_type;
1186 std::vector<llvm::Value*> args;
1187 for (unsigned i = 0; i < num_args; i++) {
1188 args.push_back(call_inst.getArgOperand(i));
1189 args_type.push_back(args[i]->getType());
1190 }
1191
1192 llvm::FunctionType* callee_method_type =
1193 llvm::FunctionType::get(ret_type, args_type, false);
1194
1195 llvm::Value* code_addr =
1196 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001197 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001198 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001199 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001200
1201 // Invoke callee
1202 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1203
1204 return retval;
1205}
1206
TDYa1274ec8ccd2012-08-11 07:04:57 -07001207llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001208 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001209 llvm::Value* dividend = call_inst.getArgOperand(0);
1210 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001211 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1212 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001213 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1214
1215 // Check the special case: MININT / -1 = MININT
1216 // That case will cause overflow, which is undefined behavior in llvm.
1217 // So we check the divisor is -1 or not, if the divisor is -1, we do
1218 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001219 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001220 llvm::Value* zero = irb_.getJZero(op_jty);
1221 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1222
TDYa1275e869b62012-07-25 00:45:39 -07001223 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001224 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1225 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1226 llvm::BasicBlock* neg_one_cont =
1227 llvm::BasicBlock::Create(context_, "", parent);
1228
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001229 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1230 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1231
1232 // If divisor == -1
1233 irb_.SetInsertPoint(eq_neg_one);
1234 llvm::Value* eq_result;
1235 if (is_div) {
1236 // We can just change from "dividend div -1" to "neg dividend". The sub
1237 // don't care the sign/unsigned because of two's complement representation.
1238 // And the behavior is what we want:
1239 // -(2^n) (2^n)-1
1240 // MININT < k <= MAXINT -> mul k -1 = -k
1241 // MININT == k -> mul k -1 = k
1242 //
1243 // LLVM use sub to represent 'neg'
1244 eq_result = irb_.CreateSub(zero, dividend);
1245 } else {
1246 // Everything modulo -1 will be 0.
1247 eq_result = zero;
1248 }
1249 irb_.CreateBr(neg_one_cont);
1250
1251 // If divisor != -1, just do the division.
1252 irb_.SetInsertPoint(ne_neg_one);
1253 llvm::Value* ne_result;
1254 if (is_div) {
1255 ne_result = irb_.CreateSDiv(dividend, divisor);
1256 } else {
1257 ne_result = irb_.CreateSRem(dividend, divisor);
1258 }
1259 irb_.CreateBr(neg_one_cont);
1260
1261 irb_.SetInsertPoint(neg_one_cont);
1262 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1263 result->addIncoming(eq_result, eq_neg_one);
1264 result->addIncoming(ne_result, ne_neg_one);
1265
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001266 return result;
1267}
1268
TDYa127ce4cc0d2012-11-18 16:59:53 -08001269void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001270 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1271 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001272 uint16_t num_vregs =
1273 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001274
1275 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001276 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001277
Sebastien Hertz77209702013-02-28 16:34:13 +01001278 // Create allocas at the start of entry block.
1279 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1280 llvm::BasicBlock* entry_block = &func_->front();
1281 irb_.SetInsertPoint(&entry_block->front());
1282
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001283 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1284
1285 // Alloca a pointer to old shadow frame
1286 old_shadow_frame_ =
1287 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1288
Sebastien Hertz77209702013-02-28 16:34:13 +01001289 irb_.restoreIP(irb_ip_original);
1290
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001291 // Push the shadow frame
1292 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1293
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001294 llvm::Value* shadow_frame_upcast =
1295 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1296
1297 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1298 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001299 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001300
1301 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1302
1303 return;
1304}
1305
TDYa1278e950c12012-11-02 09:58:19 -07001306void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1307 llvm::Value* value) {
1308 DCHECK(shadow_frame_ != NULL);
1309
1310 llvm::Value* gep_index[] = {
1311 irb_.getInt32(0), // No pointer displacement
TDYa127ce4cc0d2012-11-18 16:59:53 -08001312 irb_.getInt32(1), // VRegs
TDYa1278e950c12012-11-02 09:58:19 -07001313 entry_idx // Pointer field
1314 };
1315
1316 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1317
1318 irb_.CreateStore(value,
1319 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1320 kTBAAShadowFrame);
1321 return;
1322}
1323
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001324void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001325 if (old_shadow_frame_ == NULL) {
1326 return;
1327 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001328 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1329 return;
1330}
1331
1332void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1333 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001334 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001335 dex_pc_value,
1336 kTBAAShadowFrame);
1337 return;
1338}
1339
Logan Chien67645d82012-08-17 09:10:54 +08001340void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001341 // All alloca instructions are generated in the first basic block of the
1342 // function, and there are no alloca instructions after the first non-alloca
1343 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001344
Logan Chien67645d82012-08-17 09:10:54 +08001345 llvm::BasicBlock* first_basic_block = &func.front();
1346
1347 // Look for first non-alloca instruction
1348 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001349 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1350 ++first_non_alloca;
1351 }
1352
Logan Chien67645d82012-08-17 09:10:54 +08001353 irb_.SetInsertPoint(first_non_alloca);
1354
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001355 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1356 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001357 EmitStackOverflowCheck(&*first_non_alloca);
1358
TDYa127890ea892012-08-22 10:49:42 -07001359 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001360
Logan Chien67645d82012-08-17 09:10:54 +08001361 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1362 if (next_basic_block != first_basic_block) {
1363 // Splice the rest of the instruction to the continuing basic block
1364 next_basic_block->getInstList().splice(
1365 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1366 first_non_alloca, first_basic_block->end());
1367
1368 // Rewrite the basic block
1369 RewriteBasicBlock(next_basic_block);
1370
1371 // Update the phi-instructions in the successor basic block
1372 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1373 }
1374
1375 // We have changed the basic block
1376 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001377}
1378
TDYa1275e869b62012-07-25 00:45:39 -07001379// ==== High-level intrinsic expander ==========================================
1380
TDYa127a1b21852012-07-23 03:20:39 -07001381llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1382 llvm::Value* src2_value,
1383 bool gt_bias) {
1384 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1385 llvm::Value* cmp_lt;
1386
1387 if (gt_bias) {
1388 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1389 } else {
1390 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1391 }
1392
1393 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1394}
1395
1396llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1397 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1398 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1399
1400 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1401}
1402
1403llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1404 llvm::Value* cmp_lt) {
1405
1406 llvm::Constant* zero = irb_.getJInt(0);
1407 llvm::Constant* pos1 = irb_.getJInt(1);
1408 llvm::Constant* neg1 = irb_.getJInt(-1);
1409
1410 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1411 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1412
1413 return result_eq;
1414}
1415
Logan Chien75e4b602012-07-23 14:24:12 -07001416llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1417 llvm::Value* src2_value,
1418 IntegerShiftKind kind,
1419 JType op_jty) {
1420 DCHECK(op_jty == kInt || op_jty == kLong);
1421
1422 // Mask and zero-extend RHS properly
1423 if (op_jty == kInt) {
1424 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1425 } else {
1426 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1427 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1428 }
1429
1430 // Create integer shift llvm instruction
1431 switch (kind) {
1432 case kIntegerSHL:
1433 return irb_.CreateShl(src1_value, src2_value);
1434
1435 case kIntegerSHR:
1436 return irb_.CreateAShr(src1_value, src2_value);
1437
1438 case kIntegerUSHR:
1439 return irb_.CreateLShr(src1_value, src2_value);
1440
1441 default:
1442 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1443 return NULL;
1444 }
1445}
1446
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001447llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1448 switch (jty) {
1449 case kBoolean:
1450 case kChar:
1451 return irb_.CreateZExt(value, irb_.getJType(kInt));
1452 case kByte:
1453 case kShort:
1454 return irb_.CreateSExt(value, irb_.getJType(kInt));
1455 case kVoid:
1456 case kInt:
1457 case kLong:
1458 case kFloat:
1459 case kDouble:
1460 case kObject:
1461 return value; // Nothing to do.
1462 default:
1463 LOG(FATAL) << "Unknown java type: " << jty;
1464 return NULL;
1465 }
1466}
1467
1468llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1469 switch (jty) {
1470 case kBoolean:
1471 case kChar:
1472 case kByte:
1473 case kShort:
1474 return irb_.CreateTrunc(value, irb_.getJType(jty));
1475 case kVoid:
1476 case kInt:
1477 case kLong:
1478 case kFloat:
1479 case kDouble:
1480 case kObject:
1481 return value; // Nothing to do.
1482 default:
1483 LOG(FATAL) << "Unknown java type: " << jty;
1484 return NULL;
1485 }
1486}
1487
TDYa1275a26d442012-07-26 18:58:38 -07001488llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1489 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001490 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1491 llvm::Value* array_addr = call_inst.getArgOperand(1);
1492 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001493 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001494
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001495 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1496 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1497 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001498
1499 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1500
1501 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1502
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001503 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001504}
1505
1506
1507void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1508 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001509 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1510 llvm::Value* new_value = call_inst.getArgOperand(1);
1511 llvm::Value* array_addr = call_inst.getArgOperand(2);
1512 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001513 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001514
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001515 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1516 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1517 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001518
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001519 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001520
1521 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1522
1523 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1524 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1525
1526 irb_.CreateCall2(runtime_func, new_value, array_addr);
1527
1528 EmitGuard_ExceptionLandingPad(dex_pc);
1529
1530 EmitMarkGCCard(new_value, array_addr);
1531 }
1532
1533 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1534
1535 return;
1536}
1537
TDYa1275e869b62012-07-25 00:45:39 -07001538llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1539 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001540 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1541 llvm::Value* object_addr = call_inst.getArgOperand(1);
1542 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001543 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001544
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001545 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001546
1547 llvm::Value* field_value;
1548
1549 int field_offset;
1550 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001551 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001552 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001553
1554 if (!is_fast_path) {
1555 llvm::Function* runtime_func;
1556
1557 if (field_jty == kObject) {
1558 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1559 } else if (field_jty == kLong || field_jty == kDouble) {
1560 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1561 } else {
1562 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1563 }
1564
1565 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1566
1567 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1568
1569 EmitUpdateDexPC(dex_pc);
1570
1571 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1572 method_object_addr, object_addr);
1573
1574 EmitGuard_ExceptionLandingPad(dex_pc);
1575
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001576 if (field_jty == kFloat || field_jty == kDouble) {
1577 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1578 }
TDYa1275e869b62012-07-25 00:45:39 -07001579 } else {
1580 DCHECK_GE(field_offset, 0);
1581
1582 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001583 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001584
1585 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1586
1587 llvm::Value* field_addr =
1588 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1589
TDYa1275e869b62012-07-25 00:45:39 -07001590 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001591 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001592
1593 if (is_volatile) {
1594 irb_.CreateMemoryBarrier(art::kLoadLoad);
1595 }
TDYa1275e869b62012-07-25 00:45:39 -07001596 }
1597
TDYa1275e869b62012-07-25 00:45:39 -07001598 return field_value;
1599}
1600
1601void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1602 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001603 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001604 llvm::Value* new_value = call_inst.getArgOperand(1);
1605 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001606 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001607 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001608
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001609 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001610
1611 int field_offset;
1612 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001613 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001614 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001615
1616 if (!is_fast_path) {
1617 llvm::Function* runtime_func;
1618
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001619 if (field_jty == kFloat) {
1620 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1621 } else if (field_jty == kDouble) {
1622 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1623 }
1624
TDYa1275e869b62012-07-25 00:45:39 -07001625 if (field_jty == kObject) {
1626 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1627 } else if (field_jty == kLong || field_jty == kDouble) {
1628 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1629 } else {
1630 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1631 }
1632
1633 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1634
1635 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1636
1637 EmitUpdateDexPC(dex_pc);
1638
1639 irb_.CreateCall4(runtime_func, field_idx_value,
1640 method_object_addr, object_addr, new_value);
1641
1642 EmitGuard_ExceptionLandingPad(dex_pc);
1643
1644 } else {
1645 DCHECK_GE(field_offset, 0);
1646
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001647 if (is_volatile) {
1648 irb_.CreateMemoryBarrier(art::kStoreStore);
1649 }
1650
TDYa1275e869b62012-07-25 00:45:39 -07001651 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001652 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001653
1654 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1655
1656 llvm::Value* field_addr =
1657 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1658
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001659 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001660 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1661
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001662 if (is_volatile) {
1663 irb_.CreateMemoryBarrier(art::kLoadLoad);
1664 }
1665
TDYa1275e869b62012-07-25 00:45:39 -07001666 if (field_jty == kObject) { // If put an object, mark the GC card table.
1667 EmitMarkGCCard(new_value, object_addr);
1668 }
1669 }
1670
1671 return;
1672}
1673
TDYa127f71bf5a2012-07-29 20:09:52 -07001674llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1675 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001676 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1677 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001678 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1679
1680 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1681
1682 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1683
1684 llvm::Function* runtime_func =
1685 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1686
1687 EmitUpdateDexPC(dex_pc);
1688
1689 llvm::Value* type_object_addr =
1690 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1691
1692 EmitGuard_ExceptionLandingPad(dex_pc);
1693
1694 return type_object_addr;
1695
1696 } else {
1697 // Try to load the class (type) object from the test cache.
1698 llvm::Value* type_field_addr =
1699 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1700
TDYa127ce4cc0d2012-11-18 16:59:53 -08001701 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001702
Ian Rogers89756f22013-03-04 16:40:02 -08001703 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001704 return type_object_addr;
1705 }
1706
1707 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1708
1709 // Test whether class (type) object is in the dex cache or not
1710 llvm::Value* equal_null =
1711 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1712
1713 llvm::BasicBlock* block_cont =
1714 CreateBasicBlockWithDexPC(dex_pc, "cont");
1715
1716 llvm::BasicBlock* block_load_class =
1717 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1718
1719 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1720
1721 // Failback routine to load the class object
1722 irb_.SetInsertPoint(block_load_class);
1723
1724 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1725
1726 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1727
1728 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1729
1730 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1731
1732 EmitUpdateDexPC(dex_pc);
1733
1734 llvm::Value* loaded_type_object_addr =
1735 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1736
1737 EmitGuard_ExceptionLandingPad(dex_pc);
1738
1739 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1740
1741 irb_.CreateBr(block_cont);
1742
1743 // Now the class object must be loaded
1744 irb_.SetInsertPoint(block_cont);
1745
1746 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1747
1748 phi->addIncoming(type_object_addr, block_original);
1749 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1750
1751 return phi;
1752 }
1753}
1754
TDYa1275a26d442012-07-26 18:58:38 -07001755llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1756 uint32_t type_idx) {
1757 llvm::BasicBlock* block_load_static =
1758 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1759
1760 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1761
1762 // Load static storage from dex cache
1763 llvm::Value* storage_field_addr =
1764 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1765
TDYa127ce4cc0d2012-11-18 16:59:53 -08001766 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001767
1768 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1769
1770 // Test: Is the static storage of this class initialized?
1771 llvm::Value* equal_null =
1772 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1773
1774 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1775
1776 // Failback routine to load the class object
1777 irb_.SetInsertPoint(block_load_static);
1778
1779 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1780
1781 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1782
1783 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1784
1785 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1786
1787 EmitUpdateDexPC(dex_pc);
1788
1789 llvm::Value* loaded_storage_object_addr =
1790 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1791
1792 EmitGuard_ExceptionLandingPad(dex_pc);
1793
1794 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1795
1796 irb_.CreateBr(block_cont);
1797
1798 // Now the class object must be loaded
1799 irb_.SetInsertPoint(block_cont);
1800
1801 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1802
1803 phi->addIncoming(storage_object_addr, block_original);
1804 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1805
1806 return phi;
1807}
1808
1809llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1810 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001811 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1812 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1813
1814 int field_offset;
1815 int ssb_index;
1816 bool is_referrers_class;
1817 bool is_volatile;
1818
Ian Rogers1212a022013-03-04 10:48:41 -08001819 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001820 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001821 is_referrers_class, is_volatile, false);
1822
1823 llvm::Value* static_field_value;
1824
1825 if (!is_fast_path) {
1826 llvm::Function* runtime_func;
1827
1828 if (field_jty == kObject) {
1829 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1830 } else if (field_jty == kLong || field_jty == kDouble) {
1831 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1832 } else {
1833 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1834 }
1835
1836 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1837
1838 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1839
1840 EmitUpdateDexPC(dex_pc);
1841
1842 static_field_value =
1843 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1844
1845 EmitGuard_ExceptionLandingPad(dex_pc);
1846
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001847 if (field_jty == kFloat || field_jty == kDouble) {
1848 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1849 }
TDYa1275a26d442012-07-26 18:58:38 -07001850 } else {
1851 DCHECK_GE(field_offset, 0);
1852
1853 llvm::Value* static_storage_addr = NULL;
1854
1855 if (is_referrers_class) {
1856 // Fast path, static storage base is this method's class
1857 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1858
1859 static_storage_addr =
1860 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001861 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001862 irb_.getJObjectTy(),
1863 kTBAAConstJObject);
1864 } else {
1865 // Medium path, static storage base in a different class which
1866 // requires checks that the other class is initialized
1867 DCHECK_GE(ssb_index, 0);
1868 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1869 }
1870
1871 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1872
1873 llvm::Value* static_field_addr =
1874 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001875 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001876
TDYa1275a26d442012-07-26 18:58:38 -07001877 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001878 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001879
1880 if (is_volatile) {
1881 irb_.CreateMemoryBarrier(art::kLoadLoad);
1882 }
TDYa1275a26d442012-07-26 18:58:38 -07001883 }
1884
TDYa1275a26d442012-07-26 18:58:38 -07001885 return static_field_value;
1886}
1887
1888void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1889 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001890 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1891 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1892 llvm::Value* new_value = call_inst.getArgOperand(1);
1893
1894 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001895 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001896 }
1897
1898 int field_offset;
1899 int ssb_index;
1900 bool is_referrers_class;
1901 bool is_volatile;
1902
Ian Rogers1212a022013-03-04 10:48:41 -08001903 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001904 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001905 is_referrers_class, is_volatile, true);
1906
1907 if (!is_fast_path) {
1908 llvm::Function* runtime_func;
1909
1910 if (field_jty == kObject) {
1911 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1912 } else if (field_jty == kLong || field_jty == kDouble) {
1913 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1914 } else {
1915 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1916 }
1917
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001918 if (field_jty == kFloat) {
1919 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1920 } else if (field_jty == kDouble) {
1921 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1922 }
1923
TDYa1275a26d442012-07-26 18:58:38 -07001924 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1925
1926 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1927
1928 EmitUpdateDexPC(dex_pc);
1929
1930 irb_.CreateCall3(runtime_func, field_idx_value,
1931 method_object_addr, new_value);
1932
1933 EmitGuard_ExceptionLandingPad(dex_pc);
1934
1935 } else {
1936 DCHECK_GE(field_offset, 0);
1937
1938 llvm::Value* static_storage_addr = NULL;
1939
1940 if (is_referrers_class) {
1941 // Fast path, static storage base is this method's class
1942 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1943
1944 static_storage_addr =
1945 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001946 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001947 irb_.getJObjectTy(),
1948 kTBAAConstJObject);
1949 } else {
1950 // Medium path, static storage base in a different class which
1951 // requires checks that the other class is initialized
1952 DCHECK_GE(ssb_index, 0);
1953 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1954 }
1955
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001956 if (is_volatile) {
1957 irb_.CreateMemoryBarrier(art::kStoreStore);
1958 }
1959
TDYa1275a26d442012-07-26 18:58:38 -07001960 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1961
1962 llvm::Value* static_field_addr =
1963 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001964 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001965
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001966 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001967 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1968
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001969 if (is_volatile) {
1970 irb_.CreateMemoryBarrier(art::kStoreLoad);
1971 }
1972
TDYa1275a26d442012-07-26 18:58:38 -07001973 if (field_jty == kObject) { // If put an object, mark the GC card table.
1974 EmitMarkGCCard(new_value, static_storage_addr);
1975 }
1976 }
1977
1978 return;
1979}
1980
TDYa127f71bf5a2012-07-29 20:09:52 -07001981llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001982 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1983 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1984
1985 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1986
TDYa127ce4cc0d2012-11-18 16:59:53 -08001987 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001988
Ian Rogers89756f22013-03-04 16:40:02 -08001989 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
1990 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001991 llvm::BasicBlock* block_str_exist =
1992 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1993
1994 llvm::BasicBlock* block_str_resolve =
1995 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1996
1997 llvm::BasicBlock* block_cont =
1998 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1999
2000 // Test: Is the string resolved and in the dex cache?
2001 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2002
2003 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2004
2005 // String is resolved, go to next basic block.
2006 irb_.SetInsertPoint(block_str_exist);
2007 irb_.CreateBr(block_cont);
2008
2009 // String is not resolved yet, resolve it now.
2010 irb_.SetInsertPoint(block_str_resolve);
2011
2012 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2013
2014 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2015
2016 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2017
2018 EmitUpdateDexPC(dex_pc);
2019
2020 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2021 string_idx_value);
2022
2023 EmitGuard_ExceptionLandingPad(dex_pc);
2024
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002025 irb_.CreateBr(block_cont);
2026
2027
TDYa127f71bf5a2012-07-29 20:09:52 -07002028 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2029
2030 irb_.SetInsertPoint(block_cont);
2031
2032 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2033
2034 phi->addIncoming(string_addr, block_str_exist);
2035 phi->addIncoming(result, block_pre_cont);
2036
2037 string_addr = phi;
2038 }
2039
2040 return string_addr;
2041}
2042
2043llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002044 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2045 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2046
2047 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2048
2049 return type_object_addr;
2050}
2051
2052void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002053 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2054 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002055 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002056
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002057 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002058
TDYa127ce4cc0d2012-11-18 16:59:53 -08002059 EmitUpdateDexPC(dex_pc);
2060
TDYa127f71bf5a2012-07-29 20:09:52 -07002061 irb_.Runtime().EmitLockObject(object_addr);
2062
2063 return;
2064}
2065
2066void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002067 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2068 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002069 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002070
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002071 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002072
2073 EmitUpdateDexPC(dex_pc);
2074
2075 irb_.Runtime().EmitUnlockObject(object_addr);
2076
2077 EmitGuard_ExceptionLandingPad(dex_pc);
2078
2079 return;
2080}
2081
2082void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002083 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2084 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2085 llvm::Value* object_addr = call_inst.getArgOperand(1);
2086
2087 llvm::BasicBlock* block_test_class =
2088 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2089
2090 llvm::BasicBlock* block_test_sub_class =
2091 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2092
2093 llvm::BasicBlock* block_cont =
2094 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2095
2096 // Test: Is the reference equal to null? Act as no-op when it is null.
2097 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2098
Ian Rogers8e696052013-03-04 09:00:40 -08002099 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002100
2101 // Test: Is the object instantiated from the given class?
2102 irb_.SetInsertPoint(block_test_class);
2103 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002104 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002105
2106 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2107
2108 llvm::Value* object_type_field_addr =
2109 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2110
2111 llvm::Value* object_type_object_addr =
2112 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2113
2114 llvm::Value* equal_class =
2115 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2116
Ian Rogers8e696052013-03-04 09:00:40 -08002117 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002118
2119 // Test: Is the object instantiated from the subclass of the given class?
2120 irb_.SetInsertPoint(block_test_sub_class);
2121
2122 EmitUpdateDexPC(dex_pc);
2123
2124 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2125 type_object_addr, object_type_object_addr);
2126
2127 EmitGuard_ExceptionLandingPad(dex_pc);
2128
2129 irb_.CreateBr(block_cont);
2130
2131 irb_.SetInsertPoint(block_cont);
2132
2133 return;
2134}
2135
2136llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002137 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2138 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2139 llvm::Value* object_addr = call_inst.getArgOperand(1);
2140
2141 llvm::BasicBlock* block_nullp =
2142 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2143
2144 llvm::BasicBlock* block_test_class =
2145 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2146
2147 llvm::BasicBlock* block_class_equals =
2148 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2149
2150 llvm::BasicBlock* block_test_sub_class =
2151 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2152
2153 llvm::BasicBlock* block_cont =
2154 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2155
2156 // Overview of the following code :
2157 // We check for null, if so, then false, otherwise check for class == . If so
2158 // then true, otherwise do callout slowpath.
2159 //
2160 // Test: Is the reference equal to null? Set 0 when it is null.
2161 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2162
Ian Rogers8e696052013-03-04 09:00:40 -08002163 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002164
2165 irb_.SetInsertPoint(block_nullp);
2166 irb_.CreateBr(block_cont);
2167
2168 // Test: Is the object instantiated from the given class?
2169 irb_.SetInsertPoint(block_test_class);
2170 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002171 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002172
2173 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2174
2175 llvm::Value* object_type_field_addr =
2176 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2177
2178 llvm::Value* object_type_object_addr =
2179 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2180
2181 llvm::Value* equal_class =
2182 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2183
Ian Rogers8e696052013-03-04 09:00:40 -08002184 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002185
2186 irb_.SetInsertPoint(block_class_equals);
2187 irb_.CreateBr(block_cont);
2188
2189 // Test: Is the object instantiated from the subclass of the given class?
2190 irb_.SetInsertPoint(block_test_sub_class);
2191 llvm::Value* result =
2192 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2193 type_object_addr, object_type_object_addr);
2194 irb_.CreateBr(block_cont);
2195
2196 irb_.SetInsertPoint(block_cont);
2197
2198 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2199
2200 phi->addIncoming(irb_.getJInt(0), block_nullp);
2201 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2202 phi->addIncoming(result, block_test_sub_class);
2203
2204 return phi;
2205}
2206
2207llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002208 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2209 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2210
2211 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002212 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2213 *dex_compilation_unit_->GetDexFile(),
2214 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002215 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2216 } else {
2217 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2218 }
2219
2220 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2221
2222 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2223
2224 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2225
2226 EmitUpdateDexPC(dex_pc);
2227
2228 llvm::Value* object_addr =
2229 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2230
2231 EmitGuard_ExceptionLandingPad(dex_pc);
2232
2233 return object_addr;
2234}
2235
2236llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002237 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2238 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002239
2240 if (!is_static) {
2241 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002242 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2243 llvm::Value* this_addr = call_inst.getArgOperand(3);
2244 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2245
2246 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002247 }
2248
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002249 llvm::Value* result = NULL;
2250 if (EmitIntrinsic(call_inst, &result)) {
2251 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002252 }
2253
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002254 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002255}
2256
2257llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002258 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2259 // Get the array object address
2260 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002261 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002262
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002263 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002264
2265 // Get the array length and store it to the register
2266 return EmitLoadArrayLength(array_addr);
2267}
2268
2269llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002270 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2271 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2272 llvm::Value* length = call_inst.getArgOperand(1);
2273
2274 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2275}
2276
2277llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002278 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2279 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2280 uint32_t length = call_inst.getNumArgOperands() - 3;
2281
2282 llvm::Value* object_addr =
2283 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2284
2285 if (length > 0) {
2286 // Check for the element type
2287 uint32_t type_desc_len = 0;
2288 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002289 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002290
2291 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2292 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2293 bool is_elem_int_ty = (type_desc[1] == 'I');
2294
2295 uint32_t alignment;
2296 llvm::Constant* elem_size;
2297 llvm::PointerType* field_type;
2298
2299 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2300 // as the element, thus we are only checking 2 cases: primitive int and
2301 // non-primitive type.
2302 if (is_elem_int_ty) {
2303 alignment = sizeof(int32_t);
2304 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2305 field_type = irb_.getJIntTy()->getPointerTo();
2306 } else {
2307 alignment = irb_.getSizeOfPtrEquivInt();
2308 elem_size = irb_.getSizeOfPtrEquivIntValue();
2309 field_type = irb_.getJObjectTy()->getPointerTo();
2310 }
2311
2312 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002313 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002314
2315 llvm::Value* data_field_addr =
2316 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2317
2318 // TODO: Tune this code. Currently we are generating one instruction for
2319 // one element which may be very space consuming. Maybe changing to use
2320 // memcpy may help; however, since we can't guarantee that the alloca of
2321 // dalvik register are continuous, we can't perform such optimization yet.
2322 for (uint32_t i = 0; i < length; ++i) {
2323 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2324
2325 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2326
2327 data_field_addr =
2328 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2329 }
2330 }
2331
2332 return object_addr;
2333}
2334
2335void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002336 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2337 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2338 LV2SInt(call_inst.getArgOperand(0));
2339 llvm::Value* array_addr = call_inst.getArgOperand(1);
2340
TDYa127920be7c2012-09-10 17:13:22 -07002341 const art::Instruction::ArrayDataPayload* payload =
2342 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002343 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002344
2345 if (payload->element_count == 0) {
2346 // When the number of the elements in the payload is zero, we don't have
2347 // to copy any numbers. However, we should check whether the array object
2348 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002349 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002350 } else {
2351 // To save the code size, we are going to call the runtime function to
2352 // copy the content from DexFile.
2353
2354 // NOTE: We will check for the NullPointerException in the runtime.
2355
2356 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2357
2358 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2359
2360 EmitUpdateDexPC(dex_pc);
2361
2362 irb_.CreateCall4(runtime_func,
2363 method_object_addr, irb_.getInt32(dex_pc),
2364 array_addr, irb_.getInt32(payload_offset));
2365
2366 EmitGuard_ExceptionLandingPad(dex_pc);
2367 }
2368
2369 return;
2370}
2371
2372llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2373 llvm::Value* array_length_value,
2374 uint32_t type_idx,
2375 bool is_filled_new_array) {
2376 llvm::Function* runtime_func;
2377
2378 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002379 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2380 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002381
2382
2383 if (is_filled_new_array) {
2384 runtime_func = skip_access_check ?
2385 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2386 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2387 } else {
2388 runtime_func = skip_access_check ?
2389 irb_.GetRuntime(runtime_support::AllocArray) :
2390 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2391 }
2392
2393 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2394
2395 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2396
2397 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2398
2399 EmitUpdateDexPC(dex_pc);
2400
2401 llvm::Value* object_addr =
2402 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2403 array_length_value, thread_object_addr);
2404
2405 EmitGuard_ExceptionLandingPad(dex_pc);
2406
2407 return object_addr;
2408}
2409
2410llvm::Value* GBCExpanderPass::
2411EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002412 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002413 llvm::Value* this_addr,
2414 uint32_t dex_pc,
2415 bool is_fast_path) {
2416
2417 llvm::Function* runtime_func = NULL;
2418
2419 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002420 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002421 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2422 break;
2423
TDYa127920be7c2012-09-10 17:13:22 -07002424 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002425 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2426 break;
2427
TDYa127920be7c2012-09-10 17:13:22 -07002428 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002429 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2430 break;
2431
TDYa127920be7c2012-09-10 17:13:22 -07002432 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002433 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2434 break;
2435
TDYa127920be7c2012-09-10 17:13:22 -07002436 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002437 if (is_fast_path) {
2438 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2439 } else {
2440 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2441 }
2442 break;
2443 }
2444
2445 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2446
2447 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002448 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002449 this_addr = irb_.getJNull();
2450 }
2451
2452 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2453
2454 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2455
2456 EmitUpdateDexPC(dex_pc);
2457
2458 llvm::Value* callee_method_object_addr =
2459 irb_.CreateCall4(runtime_func,
2460 callee_method_idx_value,
2461 this_addr,
2462 caller_method_object_addr,
2463 thread_object_addr);
2464
2465 EmitGuard_ExceptionLandingPad(dex_pc);
2466
2467 return callee_method_object_addr;
2468}
2469
TDYa1275e869b62012-07-25 00:45:39 -07002470void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2471 // Using runtime support, let the target can override by InlineAssembly.
2472 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2473}
2474
2475void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002476 if (shadow_frame_ == NULL) {
2477 return;
2478 }
TDYa1275e869b62012-07-25 00:45:39 -07002479 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002480 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002481 irb_.getInt32(dex_pc),
2482 kTBAAShadowFrame);
2483}
2484
2485void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2486 llvm::Value* denominator,
2487 JType op_jty) {
2488 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2489
2490 llvm::Constant* zero = irb_.getJZero(op_jty);
2491
2492 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2493
2494 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2495
2496 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2497
2498 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2499
2500 irb_.SetInsertPoint(block_exception);
2501 EmitUpdateDexPC(dex_pc);
2502 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2503 EmitBranchExceptionLandingPad(dex_pc);
2504
2505 irb_.SetInsertPoint(block_continue);
2506}
2507
2508void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002509 llvm::Value* object,
2510 int opt_flags) {
2511 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2512 if (ignore_null_check) {
2513 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2514 if (lpad) {
2515 // There is at least one catch: create a "fake" conditional branch to
2516 // keep the exception edge to the catch block.
2517 landing_pad_phi_mapping_[lpad].push_back(
2518 std::make_pair(current_bb_->getUniquePredecessor(),
2519 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002520
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002521 llvm::BasicBlock* block_continue =
2522 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002523
Ian Rogers8e696052013-03-04 09:00:40 -08002524 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002525
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002526 irb_.SetInsertPoint(block_continue);
2527 }
2528 } else {
2529 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002530
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002531 llvm::BasicBlock* block_exception =
2532 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002533
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002534 llvm::BasicBlock* block_continue =
2535 CreateBasicBlockWithDexPC(dex_pc, "cont");
2536
2537 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2538
2539 irb_.SetInsertPoint(block_exception);
2540 EmitUpdateDexPC(dex_pc);
2541 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2542 irb_.getInt32(dex_pc));
2543 EmitBranchExceptionLandingPad(dex_pc);
2544
2545 irb_.SetInsertPoint(block_continue);
2546 }
TDYa1275e869b62012-07-25 00:45:39 -07002547}
2548
2549void
2550GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2551 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002552 llvm::Value* index,
2553 int opt_flags) {
2554 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2555 if (ignore_range_check) {
2556 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2557 if (lpad) {
2558 // There is at least one catch: create a "fake" conditional branch to
2559 // keep the exception edge to the catch block.
2560 landing_pad_phi_mapping_[lpad].push_back(
2561 std::make_pair(current_bb_->getUniquePredecessor(),
2562 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002563
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002564 llvm::BasicBlock* block_continue =
2565 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002566
Ian Rogers8e696052013-03-04 09:00:40 -08002567 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002568
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002569 irb_.SetInsertPoint(block_continue);
2570 }
2571 } else {
2572 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002573
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002574 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002575
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002576 llvm::BasicBlock* block_exception =
2577 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002578
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002579 llvm::BasicBlock* block_continue =
2580 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002581
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002582 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2583
2584 irb_.SetInsertPoint(block_exception);
2585
2586 EmitUpdateDexPC(dex_pc);
2587 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2588 EmitBranchExceptionLandingPad(dex_pc);
2589
2590 irb_.SetInsertPoint(block_continue);
2591 }
TDYa1275e869b62012-07-25 00:45:39 -07002592}
2593
Ian Rogers8e696052013-03-04 09:00:40 -08002594llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002595 bool is_static) {
2596 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002597 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002598 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002599
2600 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002601 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002602 CHECK_GE(shorty_size, 1u);
2603
TDYa1275e869b62012-07-25 00:45:39 -07002604 // Get argument type
2605 std::vector<llvm::Type*> args_type;
2606
2607 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2608
2609 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002610 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002611 }
2612
2613 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002614 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002615 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002616 }
2617
2618 return llvm::FunctionType::get(ret_type, args_type, false);
2619}
2620
2621
2622llvm::BasicBlock* GBCExpanderPass::
2623CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2624 std::string name;
2625
2626#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002627 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002628#endif
2629
2630 return llvm::BasicBlock::Create(context_, name, func_);
2631}
2632
2633llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002634 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002635 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002636 return basic_blocks_[dex_pc];
2637}
2638
2639int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2640 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002641 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002642
2643 while (min <= max) {
2644 int32_t mid = min + (max - min) / 2;
2645
Ian Rogers89756f22013-03-04 16:40:02 -08002646 const art::DexFile::TryItem* ti =
2647 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002648 uint32_t start = ti->start_addr_;
2649 uint32_t end = start + ti->insn_count_;
2650
2651 if (dex_pc < start) {
2652 max = mid - 1;
2653 } else if (dex_pc >= end) {
2654 min = mid + 1;
2655 } else {
2656 return mid; // found
2657 }
2658 }
2659
2660 return -1; // not found
2661}
2662
2663llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2664 // Find the try item for this address in this method
2665 int32_t ti_offset = GetTryItemOffset(dex_pc);
2666
2667 if (ti_offset == -1) {
2668 return NULL; // No landing pad is available for this address.
2669 }
2670
2671 // Check for the existing landing pad basic block
2672 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2673 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2674
2675 if (block_lpad) {
2676 // We have generated landing pad for this try item already. Return the
2677 // same basic block.
2678 return block_lpad;
2679 }
2680
2681 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002682 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002683 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002684
2685 std::string lpadname;
2686
2687#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002688 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002689#endif
2690
2691 // Create landing pad basic block
2692 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2693
2694 // Change IRBuilder insert point
2695 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2696 irb_.SetInsertPoint(block_lpad);
2697
2698 // Find catch block with matching type
2699 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2700
2701 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2702
2703 llvm::Value* catch_handler_index_value =
2704 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2705 method_object_addr, ti_offset_value);
2706
2707 // Switch instruction (Go to unwind basic block by default)
2708 llvm::SwitchInst* sw =
2709 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2710
2711 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002712 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002713
2714 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2715 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2716 }
2717
2718 // Restore the orignal insert point for IRBuilder
2719 irb_.restoreIP(irb_ip_original);
2720
2721 // Cache this landing pad
2722 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2723 basic_block_landing_pads_[ti_offset] = block_lpad;
2724
2725 return block_lpad;
2726}
2727
2728llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2729 // Check the existing unwinding baisc block block
2730 if (basic_block_unwind_ != NULL) {
2731 return basic_block_unwind_;
2732 }
2733
2734 // Create new basic block for unwinding
2735 basic_block_unwind_ =
2736 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2737
2738 // Change IRBuilder insert point
2739 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2740 irb_.SetInsertPoint(basic_block_unwind_);
2741
2742 // Pop the shadow frame
2743 Expand_PopShadowFrame();
2744
2745 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002746 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002747 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002748 if (ret_shorty == 'V') {
2749 irb_.CreateRetVoid();
2750 } else {
2751 irb_.CreateRet(irb_.getJZero(ret_shorty));
2752 }
2753
2754 // Restore the orignal insert point for IRBuilder
2755 irb_.restoreIP(irb_ip_original);
2756
2757 return basic_block_unwind_;
2758}
2759
2760void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2761 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002762 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002763 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002764 irb_.CreateBr(lpad);
2765 } else {
2766 irb_.CreateBr(GetUnwindBasicBlock());
2767 }
2768}
2769
2770void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002771 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2772
TDYa1275e869b62012-07-25 00:45:39 -07002773 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2774
2775 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002776 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002777 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002778 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002779 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002780 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002781 }
2782
2783 irb_.SetInsertPoint(block_cont);
2784}
2785
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002786llvm::Value*
2787GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2788 llvm::CallInst& call_inst) {
2789 switch (intr_id) {
2790 //==- Thread -----------------------------------------------------------==//
2791 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002792 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002793 }
Logan Chien75e4b602012-07-23 14:24:12 -07002794 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002795 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002796 return NULL;
2797 }
2798 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002799 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002800 return NULL;
2801 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002802 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002803 Expand_MarkGCCard(call_inst);
2804 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002805 }
Logan Chien75e4b602012-07-23 14:24:12 -07002806
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002807 //==- Exception --------------------------------------------------------==//
2808 case IntrinsicHelper::ThrowException: {
2809 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2810 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002811 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002812 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2813
2814 EmitUpdateDexPC(dex_pc);
2815
2816 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2817 call_inst.getArgOperand(0));
2818
2819 EmitGuard_ExceptionLandingPad(dex_pc);
2820 return NULL;
2821 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002822 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002823 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002824 }
2825 case IntrinsicHelper::IsExceptionPending: {
2826 return irb_.Runtime().EmitIsExceptionPending();
2827 }
2828 case IntrinsicHelper::FindCatchBlock: {
2829 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2830 }
2831 case IntrinsicHelper::ThrowDivZeroException: {
2832 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2833 }
2834 case IntrinsicHelper::ThrowNullPointerException: {
2835 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2836 }
2837 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2838 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2839 }
Logan Chien75e4b602012-07-23 14:24:12 -07002840
2841 //==- Const String -----------------------------------------------------==//
2842 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002843 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002844 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002845 case IntrinsicHelper::LoadStringFromDexCache: {
2846 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2847 }
2848 case IntrinsicHelper::ResolveString: {
2849 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2850 }
Logan Chien75e4b602012-07-23 14:24:12 -07002851
2852 //==- Const Class ------------------------------------------------------==//
2853 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002854 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002855 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002856 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2857 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2858 }
2859 case IntrinsicHelper::LoadTypeFromDexCache: {
2860 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2861 }
2862 case IntrinsicHelper::InitializeType: {
2863 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2864 }
Logan Chien75e4b602012-07-23 14:24:12 -07002865
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002866 //==- Lock -------------------------------------------------------------==//
2867 case IntrinsicHelper::LockObject: {
2868 Expand_LockObject(call_inst.getArgOperand(0));
2869 return NULL;
2870 }
2871 case IntrinsicHelper::UnlockObject: {
2872 Expand_UnlockObject(call_inst.getArgOperand(0));
2873 return NULL;
2874 }
Logan Chien75e4b602012-07-23 14:24:12 -07002875
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002876 //==- Cast -------------------------------------------------------------==//
2877 case IntrinsicHelper::CheckCast: {
2878 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2879 }
Logan Chien75e4b602012-07-23 14:24:12 -07002880 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002881 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002882 return NULL;
2883 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002884 case IntrinsicHelper::IsAssignable: {
2885 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2886 }
Logan Chien75e4b602012-07-23 14:24:12 -07002887
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002888 //==- Alloc ------------------------------------------------------------==//
2889 case IntrinsicHelper::AllocObject: {
2890 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2891 }
2892 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2893 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2894 }
Logan Chien75e4b602012-07-23 14:24:12 -07002895
2896 //==- Instance ---------------------------------------------------------==//
2897 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002898 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002899 }
2900 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002901 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002902 }
2903
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002904 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002905 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002906 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002907 }
2908 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002909 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002910 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002911 case IntrinsicHelper::ArrayLength: {
2912 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2913 }
2914 case IntrinsicHelper::AllocArray: {
2915 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2916 }
2917 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2918 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2919 call_inst);
2920 }
2921 case IntrinsicHelper::CheckAndAllocArray: {
2922 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2923 }
2924 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2925 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2926 call_inst);
2927 }
2928 case IntrinsicHelper::ArrayGet: {
2929 return Expand_ArrayGet(call_inst.getArgOperand(0),
2930 call_inst.getArgOperand(1),
2931 kInt);
2932 }
2933 case IntrinsicHelper::ArrayGetWide: {
2934 return Expand_ArrayGet(call_inst.getArgOperand(0),
2935 call_inst.getArgOperand(1),
2936 kLong);
2937 }
2938 case IntrinsicHelper::ArrayGetObject: {
2939 return Expand_ArrayGet(call_inst.getArgOperand(0),
2940 call_inst.getArgOperand(1),
2941 kObject);
2942 }
2943 case IntrinsicHelper::ArrayGetBoolean: {
2944 return Expand_ArrayGet(call_inst.getArgOperand(0),
2945 call_inst.getArgOperand(1),
2946 kBoolean);
2947 }
2948 case IntrinsicHelper::ArrayGetByte: {
2949 return Expand_ArrayGet(call_inst.getArgOperand(0),
2950 call_inst.getArgOperand(1),
2951 kByte);
2952 }
2953 case IntrinsicHelper::ArrayGetChar: {
2954 return Expand_ArrayGet(call_inst.getArgOperand(0),
2955 call_inst.getArgOperand(1),
2956 kChar);
2957 }
2958 case IntrinsicHelper::ArrayGetShort: {
2959 return Expand_ArrayGet(call_inst.getArgOperand(0),
2960 call_inst.getArgOperand(1),
2961 kShort);
2962 }
2963 case IntrinsicHelper::ArrayPut: {
2964 Expand_ArrayPut(call_inst.getArgOperand(0),
2965 call_inst.getArgOperand(1),
2966 call_inst.getArgOperand(2),
2967 kInt);
2968 return NULL;
2969 }
2970 case IntrinsicHelper::ArrayPutWide: {
2971 Expand_ArrayPut(call_inst.getArgOperand(0),
2972 call_inst.getArgOperand(1),
2973 call_inst.getArgOperand(2),
2974 kLong);
2975 return NULL;
2976 }
2977 case IntrinsicHelper::ArrayPutObject: {
2978 Expand_ArrayPut(call_inst.getArgOperand(0),
2979 call_inst.getArgOperand(1),
2980 call_inst.getArgOperand(2),
2981 kObject);
2982 return NULL;
2983 }
2984 case IntrinsicHelper::ArrayPutBoolean: {
2985 Expand_ArrayPut(call_inst.getArgOperand(0),
2986 call_inst.getArgOperand(1),
2987 call_inst.getArgOperand(2),
2988 kBoolean);
2989 return NULL;
2990 }
2991 case IntrinsicHelper::ArrayPutByte: {
2992 Expand_ArrayPut(call_inst.getArgOperand(0),
2993 call_inst.getArgOperand(1),
2994 call_inst.getArgOperand(2),
2995 kByte);
2996 return NULL;
2997 }
2998 case IntrinsicHelper::ArrayPutChar: {
2999 Expand_ArrayPut(call_inst.getArgOperand(0),
3000 call_inst.getArgOperand(1),
3001 call_inst.getArgOperand(2),
3002 kChar);
3003 return NULL;
3004 }
3005 case IntrinsicHelper::ArrayPutShort: {
3006 Expand_ArrayPut(call_inst.getArgOperand(0),
3007 call_inst.getArgOperand(1),
3008 call_inst.getArgOperand(2),
3009 kShort);
3010 return NULL;
3011 }
3012 case IntrinsicHelper::CheckPutArrayElement: {
3013 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3014 }
3015 case IntrinsicHelper::FilledNewArray: {
3016 Expand_FilledNewArray(call_inst);
3017 return NULL;
3018 }
3019 case IntrinsicHelper::FillArrayData: {
3020 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3021 }
Logan Chien75e4b602012-07-23 14:24:12 -07003022 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003023 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003024 return NULL;
3025 }
3026 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003027 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003028 }
3029
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003030 //==- Instance Field ---------------------------------------------------==//
3031 case IntrinsicHelper::InstanceFieldGet:
3032 case IntrinsicHelper::InstanceFieldGetBoolean:
3033 case IntrinsicHelper::InstanceFieldGetByte:
3034 case IntrinsicHelper::InstanceFieldGetChar:
3035 case IntrinsicHelper::InstanceFieldGetShort: {
3036 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3037 }
3038 case IntrinsicHelper::InstanceFieldGetWide: {
3039 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3040 }
3041 case IntrinsicHelper::InstanceFieldGetObject: {
3042 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3043 }
3044 case IntrinsicHelper::InstanceFieldGetFast: {
3045 return Expand_IGetFast(call_inst.getArgOperand(0),
3046 call_inst.getArgOperand(1),
3047 call_inst.getArgOperand(2),
3048 kInt);
3049 }
3050 case IntrinsicHelper::InstanceFieldGetWideFast: {
3051 return Expand_IGetFast(call_inst.getArgOperand(0),
3052 call_inst.getArgOperand(1),
3053 call_inst.getArgOperand(2),
3054 kLong);
3055 }
3056 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3057 return Expand_IGetFast(call_inst.getArgOperand(0),
3058 call_inst.getArgOperand(1),
3059 call_inst.getArgOperand(2),
3060 kObject);
3061 }
3062 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3063 return Expand_IGetFast(call_inst.getArgOperand(0),
3064 call_inst.getArgOperand(1),
3065 call_inst.getArgOperand(2),
3066 kBoolean);
3067 }
3068 case IntrinsicHelper::InstanceFieldGetByteFast: {
3069 return Expand_IGetFast(call_inst.getArgOperand(0),
3070 call_inst.getArgOperand(1),
3071 call_inst.getArgOperand(2),
3072 kByte);
3073 }
3074 case IntrinsicHelper::InstanceFieldGetCharFast: {
3075 return Expand_IGetFast(call_inst.getArgOperand(0),
3076 call_inst.getArgOperand(1),
3077 call_inst.getArgOperand(2),
3078 kChar);
3079 }
3080 case IntrinsicHelper::InstanceFieldGetShortFast: {
3081 return Expand_IGetFast(call_inst.getArgOperand(0),
3082 call_inst.getArgOperand(1),
3083 call_inst.getArgOperand(2),
3084 kShort);
3085 }
3086 case IntrinsicHelper::InstanceFieldPut:
3087 case IntrinsicHelper::InstanceFieldPutBoolean:
3088 case IntrinsicHelper::InstanceFieldPutByte:
3089 case IntrinsicHelper::InstanceFieldPutChar:
3090 case IntrinsicHelper::InstanceFieldPutShort: {
3091 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3092 }
3093 case IntrinsicHelper::InstanceFieldPutWide: {
3094 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3095 }
3096 case IntrinsicHelper::InstanceFieldPutObject: {
3097 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3098 }
3099 case IntrinsicHelper::InstanceFieldPutFast: {
3100 Expand_IPutFast(call_inst.getArgOperand(0),
3101 call_inst.getArgOperand(1),
3102 call_inst.getArgOperand(2),
3103 call_inst.getArgOperand(3),
3104 kInt);
3105 return NULL;
3106 }
3107 case IntrinsicHelper::InstanceFieldPutWideFast: {
3108 Expand_IPutFast(call_inst.getArgOperand(0),
3109 call_inst.getArgOperand(1),
3110 call_inst.getArgOperand(2),
3111 call_inst.getArgOperand(3),
3112 kLong);
3113 return NULL;
3114 }
3115 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3116 Expand_IPutFast(call_inst.getArgOperand(0),
3117 call_inst.getArgOperand(1),
3118 call_inst.getArgOperand(2),
3119 call_inst.getArgOperand(3),
3120 kObject);
3121 return NULL;
3122 }
3123 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3124 Expand_IPutFast(call_inst.getArgOperand(0),
3125 call_inst.getArgOperand(1),
3126 call_inst.getArgOperand(2),
3127 call_inst.getArgOperand(3),
3128 kBoolean);
3129 return NULL;
3130 }
3131 case IntrinsicHelper::InstanceFieldPutByteFast: {
3132 Expand_IPutFast(call_inst.getArgOperand(0),
3133 call_inst.getArgOperand(1),
3134 call_inst.getArgOperand(2),
3135 call_inst.getArgOperand(3),
3136 kByte);
3137 return NULL;
3138 }
3139 case IntrinsicHelper::InstanceFieldPutCharFast: {
3140 Expand_IPutFast(call_inst.getArgOperand(0),
3141 call_inst.getArgOperand(1),
3142 call_inst.getArgOperand(2),
3143 call_inst.getArgOperand(3),
3144 kChar);
3145 return NULL;
3146 }
3147 case IntrinsicHelper::InstanceFieldPutShortFast: {
3148 Expand_IPutFast(call_inst.getArgOperand(0),
3149 call_inst.getArgOperand(1),
3150 call_inst.getArgOperand(2),
3151 call_inst.getArgOperand(3),
3152 kShort);
3153 return NULL;
3154 }
Logan Chien75e4b602012-07-23 14:24:12 -07003155
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003156 //==- Static Field -----------------------------------------------------==//
3157 case IntrinsicHelper::StaticFieldGet:
3158 case IntrinsicHelper::StaticFieldGetBoolean:
3159 case IntrinsicHelper::StaticFieldGetByte:
3160 case IntrinsicHelper::StaticFieldGetChar:
3161 case IntrinsicHelper::StaticFieldGetShort: {
3162 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3163 }
3164 case IntrinsicHelper::StaticFieldGetWide: {
3165 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3166 }
3167 case IntrinsicHelper::StaticFieldGetObject: {
3168 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3169 }
3170 case IntrinsicHelper::StaticFieldGetFast: {
3171 return Expand_SGetFast(call_inst.getArgOperand(0),
3172 call_inst.getArgOperand(1),
3173 call_inst.getArgOperand(2),
3174 kInt);
3175 }
3176 case IntrinsicHelper::StaticFieldGetWideFast: {
3177 return Expand_SGetFast(call_inst.getArgOperand(0),
3178 call_inst.getArgOperand(1),
3179 call_inst.getArgOperand(2),
3180 kLong);
3181 }
3182 case IntrinsicHelper::StaticFieldGetObjectFast: {
3183 return Expand_SGetFast(call_inst.getArgOperand(0),
3184 call_inst.getArgOperand(1),
3185 call_inst.getArgOperand(2),
3186 kObject);
3187 }
3188 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3189 return Expand_SGetFast(call_inst.getArgOperand(0),
3190 call_inst.getArgOperand(1),
3191 call_inst.getArgOperand(2),
3192 kBoolean);
3193 }
3194 case IntrinsicHelper::StaticFieldGetByteFast: {
3195 return Expand_SGetFast(call_inst.getArgOperand(0),
3196 call_inst.getArgOperand(1),
3197 call_inst.getArgOperand(2),
3198 kByte);
3199 }
3200 case IntrinsicHelper::StaticFieldGetCharFast: {
3201 return Expand_SGetFast(call_inst.getArgOperand(0),
3202 call_inst.getArgOperand(1),
3203 call_inst.getArgOperand(2),
3204 kChar);
3205 }
3206 case IntrinsicHelper::StaticFieldGetShortFast: {
3207 return Expand_SGetFast(call_inst.getArgOperand(0),
3208 call_inst.getArgOperand(1),
3209 call_inst.getArgOperand(2),
3210 kShort);
3211 }
3212 case IntrinsicHelper::StaticFieldPut:
3213 case IntrinsicHelper::StaticFieldPutBoolean:
3214 case IntrinsicHelper::StaticFieldPutByte:
3215 case IntrinsicHelper::StaticFieldPutChar:
3216 case IntrinsicHelper::StaticFieldPutShort: {
3217 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3218 }
3219 case IntrinsicHelper::StaticFieldPutWide: {
3220 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3221 }
3222 case IntrinsicHelper::StaticFieldPutObject: {
3223 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3224 }
3225 case IntrinsicHelper::StaticFieldPutFast: {
3226 Expand_SPutFast(call_inst.getArgOperand(0),
3227 call_inst.getArgOperand(1),
3228 call_inst.getArgOperand(2),
3229 call_inst.getArgOperand(3),
3230 kInt);
3231 return NULL;
3232 }
3233 case IntrinsicHelper::StaticFieldPutWideFast: {
3234 Expand_SPutFast(call_inst.getArgOperand(0),
3235 call_inst.getArgOperand(1),
3236 call_inst.getArgOperand(2),
3237 call_inst.getArgOperand(3),
3238 kLong);
3239 return NULL;
3240 }
3241 case IntrinsicHelper::StaticFieldPutObjectFast: {
3242 Expand_SPutFast(call_inst.getArgOperand(0),
3243 call_inst.getArgOperand(1),
3244 call_inst.getArgOperand(2),
3245 call_inst.getArgOperand(3),
3246 kObject);
3247 return NULL;
3248 }
3249 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3250 Expand_SPutFast(call_inst.getArgOperand(0),
3251 call_inst.getArgOperand(1),
3252 call_inst.getArgOperand(2),
3253 call_inst.getArgOperand(3),
3254 kBoolean);
3255 return NULL;
3256 }
3257 case IntrinsicHelper::StaticFieldPutByteFast: {
3258 Expand_SPutFast(call_inst.getArgOperand(0),
3259 call_inst.getArgOperand(1),
3260 call_inst.getArgOperand(2),
3261 call_inst.getArgOperand(3),
3262 kByte);
3263 return NULL;
3264 }
3265 case IntrinsicHelper::StaticFieldPutCharFast: {
3266 Expand_SPutFast(call_inst.getArgOperand(0),
3267 call_inst.getArgOperand(1),
3268 call_inst.getArgOperand(2),
3269 call_inst.getArgOperand(3),
3270 kChar);
3271 return NULL;
3272 }
3273 case IntrinsicHelper::StaticFieldPutShortFast: {
3274 Expand_SPutFast(call_inst.getArgOperand(0),
3275 call_inst.getArgOperand(1),
3276 call_inst.getArgOperand(2),
3277 call_inst.getArgOperand(3),
3278 kShort);
3279 return NULL;
3280 }
3281 case IntrinsicHelper::LoadDeclaringClassSSB: {
3282 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3283 }
3284 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3285 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3286 }
3287 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3288 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3289 }
Logan Chien75e4b602012-07-23 14:24:12 -07003290
3291 //==- High-level Array -------------------------------------------------==//
3292 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003293 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003294 }
3295 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003296 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003297 }
3298 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003299 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003300 }
3301 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003302 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003303 }
3304 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003305 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003306 }
3307 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003308 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003309 }
3310 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003311 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003312 }
3313 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003314 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003315 }
3316 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003317 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003318 }
3319 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003320 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003321 return NULL;
3322 }
3323 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003324 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003325 return NULL;
3326 }
3327 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003328 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003329 return NULL;
3330 }
3331 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003332 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003333 return NULL;
3334 }
3335 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003336 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003337 return NULL;
3338 }
3339 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003340 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003341 return NULL;
3342 }
3343 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003344 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003345 return NULL;
3346 }
3347 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003348 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003349 return NULL;
3350 }
3351 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003352 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003353 return NULL;
3354 }
3355
3356 //==- High-level Instance ----------------------------------------------==//
3357 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003358 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003359 }
3360 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003361 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003362 }
3363 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003364 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003365 }
3366 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003367 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003368 }
3369 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003370 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003371 }
3372 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003373 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003374 }
3375 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003376 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003377 }
3378 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003379 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003380 }
3381 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003382 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003383 }
3384 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003385 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003386 return NULL;
3387 }
3388 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003389 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003390 return NULL;
3391 }
3392 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003393 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003394 return NULL;
3395 }
3396 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003397 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003398 return NULL;
3399 }
3400 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003401 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003402 return NULL;
3403 }
3404 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003405 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 return NULL;
3407 }
3408 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003409 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003410 return NULL;
3411 }
3412 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003413 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003414 return NULL;
3415 }
3416 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003417 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 return NULL;
3419 }
3420
3421 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003422 case IntrinsicHelper::HLInvokeVoid:
3423 case IntrinsicHelper::HLInvokeObj:
3424 case IntrinsicHelper::HLInvokeInt:
3425 case IntrinsicHelper::HLInvokeFloat:
3426 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003427 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003428 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003429 }
3430
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003431 //==- Invoke -----------------------------------------------------------==//
3432 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3433 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3434 }
3435 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3436 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3437 }
3438 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3439 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3440 }
3441 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3442 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3443 }
3444 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3445 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3446 }
3447 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3448 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3449 }
3450 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3451 return Expand_GetVirtualCalleeMethodObjAddrFast(
3452 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3453 }
3454 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3455 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3456 }
3457 case IntrinsicHelper::InvokeRetVoid:
3458 case IntrinsicHelper::InvokeRetBoolean:
3459 case IntrinsicHelper::InvokeRetByte:
3460 case IntrinsicHelper::InvokeRetChar:
3461 case IntrinsicHelper::InvokeRetShort:
3462 case IntrinsicHelper::InvokeRetInt:
3463 case IntrinsicHelper::InvokeRetLong:
3464 case IntrinsicHelper::InvokeRetFloat:
3465 case IntrinsicHelper::InvokeRetDouble:
3466 case IntrinsicHelper::InvokeRetObject: {
3467 return Expand_Invoke(call_inst);
3468 }
Logan Chien75e4b602012-07-23 14:24:12 -07003469
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003470 //==- Math -------------------------------------------------------------==//
3471 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003472 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003473 }
3474 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003475 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003476 }
3477 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003478 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003479 }
3480 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003481 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003482 }
3483 case IntrinsicHelper::D2L: {
3484 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3485 }
3486 case IntrinsicHelper::D2I: {
3487 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3488 }
3489 case IntrinsicHelper::F2L: {
3490 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3491 }
3492 case IntrinsicHelper::F2I: {
3493 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3494 }
Logan Chien75e4b602012-07-23 14:24:12 -07003495
3496 //==- High-level Static ------------------------------------------------==//
3497 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003498 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003499 }
3500 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003501 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003502 }
3503 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003504 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003505 }
3506 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003507 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003508 }
3509 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003510 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003511 }
3512 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003513 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003514 }
3515 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003516 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003517 }
3518 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003519 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003520 }
3521 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003522 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003523 }
3524 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003525 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003526 return NULL;
3527 }
3528 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003529 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003530 return NULL;
3531 }
3532 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003533 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003534 return NULL;
3535 }
3536 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003537 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003538 return NULL;
3539 }
3540 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003541 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003542 return NULL;
3543 }
3544 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003545 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003546 return NULL;
3547 }
3548 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003549 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003550 return NULL;
3551 }
3552 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003553 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003554 return NULL;
3555 }
3556 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003557 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003558 return NULL;
3559 }
3560
3561 //==- High-level Monitor -----------------------------------------------==//
3562 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003563 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003564 return NULL;
3565 }
3566 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003567 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003568 return NULL;
3569 }
3570
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003571 //==- Shadow Frame -----------------------------------------------------==//
3572 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003573 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003574 return NULL;
3575 }
TDYa1278e950c12012-11-02 09:58:19 -07003576 case IntrinsicHelper::SetVReg: {
3577 Expand_SetVReg(call_inst.getArgOperand(0),
3578 call_inst.getArgOperand(1));
3579 return NULL;
3580 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003581 case IntrinsicHelper::PopShadowFrame: {
3582 Expand_PopShadowFrame();
3583 return NULL;
3584 }
3585 case IntrinsicHelper::UpdateDexPC: {
3586 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3587 return NULL;
3588 }
TDYa127a1b21852012-07-23 03:20:39 -07003589
Logan Chien75e4b602012-07-23 14:24:12 -07003590 //==- Comparison -------------------------------------------------------==//
3591 case IntrinsicHelper::CmplFloat:
3592 case IntrinsicHelper::CmplDouble: {
3593 return Expand_FPCompare(call_inst.getArgOperand(0),
3594 call_inst.getArgOperand(1),
3595 false);
3596 }
3597 case IntrinsicHelper::CmpgFloat:
3598 case IntrinsicHelper::CmpgDouble: {
3599 return Expand_FPCompare(call_inst.getArgOperand(0),
3600 call_inst.getArgOperand(1),
3601 true);
3602 }
3603 case IntrinsicHelper::CmpLong: {
3604 return Expand_LongCompare(call_inst.getArgOperand(0),
3605 call_inst.getArgOperand(1));
3606 }
TDYa127a1b21852012-07-23 03:20:39 -07003607
Logan Chien75e4b602012-07-23 14:24:12 -07003608 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003609 case IntrinsicHelper::ConstInt:
3610 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003611 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003612 }
TDYa127920be7c2012-09-10 17:13:22 -07003613 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003614 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3615 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003616 }
TDYa127920be7c2012-09-10 17:13:22 -07003617 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003618 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3619 irb_.getJDoubleTy());
3620 }
TDYa127920be7c2012-09-10 17:13:22 -07003621 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003622 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3623 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003624 }
3625
3626 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003627 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003628 // Nothing to be done, because MethodInfo carries optional hints that are
3629 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003630 return NULL;
3631 }
3632
3633 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003634 case IntrinsicHelper::CopyInt:
3635 case IntrinsicHelper::CopyFloat:
3636 case IntrinsicHelper::CopyLong:
3637 case IntrinsicHelper::CopyDouble:
3638 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003639 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003640 }
3641
3642 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003643 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003644 return Expand_IntegerShift(call_inst.getArgOperand(0),
3645 call_inst.getArgOperand(1),
3646 kIntegerSHL, kLong);
3647 }
TDYa127920be7c2012-09-10 17:13:22 -07003648 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003649 return Expand_IntegerShift(call_inst.getArgOperand(0),
3650 call_inst.getArgOperand(1),
3651 kIntegerSHR, kLong);
3652 }
TDYa127920be7c2012-09-10 17:13:22 -07003653 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003654 return Expand_IntegerShift(call_inst.getArgOperand(0),
3655 call_inst.getArgOperand(1),
3656 kIntegerUSHR, kLong);
3657 }
TDYa127920be7c2012-09-10 17:13:22 -07003658 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003659 return Expand_IntegerShift(call_inst.getArgOperand(0),
3660 call_inst.getArgOperand(1),
3661 kIntegerSHL, kInt);
3662 }
TDYa127920be7c2012-09-10 17:13:22 -07003663 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003664 return Expand_IntegerShift(call_inst.getArgOperand(0),
3665 call_inst.getArgOperand(1),
3666 kIntegerSHR, kInt);
3667 }
TDYa127920be7c2012-09-10 17:13:22 -07003668 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003669 return Expand_IntegerShift(call_inst.getArgOperand(0),
3670 call_inst.getArgOperand(1),
3671 kIntegerUSHR, kInt);
3672 }
3673
3674 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003675 case IntrinsicHelper::IntToChar: {
3676 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3677 irb_.getJIntTy());
3678 }
3679 case IntrinsicHelper::IntToShort: {
3680 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3681 irb_.getJIntTy());
3682 }
3683 case IntrinsicHelper::IntToByte: {
3684 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3685 irb_.getJIntTy());
3686 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003687
TDYa12787caa7e2012-08-25 23:23:27 -07003688 //==- Exception --------------------------------------------------------==//
3689 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003690 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003691 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3692 CHECK(si != NULL);
3693 irb_.CreateBr(si->getDefaultDest());
3694 si->eraseFromParent();
3695 return call_inst.getArgOperand(0);
3696 }
3697
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003698 //==- Constructor barrier-----------------------------------------------==//
3699 case IntrinsicHelper::ConstructorBarrier: {
3700 irb_.CreateMemoryBarrier(art::kStoreStore);
3701 return NULL;
3702 }
3703
Logan Chien75e4b602012-07-23 14:24:12 -07003704 //==- Unknown Cases ----------------------------------------------------==//
3705 case IntrinsicHelper::MaxIntrinsicId:
3706 case IntrinsicHelper::UnknownId:
3707 //default:
3708 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3709 // give some warning on unmatched cases.
3710 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003711 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003712 }
Logan Chien75e4b602012-07-23 14:24:12 -07003713 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003714 return NULL;
3715}
3716
3717} // anonymous namespace
3718
3719namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003720namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003721
Ian Rogers4c1c2832013-03-04 18:30:13 -08003722::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003723CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003724 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003725 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003726}
3727
Ian Rogers4c1c2832013-03-04 18:30:13 -08003728} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003729} // namespace art