blob: 2ae77a127e13e44c6860391ca5740594eb436464 [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
Sebastien Hertzf11afa02013-03-19 17:39:29 +010083 // Maps each vreg to its shadow frame address.
84 std::vector<llvm::Value*> shadow_frame_vreg_addresses_;
85
Logan Chien67645d82012-08-17 09:10:54 +080086 bool changed_;
87
TDYa1275e869b62012-07-25 00:45:39 -070088 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070089 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070090 // Constant for GBC expansion
91 //----------------------------------------------------------------------------
92 enum IntegerShiftKind {
93 kIntegerSHL,
94 kIntegerSHR,
95 kIntegerUSHR,
96 };
97
98 private:
99 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700100 // Helper function for GBC expansion
101 //----------------------------------------------------------------------------
102
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700103 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
104 llvm::CallInst& inst);
105
TDYa1275e869b62012-07-25 00:45:39 -0700106 uint64_t LV2UInt(llvm::Value* lv) {
107 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
108 }
109
110 int64_t LV2SInt(llvm::Value* lv) {
111 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
112 }
113
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700114 private:
115 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
116 // Refactor these utility functions from MethodCompiler to avoid forking.
117
Logan Chien67645d82012-08-17 09:10:54 +0800118 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
119
120 void RewriteFunction();
121
122 void RewriteBasicBlock(llvm::BasicBlock* original_block);
123
124 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
125 llvm::BasicBlock* new_basic_block);
126
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700127
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800128 // Sign or zero extend category 1 types < 32bits in size to 32bits.
129 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
130
131 // Truncate category 1 types from 32bits to the given JType size.
132 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
133
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700134 //----------------------------------------------------------------------------
135 // Dex cache code generation helper function
136 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700137 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700138
139 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
140
141 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
142
143 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
144
145 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
146
147 //----------------------------------------------------------------------------
148 // Code generation helper function
149 //----------------------------------------------------------------------------
150 llvm::Value* EmitLoadMethodObjectAddr();
151
152 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
153
154 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
155
156 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
157 llvm::Value* this_addr);
158
159 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
160 llvm::Value* index_value,
161 JType elem_jty);
162
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100163 //----------------------------------------------------------------------------
164 // Invoke helper function
165 //----------------------------------------------------------------------------
166 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
167
168 //----------------------------------------------------------------------------
169 // Inlining helper functions
170 //----------------------------------------------------------------------------
171 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
172
173 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
174 llvm::Value** result, bool is_empty);
175
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700176 private:
177 //----------------------------------------------------------------------------
178 // Expand Greenland intrinsics
179 //----------------------------------------------------------------------------
180 void Expand_TestSuspend(llvm::CallInst& call_inst);
181
TDYa1279a129452012-07-19 03:10:08 -0700182 void Expand_MarkGCCard(llvm::CallInst& call_inst);
183
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700184 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
185
186 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
187
188 void Expand_LockObject(llvm::Value* obj);
189
190 void Expand_UnlockObject(llvm::Value* obj);
191
192 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
193 llvm::Value* index_value,
194 JType elem_jty);
195
196 void Expand_ArrayPut(llvm::Value* new_value,
197 llvm::Value* array_addr,
198 llvm::Value* index_value,
199 JType elem_jty);
200
201 void Expand_FilledNewArray(llvm::CallInst& call_inst);
202
203 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
204 llvm::Value* is_volatile_value,
205 llvm::Value* object_addr,
206 JType field_jty);
207
208 void Expand_IPutFast(llvm::Value* field_offset_value,
209 llvm::Value* is_volatile_value,
210 llvm::Value* object_addr,
211 llvm::Value* new_value,
212 JType field_jty);
213
214 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
215 llvm::Value* field_offset_value,
216 llvm::Value* is_volatile_value,
217 JType field_jty);
218
219 void Expand_SPutFast(llvm::Value* static_storage_addr,
220 llvm::Value* field_offset_value,
221 llvm::Value* is_volatile_value,
222 llvm::Value* new_value,
223 JType field_jty);
224
225 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
226
227 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
228
229 llvm::Value*
230 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
231
232 llvm::Value*
233 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
234 llvm::Value* this_addr);
235
236 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
237
TDYa1274ec8ccd2012-08-11 07:04:57 -0700238 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700239
TDYa127ce4cc0d2012-11-18 16:59:53 -0800240 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700241
TDYa1278e950c12012-11-02 09:58:19 -0700242 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
243
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700244 void Expand_PopShadowFrame();
245
246 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
247
TDYa127a1b21852012-07-23 03:20:39 -0700248 //----------------------------------------------------------------------------
249 // Quick
250 //----------------------------------------------------------------------------
251
252 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
253 llvm::Value* src2_value,
254 bool gt_bias);
255
256 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
257
258 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
259 llvm::Value* cmp_lt);
260
TDYa127f71bf5a2012-07-29 20:09:52 -0700261 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700262 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
263
TDYa1275e869b62012-07-25 00:45:39 -0700264 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
265 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
266
TDYa1275a26d442012-07-26 18:58:38 -0700267 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
268 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
269
270 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
271 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
272
TDYa127f71bf5a2012-07-29 20:09:52 -0700273 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
274 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
275
276 void Expand_MonitorEnter(llvm::CallInst& call_inst);
277 void Expand_MonitorExit(llvm::CallInst& call_inst);
278
279 void Expand_HLCheckCast(llvm::CallInst& call_inst);
280 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
281
282 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
283
284 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
285
286 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
287 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
288 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
289 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
290
291 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
292 llvm::Value* array_length_value,
293 uint32_t type_idx,
294 bool is_filled_new_array);
295
296 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700297 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700298 llvm::Value* this_addr,
299 uint32_t dex_pc,
300 bool is_fast_path);
301
TDYa1275e869b62012-07-25 00:45:39 -0700302 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
303
304 void EmitUpdateDexPC(uint32_t dex_pc);
305
306 void EmitGuard_DivZeroException(uint32_t dex_pc,
307 llvm::Value* denominator,
308 JType op_jty);
309
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100310 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
311 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700312
313 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
314 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100315 llvm::Value* index,
316 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700317
Ian Rogers8e696052013-03-04 09:00:40 -0800318 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700319
320 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
321
322 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
323 const char* postfix);
324
325 int32_t GetTryItemOffset(uint32_t dex_pc);
326
327 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
328
329 llvm::BasicBlock* GetUnwindBasicBlock();
330
331 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
332
333 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
334
Logan Chien75e4b602012-07-23 14:24:12 -0700335 //----------------------------------------------------------------------------
336 // Expand Arithmetic Helper Intrinsics
337 //----------------------------------------------------------------------------
338
339 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
340 llvm::Value* src2_value,
341 IntegerShiftKind kind,
342 JType op_jty);
343
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700344 public:
345 static char ID;
346
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700347 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800348 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700349 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
350 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700351 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800352 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800353 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800354 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700355
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356 bool runOnFunction(llvm::Function& func);
357
358 private:
Logan Chien67645d82012-08-17 09:10:54 +0800359 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700360
361 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
362 llvm::CallInst& call_inst);
363
364};
365
366char GBCExpanderPass::ID = 0;
367
368bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800369 VLOG(compiler) << "GBC expansion on " << func.getName().str();
370
TDYa127b672d1e2012-06-28 21:21:45 -0700371 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800372 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700373 return false;
374 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700375
Logan Chien67645d82012-08-17 09:10:54 +0800376 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700377 shadow_frame_ = NULL;
378 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700379 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800380 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700381
Sebastien Hertzf11afa02013-03-19 17:39:29 +0100382 shadow_frame_vreg_addresses_.resize(dex_compilation_unit_->GetCodeItem()->registers_size_, NULL);
Ian Rogers89756f22013-03-04 16:40:02 -0800383 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
384 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700385 basic_block_unwind_ = NULL;
386 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
387 bb_iter != bb_end;
388 ++bb_iter) {
389 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
390 continue;
391 }
392 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
393 basic_blocks_[dex_pc] = bb_iter;
394 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700395
Logan Chien67645d82012-08-17 09:10:54 +0800396 // Insert stack overflow check
397 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700398
Logan Chien67645d82012-08-17 09:10:54 +0800399 // Rewrite the intrinsics
400 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700401
402 VERIFY_LLVM_FUNCTION(func);
403
Logan Chien67645d82012-08-17 09:10:54 +0800404 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700405}
406
Logan Chien67645d82012-08-17 09:10:54 +0800407void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
408 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700409
Logan Chien67645d82012-08-17 09:10:54 +0800410 llvm::BasicBlock::iterator inst_iter = original_block->begin();
411 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700412
Logan Chien67645d82012-08-17 09:10:54 +0800413 while (inst_iter != inst_end) {
414 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
415 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700416
Logan Chien67645d82012-08-17 09:10:54 +0800417 if (call_inst) {
418 llvm::Function* callee_func = call_inst->getCalledFunction();
419 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
420 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700421
Logan Chien67645d82012-08-17 09:10:54 +0800422 if (intr_id == IntrinsicHelper::UnknownId) {
423 // This is not intrinsic call. Skip this instruction.
424 ++inst_iter;
425 continue;
426 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700427
Logan Chien67645d82012-08-17 09:10:54 +0800428 // Rewrite the intrinsic and change the function
429 changed_ = true;
430 irb_.SetInsertPoint(inst_iter);
431
432 // Expand the intrinsic
433 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
434 inst_iter->replaceAllUsesWith(new_value);
435 }
436
437 // Remove the old intrinsic call instruction
438 llvm::BasicBlock::iterator old_inst = inst_iter++;
439 old_inst->eraseFromParent();
440
441 // Splice the instruction to the new basic block
442 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
443 if (next_basic_block != curr_basic_block) {
444 next_basic_block->getInstList().splice(
445 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
446 inst_iter, inst_end);
447 curr_basic_block = next_basic_block;
448 inst_end = curr_basic_block->end();
449 }
450 }
451}
452
453
454void GBCExpanderPass::RewriteFunction() {
455 size_t num_basic_blocks = func_->getBasicBlockList().size();
456 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
457 // because we will create new basic block while expanding the intrinsics.
458 // We only want to iterate through the input basic blocks.
459
TDYa127aa558872012-08-16 05:11:07 -0700460 landing_pad_phi_mapping_.clear();
461
Logan Chien67645d82012-08-17 09:10:54 +0800462 for (llvm::Function::iterator bb_iter = func_->begin();
463 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700464 // Set insert point to current basic block.
465 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800466
TDYa12755e5e6c2012-09-11 15:14:42 -0700467 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700468
Logan Chien67645d82012-08-17 09:10:54 +0800469 // Rewrite the basic block
470 RewriteBasicBlock(bb_iter);
471
472 // Update the phi-instructions in the successor basic block
473 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
474 if (last_block != bb_iter) {
475 UpdatePhiInstruction(bb_iter, last_block);
476 }
477 }
TDYa127aa558872012-08-16 05:11:07 -0700478
479 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
480 HandlerPHIMap handler_phi;
481 // Iterate every used landing pad basic block
482 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
483 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
484 if (lbb == NULL) {
485 continue;
486 }
487
488 llvm::TerminatorInst* term_inst = lbb->getTerminator();
489 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
490 = landing_pad_phi_mapping_[lbb];
491 irb_.SetInsertPoint(lbb->begin());
492
493 // Iterate every succeeding basic block (catch block)
494 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
495 succ_iter != succ_end; ++succ_iter) {
496 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
497
498 // Iterate every phi instructions in the succeeding basic block
499 for (llvm::BasicBlock::iterator
500 inst_iter = succ_basic_block->begin(),
501 inst_end = succ_basic_block->end();
502 inst_iter != inst_end; ++inst_iter) {
503 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
504
505 if (!phi) {
506 break; // Meet non-phi instruction. Done.
507 }
508
509 if (handler_phi[phi] == NULL) {
510 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
511 }
512
513 // Create new_phi in landing pad
514 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
515 // Insert all incoming value into new_phi by rewrite_pair
516 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
517 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
518 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
519 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
520 }
521 // Delete all incoming value from phi by rewrite_pair
522 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
523 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
524 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
525 if (old_bb_idx >= 0) {
526 phi->removeIncomingValue(old_bb_idx, false);
527 }
528 }
529 // Insert new_phi into new handler phi
530 handler_phi[phi]->addIncoming(new_phi, lbb);
531 }
532 }
533 }
534
535 // Replace all handler phi
536 // We can't just use the old handler phi, because some exception edges will disappear after we
537 // compute fast-path.
538 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
539 llvm::PHINode* old_phi = it->first;
540 llvm::PHINode* new_phi = it->second;
541 new_phi->insertBefore(old_phi);
542 old_phi->replaceAllUsesWith(new_phi);
543 old_phi->eraseFromParent();
544 }
Logan Chien67645d82012-08-17 09:10:54 +0800545}
546
547void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
548 llvm::BasicBlock* new_basic_block) {
549 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
550
551 if (!term_inst) {
552 return; // No terminating instruction in new_basic_block. Nothing to do.
553 }
554
555 // Iterate every succeeding basic block
556 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
557 succ_iter != succ_end; ++succ_iter) {
558 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
559
560 // Iterate every phi instructions in the succeeding basic block
561 for (llvm::BasicBlock::iterator
562 inst_iter = succ_basic_block->begin(),
563 inst_end = succ_basic_block->end();
564 inst_iter != inst_end; ++inst_iter) {
565 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
566
567 if (!phi) {
568 break; // Meet non-phi instruction. Done.
569 }
570
571 // Update the incoming block of this phi instruction
572 for (llvm::PHINode::block_iterator
573 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
574 ibb_iter != ibb_end; ++ibb_iter) {
575 if (*ibb_iter == old_basic_block) {
576 *ibb_iter = new_basic_block;
577 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700578 }
579 }
580 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700581}
582
583llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
584 llvm::CallInst& inst) {
585 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
586 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
587 // function, therefore only called function is needed to change.
588 unsigned num_args = inst.getNumArgOperands();
589
590 if (num_args <= 0) {
591 return irb_.CreateCall(irb_.GetRuntime(rt));
592 } else {
593 std::vector<llvm::Value*> args;
594 for (unsigned i = 0; i < num_args; i++) {
595 args.push_back(inst.getArgOperand(i));
596 }
597
598 return irb_.CreateCall(irb_.GetRuntime(rt), args);
599 }
600}
601
Logan Chien67645d82012-08-17 09:10:54 +0800602void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700603GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
604 llvm::Function* func = first_non_alloca->getParent()->getParent();
605 llvm::Module* module = func->getParent();
606
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700607 // Call llvm intrinsic function to get frame address.
608 llvm::Function* frameaddress =
609 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
610
611 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
612 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
613
614 // Cast i8* to int
615 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
616
617 // Get thread.stack_end_
618 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700619 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700620 irb_.getPtrEquivIntTy(),
621 kTBAARuntimeInfo);
622
623 // Check the frame address < thread.stack_end_ ?
624 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
625
626 llvm::BasicBlock* block_exception =
627 llvm::BasicBlock::Create(context_, "stack_overflow", func);
628
629 llvm::BasicBlock* block_continue =
630 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
631
632 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
633
634 // If stack overflow, throw exception.
635 irb_.SetInsertPoint(block_exception);
636 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
637
638 // Unwind.
639 llvm::Type* ret_type = func->getReturnType();
640 if (ret_type->isVoidTy()) {
641 irb_.CreateRetVoid();
642 } else {
643 // The return value is ignored when there's an exception. MethodCompiler
644 // returns zero value under the the corresponding return type in this case.
645 // GBCExpander returns LLVM undef value here for brevity
646 irb_.CreateRet(llvm::UndefValue::get(ret_type));
647 }
648
649 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700650}
651
TDYa127920be7c2012-09-10 17:13:22 -0700652llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700653 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
654
655 return irb_.LoadFromObjectOffset(method_object_addr,
656 offset.Int32Value(),
657 irb_.getJObjectTy(),
658 kTBAAConstJObject);
659}
660
661llvm::Value*
662GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
663 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800664 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700665
666 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
667
668 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
669}
670
671llvm::Value*
672GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
673 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800674 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700675
676 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
677
678 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
679}
680
681llvm::Value* GBCExpanderPass::
682EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
683 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800684 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700685
686 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
687
688 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
689}
690
691llvm::Value* GBCExpanderPass::
692EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
693 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800694 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700695
696 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
697
698 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
699}
700
701llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
702 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
703 return parent_func->arg_begin();
704}
705
706llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
707 // Load array length
708 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800709 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700710 irb_.getJIntTy(),
711 kTBAAConstJObject);
712
713}
714
715llvm::Value*
716GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
717 llvm::Value* callee_method_object_field_addr =
718 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
719
TDYa127ce4cc0d2012-11-18 16:59:53 -0800720 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700721}
722
723llvm::Value* GBCExpanderPass::
724EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
725 // Load class object of *this* pointer
726 llvm::Value* class_object_addr =
727 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800728 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700729 irb_.getJObjectTy(),
730 kTBAAConstJObject);
731
732 // Load vtable address
733 llvm::Value* vtable_addr =
734 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800735 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700736 irb_.getJObjectTy(),
737 kTBAAConstJObject);
738
739 // Load callee method object
740 llvm::Value* vtable_idx_value =
741 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
742
743 llvm::Value* method_field_addr =
744 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
745
746 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
747}
748
749// Emit Array GetElementPtr
750llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
751 llvm::Value* index_value,
752 JType elem_jty) {
753
754 int data_offset;
755 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800756 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
757 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700758 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800759 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700760 }
761
762 llvm::Constant* data_offset_value =
763 irb_.getPtrEquivInt(data_offset);
764
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800765 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700766
767 llvm::Value* array_data_addr =
768 irb_.CreatePtrDisp(array_addr, data_offset_value,
769 elem_type->getPointerTo());
770
771 return irb_.CreateGEP(array_data_addr, index_value);
772}
773
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100774llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
775 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
776 art::InvokeType invoke_type =
777 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
778 bool is_static = (invoke_type == art::kStatic);
779 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
780
781 // Load *this* actual parameter
782 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
783
784 // Compute invoke related information for compiler decision
785 int vtable_idx = -1;
786 uintptr_t direct_code = 0;
787 uintptr_t direct_method = 0;
788 bool is_fast_path = driver_->
789 ComputeInvokeInfo(callee_method_idx, dex_compilation_unit_,
790 invoke_type, vtable_idx, direct_code, direct_method);
791
792 // Load the method object
793 llvm::Value* callee_method_object_addr = NULL;
794
795 if (!is_fast_path) {
796 callee_method_object_addr =
797 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
798 this_addr, dex_pc, is_fast_path);
799 } else {
800 switch (invoke_type) {
801 case art::kStatic:
802 case art::kDirect:
803 if (direct_method != 0u &&
804 direct_method != static_cast<uintptr_t>(-1)) {
805 callee_method_object_addr =
806 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
807 irb_.getJObjectTy());
808 } else {
809 callee_method_object_addr =
810 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
811 }
812 break;
813
814 case art::kVirtual:
815 DCHECK(vtable_idx != -1);
816 callee_method_object_addr =
817 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
818 break;
819
820 case art::kSuper:
821 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
822 "the fast path.";
823 break;
824
825 case art::kInterface:
826 callee_method_object_addr =
827 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
828 invoke_type, this_addr,
829 dex_pc, is_fast_path);
830 break;
831 }
832 }
833
834 // Load the actual parameter
835 std::vector<llvm::Value*> args;
836
837 args.push_back(callee_method_object_addr); // method object for callee
838
839 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
840 args.push_back(call_inst.getArgOperand(i));
841 }
842
843 llvm::Value* code_addr;
844 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
845 callee_method_idx, is_static);
846 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
847 code_addr =
848 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
849 func_type->getPointerTo());
850 } else {
851 code_addr =
852 irb_.LoadFromObjectOffset(callee_method_object_addr,
853 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
854 func_type->getPointerTo(), kTBAARuntimeInfo);
855 }
856
857 // Invoke callee
858 EmitUpdateDexPC(dex_pc);
859 llvm::Value* retval = irb_.CreateCall(code_addr, args);
860 EmitGuard_ExceptionLandingPad(dex_pc);
861
862 return retval;
863}
864
865bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
866 llvm::Value** result) {
867 DCHECK(result != NULL);
868
869 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
870 std::string callee_method_name(
871 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
872
873 if (callee_method_name == "int java.lang.String.length()") {
874 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
875 false /* is_empty */);
876 }
877 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
878 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
879 true /* is_empty */);
880 }
881
882 *result = NULL;
883 return false;
884}
885
886bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
887 llvm::Value** result,
888 bool is_empty) {
889 art::InvokeType invoke_type =
890 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
891 DCHECK_NE(invoke_type, art::kStatic);
892 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
893
894 llvm::Value* this_object = call_inst.getArgOperand(3);
895 llvm::Value* string_count =
896 irb_.LoadFromObjectOffset(this_object,
897 art::mirror::String::CountOffset().Int32Value(),
898 irb_.getJIntTy(),
899 kTBAAConstJObject);
900 if (is_empty) {
901 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
902 irb_.getJInt(0));
903 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
904 irb_.getJBoolean(true),
905 irb_.getJBoolean(false));
906 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
907 *result = is_empty;
908 } else {
909 *result = string_count;
910 }
911 return true;
912}
913
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700914void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800915 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
916
917 llvm::Value* suspend_count =
918 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
919 irb_.getInt16Ty(),
920 kTBAARuntimeInfo);
921 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
922
923 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
924 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
925
926 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
927
928 irb_.SetInsertPoint(basic_block_suspend);
929 if (dex_pc != art::DexFile::kDexNoIndex) {
930 EmitUpdateDexPC(dex_pc);
931 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700932 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800933
934 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
935 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
936 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
937
938 irb_.SetInsertPoint(basic_block_exception);
939 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
940 if (ret_type->isVoidTy()) {
941 irb_.CreateRetVoid();
942 } else {
943 // The return value is ignored when there's an exception.
944 irb_.CreateRet(llvm::UndefValue::get(ret_type));
945 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800946
947 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700948 return;
949}
950
TDYa1279a129452012-07-19 03:10:08 -0700951void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700952 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700953 return;
954}
955
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700956llvm::Value*
957GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
958 uint32_t string_idx =
959 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
960
961 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
962
TDYa127ce4cc0d2012-11-18 16:59:53 -0800963 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700964}
965
966llvm::Value*
967GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
968 uint32_t type_idx =
969 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
970
971 llvm::Value* type_field_addr =
972 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
973
TDYa127ce4cc0d2012-11-18 16:59:53 -0800974 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700975}
976
977void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700978 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700979 return;
980}
981
982void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700983 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700984 return;
985}
986
987llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
988 llvm::Value* index_value,
989 JType elem_jty) {
990 llvm::Value* array_elem_addr =
991 EmitArrayGEP(array_addr, index_value, elem_jty);
992
993 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
994}
995
996void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
997 llvm::Value* array_addr,
998 llvm::Value* index_value,
999 JType elem_jty) {
1000 llvm::Value* array_elem_addr =
1001 EmitArrayGEP(array_addr, index_value, elem_jty);
1002
1003 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1004
1005 return;
1006}
1007
1008void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1009 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1010 llvm::Value* array = call_inst.getArgOperand(0);
1011
1012 uint32_t element_jty =
1013 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1014
1015 DCHECK(call_inst.getNumArgOperands() > 2);
1016 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1017
1018 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1019
1020 uint32_t alignment;
1021 llvm::Constant* elem_size;
1022 llvm::PointerType* field_type;
1023
1024 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1025 // as the element, thus we are only checking 2 cases: primitive int and
1026 // non-primitive type.
1027 if (is_elem_int_ty) {
1028 alignment = sizeof(int32_t);
1029 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1030 field_type = irb_.getJIntTy()->getPointerTo();
1031 } else {
1032 alignment = irb_.getSizeOfPtrEquivInt();
1033 elem_size = irb_.getSizeOfPtrEquivIntValue();
1034 field_type = irb_.getJObjectTy()->getPointerTo();
1035 }
1036
1037 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001038 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001039
1040 llvm::Value* data_field_addr =
1041 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1042
1043 for (unsigned i = 0; i < num_elements; ++i) {
1044 // Values to fill the array begin at the 3rd argument
1045 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1046
1047 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1048
1049 data_field_addr =
1050 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1051 }
1052
1053 return;
1054}
1055
1056llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1057 llvm::Value* /*is_volatile_value*/,
1058 llvm::Value* object_addr,
1059 JType field_jty) {
1060 int field_offset =
1061 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1062
1063 DCHECK_GE(field_offset, 0);
1064
1065 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001066 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001067
1068 field_offset_value = irb_.getPtrEquivInt(field_offset);
1069
1070 llvm::Value* field_addr =
1071 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1072
1073 // TODO: Check is_volatile. We need to generate atomic load instruction
1074 // when is_volatile is true.
1075 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1076}
1077
1078void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1079 llvm::Value* /* is_volatile_value */,
1080 llvm::Value* object_addr,
1081 llvm::Value* new_value,
1082 JType field_jty) {
1083 int field_offset =
1084 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1085
1086 DCHECK_GE(field_offset, 0);
1087
1088 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001089 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001090
1091 field_offset_value = irb_.getPtrEquivInt(field_offset);
1092
1093 llvm::Value* field_addr =
1094 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1095
1096 // TODO: Check is_volatile. We need to generate atomic store instruction
1097 // when is_volatile is true.
1098 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1099
1100 return;
1101}
1102
1103llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1104 llvm::Value* field_offset_value,
1105 llvm::Value* /*is_volatile_value*/,
1106 JType field_jty) {
1107 int field_offset =
1108 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1109
1110 DCHECK_GE(field_offset, 0);
1111
1112 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1113
1114 llvm::Value* static_field_addr =
1115 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001116 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001117
1118 // TODO: Check is_volatile. We need to generate atomic store instruction
1119 // when is_volatile is true.
1120 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1121}
1122
1123void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1124 llvm::Value* field_offset_value,
1125 llvm::Value* /* is_volatile_value */,
1126 llvm::Value* new_value,
1127 JType field_jty) {
1128 int field_offset =
1129 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1130
1131 DCHECK_GE(field_offset, 0);
1132
1133 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1134
1135 llvm::Value* static_field_addr =
1136 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001137 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001138
1139 // TODO: Check is_volatile. We need to generate atomic store instruction
1140 // when is_volatile is true.
1141 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1142
1143 return;
1144}
1145
1146llvm::Value*
1147GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1148 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001149 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001150 irb_.getJObjectTy(),
1151 kTBAAConstJObject);
1152}
1153
1154llvm::Value*
1155GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1156 uint32_t type_idx =
1157 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1158
1159 llvm::Value* storage_field_addr =
1160 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1161
TDYa127ce4cc0d2012-11-18 16:59:53 -08001162 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001163}
1164
1165llvm::Value*
1166GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1167 uint32_t callee_method_idx =
1168 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1169
1170 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1171}
1172
1173llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1174 llvm::Value* vtable_idx_value,
1175 llvm::Value* this_addr) {
1176 int vtable_idx =
1177 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1178
1179 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1180}
1181
1182llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1183 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1184 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1185 unsigned num_args = call_inst.getNumArgOperands();
1186 llvm::Type* ret_type = call_inst.getType();
1187
1188 // Determine the function type of the callee method
1189 std::vector<llvm::Type*> args_type;
1190 std::vector<llvm::Value*> args;
1191 for (unsigned i = 0; i < num_args; i++) {
1192 args.push_back(call_inst.getArgOperand(i));
1193 args_type.push_back(args[i]->getType());
1194 }
1195
1196 llvm::FunctionType* callee_method_type =
1197 llvm::FunctionType::get(ret_type, args_type, false);
1198
1199 llvm::Value* code_addr =
1200 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001201 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001202 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001203 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001204
1205 // Invoke callee
1206 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1207
1208 return retval;
1209}
1210
TDYa1274ec8ccd2012-08-11 07:04:57 -07001211llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001212 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001213 llvm::Value* dividend = call_inst.getArgOperand(0);
1214 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001215 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1216 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001217 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1218
1219 // Check the special case: MININT / -1 = MININT
1220 // That case will cause overflow, which is undefined behavior in llvm.
1221 // So we check the divisor is -1 or not, if the divisor is -1, we do
1222 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001223 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001224 llvm::Value* zero = irb_.getJZero(op_jty);
1225 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1226
TDYa1275e869b62012-07-25 00:45:39 -07001227 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001228 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1229 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1230 llvm::BasicBlock* neg_one_cont =
1231 llvm::BasicBlock::Create(context_, "", parent);
1232
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001233 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1234 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1235
1236 // If divisor == -1
1237 irb_.SetInsertPoint(eq_neg_one);
1238 llvm::Value* eq_result;
1239 if (is_div) {
1240 // We can just change from "dividend div -1" to "neg dividend". The sub
1241 // don't care the sign/unsigned because of two's complement representation.
1242 // And the behavior is what we want:
1243 // -(2^n) (2^n)-1
1244 // MININT < k <= MAXINT -> mul k -1 = -k
1245 // MININT == k -> mul k -1 = k
1246 //
1247 // LLVM use sub to represent 'neg'
1248 eq_result = irb_.CreateSub(zero, dividend);
1249 } else {
1250 // Everything modulo -1 will be 0.
1251 eq_result = zero;
1252 }
1253 irb_.CreateBr(neg_one_cont);
1254
1255 // If divisor != -1, just do the division.
1256 irb_.SetInsertPoint(ne_neg_one);
1257 llvm::Value* ne_result;
1258 if (is_div) {
1259 ne_result = irb_.CreateSDiv(dividend, divisor);
1260 } else {
1261 ne_result = irb_.CreateSRem(dividend, divisor);
1262 }
1263 irb_.CreateBr(neg_one_cont);
1264
1265 irb_.SetInsertPoint(neg_one_cont);
1266 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1267 result->addIncoming(eq_result, eq_neg_one);
1268 result->addIncoming(ne_result, ne_neg_one);
1269
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001270 return result;
1271}
1272
TDYa127ce4cc0d2012-11-18 16:59:53 -08001273void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001274 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1275 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001276 uint16_t num_vregs =
1277 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001278
1279 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001280 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001281
Sebastien Hertz77209702013-02-28 16:34:13 +01001282 // Create allocas at the start of entry block.
1283 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1284 llvm::BasicBlock* entry_block = &func_->front();
1285 irb_.SetInsertPoint(&entry_block->front());
1286
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001287 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1288
1289 // Alloca a pointer to old shadow frame
1290 old_shadow_frame_ =
1291 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1292
Sebastien Hertz77209702013-02-28 16:34:13 +01001293 irb_.restoreIP(irb_ip_original);
1294
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001295 // Push the shadow frame
1296 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1297
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001298 llvm::Value* shadow_frame_upcast =
1299 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1300
1301 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1302 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001303 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001304
1305 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1306
1307 return;
1308}
1309
TDYa1278e950c12012-11-02 09:58:19 -07001310void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1311 llvm::Value* value) {
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001312 unsigned vreg_idx = LV2UInt(entry_idx);
1313 DCHECK_LT(vreg_idx, dex_compilation_unit_->GetCodeItem()->registers_size_);
TDYa1278e950c12012-11-02 09:58:19 -07001314
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001315 llvm::Value* vreg_addr = shadow_frame_vreg_addresses_[vreg_idx];
1316 if (UNLIKELY(vreg_addr == NULL)) {
1317 DCHECK(shadow_frame_ != NULL);
TDYa1278e950c12012-11-02 09:58:19 -07001318
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001319 llvm::Value* gep_index[] = {
1320 irb_.getInt32(0), // No pointer displacement
1321 irb_.getInt32(1), // VRegs
1322 entry_idx // Pointer field
1323 };
1324
1325 // A shadow frame address must dominate every use in the function so we
1326 // place it in the entry block right after the allocas.
1327 llvm::BasicBlock::iterator first_non_alloca = func_->getEntryBlock().begin();
1328 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1329 ++first_non_alloca;
1330 }
1331
1332 llvm::IRBuilderBase::InsertPoint ip = irb_.saveIP();
1333 irb_.SetInsertPoint(static_cast<llvm::Instruction*>(first_non_alloca));
1334 vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1335 shadow_frame_vreg_addresses_[vreg_idx] = vreg_addr;
1336 irb_.restoreIP(ip);
1337 }
TDYa1278e950c12012-11-02 09:58:19 -07001338
1339 irb_.CreateStore(value,
1340 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1341 kTBAAShadowFrame);
1342 return;
1343}
1344
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001345void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001346 if (old_shadow_frame_ == NULL) {
1347 return;
1348 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001349 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1350 return;
1351}
1352
1353void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1354 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001355 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001356 dex_pc_value,
1357 kTBAAShadowFrame);
1358 return;
1359}
1360
Logan Chien67645d82012-08-17 09:10:54 +08001361void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001362 // All alloca instructions are generated in the first basic block of the
1363 // function, and there are no alloca instructions after the first non-alloca
1364 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001365
Logan Chien67645d82012-08-17 09:10:54 +08001366 llvm::BasicBlock* first_basic_block = &func.front();
1367
1368 // Look for first non-alloca instruction
1369 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001370 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1371 ++first_non_alloca;
1372 }
1373
Logan Chien67645d82012-08-17 09:10:54 +08001374 irb_.SetInsertPoint(first_non_alloca);
1375
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001376 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1377 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001378 EmitStackOverflowCheck(&*first_non_alloca);
1379
TDYa127890ea892012-08-22 10:49:42 -07001380 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001381
Logan Chien67645d82012-08-17 09:10:54 +08001382 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1383 if (next_basic_block != first_basic_block) {
1384 // Splice the rest of the instruction to the continuing basic block
1385 next_basic_block->getInstList().splice(
1386 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1387 first_non_alloca, first_basic_block->end());
1388
1389 // Rewrite the basic block
1390 RewriteBasicBlock(next_basic_block);
1391
1392 // Update the phi-instructions in the successor basic block
1393 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1394 }
1395
1396 // We have changed the basic block
1397 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001398}
1399
TDYa1275e869b62012-07-25 00:45:39 -07001400// ==== High-level intrinsic expander ==========================================
1401
TDYa127a1b21852012-07-23 03:20:39 -07001402llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1403 llvm::Value* src2_value,
1404 bool gt_bias) {
1405 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1406 llvm::Value* cmp_lt;
1407
1408 if (gt_bias) {
1409 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1410 } else {
1411 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1412 }
1413
1414 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1415}
1416
1417llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1418 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1419 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1420
1421 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1422}
1423
1424llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1425 llvm::Value* cmp_lt) {
1426
1427 llvm::Constant* zero = irb_.getJInt(0);
1428 llvm::Constant* pos1 = irb_.getJInt(1);
1429 llvm::Constant* neg1 = irb_.getJInt(-1);
1430
1431 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1432 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1433
1434 return result_eq;
1435}
1436
Logan Chien75e4b602012-07-23 14:24:12 -07001437llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1438 llvm::Value* src2_value,
1439 IntegerShiftKind kind,
1440 JType op_jty) {
1441 DCHECK(op_jty == kInt || op_jty == kLong);
1442
1443 // Mask and zero-extend RHS properly
1444 if (op_jty == kInt) {
1445 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1446 } else {
1447 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1448 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1449 }
1450
1451 // Create integer shift llvm instruction
1452 switch (kind) {
1453 case kIntegerSHL:
1454 return irb_.CreateShl(src1_value, src2_value);
1455
1456 case kIntegerSHR:
1457 return irb_.CreateAShr(src1_value, src2_value);
1458
1459 case kIntegerUSHR:
1460 return irb_.CreateLShr(src1_value, src2_value);
1461
1462 default:
1463 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1464 return NULL;
1465 }
1466}
1467
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001468llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1469 switch (jty) {
1470 case kBoolean:
1471 case kChar:
1472 return irb_.CreateZExt(value, irb_.getJType(kInt));
1473 case kByte:
1474 case kShort:
1475 return irb_.CreateSExt(value, irb_.getJType(kInt));
1476 case kVoid:
1477 case kInt:
1478 case kLong:
1479 case kFloat:
1480 case kDouble:
1481 case kObject:
1482 return value; // Nothing to do.
1483 default:
1484 LOG(FATAL) << "Unknown java type: " << jty;
1485 return NULL;
1486 }
1487}
1488
1489llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1490 switch (jty) {
1491 case kBoolean:
1492 case kChar:
1493 case kByte:
1494 case kShort:
1495 return irb_.CreateTrunc(value, irb_.getJType(jty));
1496 case kVoid:
1497 case kInt:
1498 case kLong:
1499 case kFloat:
1500 case kDouble:
1501 case kObject:
1502 return value; // Nothing to do.
1503 default:
1504 LOG(FATAL) << "Unknown java type: " << jty;
1505 return NULL;
1506 }
1507}
1508
TDYa1275a26d442012-07-26 18:58:38 -07001509llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1510 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001511 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1512 llvm::Value* array_addr = call_inst.getArgOperand(1);
1513 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001514 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001515
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001516 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1517 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1518 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001519
1520 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1521
1522 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1523
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001524 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001525}
1526
1527
1528void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1529 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001530 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1531 llvm::Value* new_value = call_inst.getArgOperand(1);
1532 llvm::Value* array_addr = call_inst.getArgOperand(2);
1533 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001534 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001535
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001536 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1537 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1538 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001539
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001540 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001541
1542 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1543
1544 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1545 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1546
1547 irb_.CreateCall2(runtime_func, new_value, array_addr);
1548
1549 EmitGuard_ExceptionLandingPad(dex_pc);
1550
1551 EmitMarkGCCard(new_value, array_addr);
1552 }
1553
1554 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1555
1556 return;
1557}
1558
TDYa1275e869b62012-07-25 00:45:39 -07001559llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1560 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001561 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1562 llvm::Value* object_addr = call_inst.getArgOperand(1);
1563 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001564 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001565
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001566 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001567
1568 llvm::Value* field_value;
1569
1570 int field_offset;
1571 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001572 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001573 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001574
1575 if (!is_fast_path) {
1576 llvm::Function* runtime_func;
1577
1578 if (field_jty == kObject) {
1579 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1580 } else if (field_jty == kLong || field_jty == kDouble) {
1581 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1582 } else {
1583 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1584 }
1585
1586 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1587
1588 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1589
1590 EmitUpdateDexPC(dex_pc);
1591
1592 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1593 method_object_addr, object_addr);
1594
1595 EmitGuard_ExceptionLandingPad(dex_pc);
1596
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001597 if (field_jty == kFloat || field_jty == kDouble) {
1598 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1599 }
TDYa1275e869b62012-07-25 00:45:39 -07001600 } else {
1601 DCHECK_GE(field_offset, 0);
1602
1603 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001604 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001605
1606 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1607
1608 llvm::Value* field_addr =
1609 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1610
TDYa1275e869b62012-07-25 00:45:39 -07001611 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001612 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001613
1614 if (is_volatile) {
1615 irb_.CreateMemoryBarrier(art::kLoadLoad);
1616 }
TDYa1275e869b62012-07-25 00:45:39 -07001617 }
1618
TDYa1275e869b62012-07-25 00:45:39 -07001619 return field_value;
1620}
1621
1622void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1623 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001624 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001625 llvm::Value* new_value = call_inst.getArgOperand(1);
1626 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001627 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001628 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001629
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001630 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001631
1632 int field_offset;
1633 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001634 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001635 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001636
1637 if (!is_fast_path) {
1638 llvm::Function* runtime_func;
1639
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001640 if (field_jty == kFloat) {
1641 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1642 } else if (field_jty == kDouble) {
1643 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1644 }
1645
TDYa1275e869b62012-07-25 00:45:39 -07001646 if (field_jty == kObject) {
1647 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1648 } else if (field_jty == kLong || field_jty == kDouble) {
1649 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1650 } else {
1651 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1652 }
1653
1654 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1655
1656 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1657
1658 EmitUpdateDexPC(dex_pc);
1659
1660 irb_.CreateCall4(runtime_func, field_idx_value,
1661 method_object_addr, object_addr, new_value);
1662
1663 EmitGuard_ExceptionLandingPad(dex_pc);
1664
1665 } else {
1666 DCHECK_GE(field_offset, 0);
1667
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001668 if (is_volatile) {
1669 irb_.CreateMemoryBarrier(art::kStoreStore);
1670 }
1671
TDYa1275e869b62012-07-25 00:45:39 -07001672 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001673 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001674
1675 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1676
1677 llvm::Value* field_addr =
1678 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1679
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001680 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001681 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1682
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001683 if (is_volatile) {
1684 irb_.CreateMemoryBarrier(art::kLoadLoad);
1685 }
1686
TDYa1275e869b62012-07-25 00:45:39 -07001687 if (field_jty == kObject) { // If put an object, mark the GC card table.
1688 EmitMarkGCCard(new_value, object_addr);
1689 }
1690 }
1691
1692 return;
1693}
1694
TDYa127f71bf5a2012-07-29 20:09:52 -07001695llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1696 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001697 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1698 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001699 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1700
1701 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1702
1703 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1704
1705 llvm::Function* runtime_func =
1706 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1707
1708 EmitUpdateDexPC(dex_pc);
1709
1710 llvm::Value* type_object_addr =
1711 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1712
1713 EmitGuard_ExceptionLandingPad(dex_pc);
1714
1715 return type_object_addr;
1716
1717 } else {
1718 // Try to load the class (type) object from the test cache.
1719 llvm::Value* type_field_addr =
1720 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1721
TDYa127ce4cc0d2012-11-18 16:59:53 -08001722 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001723
Ian Rogers89756f22013-03-04 16:40:02 -08001724 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001725 return type_object_addr;
1726 }
1727
1728 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1729
1730 // Test whether class (type) object is in the dex cache or not
1731 llvm::Value* equal_null =
1732 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1733
1734 llvm::BasicBlock* block_cont =
1735 CreateBasicBlockWithDexPC(dex_pc, "cont");
1736
1737 llvm::BasicBlock* block_load_class =
1738 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1739
1740 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1741
1742 // Failback routine to load the class object
1743 irb_.SetInsertPoint(block_load_class);
1744
1745 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1746
1747 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1748
1749 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1750
1751 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1752
1753 EmitUpdateDexPC(dex_pc);
1754
1755 llvm::Value* loaded_type_object_addr =
1756 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1757
1758 EmitGuard_ExceptionLandingPad(dex_pc);
1759
1760 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1761
1762 irb_.CreateBr(block_cont);
1763
1764 // Now the class object must be loaded
1765 irb_.SetInsertPoint(block_cont);
1766
1767 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1768
1769 phi->addIncoming(type_object_addr, block_original);
1770 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1771
1772 return phi;
1773 }
1774}
1775
TDYa1275a26d442012-07-26 18:58:38 -07001776llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1777 uint32_t type_idx) {
1778 llvm::BasicBlock* block_load_static =
1779 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1780
1781 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1782
1783 // Load static storage from dex cache
1784 llvm::Value* storage_field_addr =
1785 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1786
TDYa127ce4cc0d2012-11-18 16:59:53 -08001787 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001788
1789 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1790
1791 // Test: Is the static storage of this class initialized?
1792 llvm::Value* equal_null =
1793 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1794
1795 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1796
1797 // Failback routine to load the class object
1798 irb_.SetInsertPoint(block_load_static);
1799
1800 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1801
1802 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1803
1804 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1805
1806 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1807
1808 EmitUpdateDexPC(dex_pc);
1809
1810 llvm::Value* loaded_storage_object_addr =
1811 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1812
1813 EmitGuard_ExceptionLandingPad(dex_pc);
1814
1815 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1816
1817 irb_.CreateBr(block_cont);
1818
1819 // Now the class object must be loaded
1820 irb_.SetInsertPoint(block_cont);
1821
1822 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1823
1824 phi->addIncoming(storage_object_addr, block_original);
1825 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1826
1827 return phi;
1828}
1829
1830llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1831 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001832 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1833 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1834
1835 int field_offset;
1836 int ssb_index;
1837 bool is_referrers_class;
1838 bool is_volatile;
1839
Ian Rogers1212a022013-03-04 10:48:41 -08001840 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001841 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001842 is_referrers_class, is_volatile, false);
1843
1844 llvm::Value* static_field_value;
1845
1846 if (!is_fast_path) {
1847 llvm::Function* runtime_func;
1848
1849 if (field_jty == kObject) {
1850 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1851 } else if (field_jty == kLong || field_jty == kDouble) {
1852 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1853 } else {
1854 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1855 }
1856
1857 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1858
1859 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1860
1861 EmitUpdateDexPC(dex_pc);
1862
1863 static_field_value =
1864 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1865
1866 EmitGuard_ExceptionLandingPad(dex_pc);
1867
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001868 if (field_jty == kFloat || field_jty == kDouble) {
1869 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1870 }
TDYa1275a26d442012-07-26 18:58:38 -07001871 } else {
1872 DCHECK_GE(field_offset, 0);
1873
1874 llvm::Value* static_storage_addr = NULL;
1875
1876 if (is_referrers_class) {
1877 // Fast path, static storage base is this method's class
1878 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1879
1880 static_storage_addr =
1881 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001882 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001883 irb_.getJObjectTy(),
1884 kTBAAConstJObject);
1885 } else {
1886 // Medium path, static storage base in a different class which
1887 // requires checks that the other class is initialized
1888 DCHECK_GE(ssb_index, 0);
1889 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1890 }
1891
1892 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1893
1894 llvm::Value* static_field_addr =
1895 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001896 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001897
TDYa1275a26d442012-07-26 18:58:38 -07001898 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001899 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001900
1901 if (is_volatile) {
1902 irb_.CreateMemoryBarrier(art::kLoadLoad);
1903 }
TDYa1275a26d442012-07-26 18:58:38 -07001904 }
1905
TDYa1275a26d442012-07-26 18:58:38 -07001906 return static_field_value;
1907}
1908
1909void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1910 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001911 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1912 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1913 llvm::Value* new_value = call_inst.getArgOperand(1);
1914
1915 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001916 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001917 }
1918
1919 int field_offset;
1920 int ssb_index;
1921 bool is_referrers_class;
1922 bool is_volatile;
1923
Ian Rogers1212a022013-03-04 10:48:41 -08001924 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001925 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001926 is_referrers_class, is_volatile, true);
1927
1928 if (!is_fast_path) {
1929 llvm::Function* runtime_func;
1930
1931 if (field_jty == kObject) {
1932 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1933 } else if (field_jty == kLong || field_jty == kDouble) {
1934 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1935 } else {
1936 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1937 }
1938
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001939 if (field_jty == kFloat) {
1940 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1941 } else if (field_jty == kDouble) {
1942 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1943 }
1944
TDYa1275a26d442012-07-26 18:58:38 -07001945 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1946
1947 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1948
1949 EmitUpdateDexPC(dex_pc);
1950
1951 irb_.CreateCall3(runtime_func, field_idx_value,
1952 method_object_addr, new_value);
1953
1954 EmitGuard_ExceptionLandingPad(dex_pc);
1955
1956 } else {
1957 DCHECK_GE(field_offset, 0);
1958
1959 llvm::Value* static_storage_addr = NULL;
1960
1961 if (is_referrers_class) {
1962 // Fast path, static storage base is this method's class
1963 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1964
1965 static_storage_addr =
1966 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001967 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001968 irb_.getJObjectTy(),
1969 kTBAAConstJObject);
1970 } else {
1971 // Medium path, static storage base in a different class which
1972 // requires checks that the other class is initialized
1973 DCHECK_GE(ssb_index, 0);
1974 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1975 }
1976
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001977 if (is_volatile) {
1978 irb_.CreateMemoryBarrier(art::kStoreStore);
1979 }
1980
TDYa1275a26d442012-07-26 18:58:38 -07001981 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1982
1983 llvm::Value* static_field_addr =
1984 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001985 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001986
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001987 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001988 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1989
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001990 if (is_volatile) {
1991 irb_.CreateMemoryBarrier(art::kStoreLoad);
1992 }
1993
TDYa1275a26d442012-07-26 18:58:38 -07001994 if (field_jty == kObject) { // If put an object, mark the GC card table.
1995 EmitMarkGCCard(new_value, static_storage_addr);
1996 }
1997 }
1998
1999 return;
2000}
2001
TDYa127f71bf5a2012-07-29 20:09:52 -07002002llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002003 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2004 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
2005
2006 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
2007
TDYa127ce4cc0d2012-11-18 16:59:53 -08002008 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002009
Ian Rogers89756f22013-03-04 16:40:02 -08002010 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
2011 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002012 llvm::BasicBlock* block_str_exist =
2013 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
2014
2015 llvm::BasicBlock* block_str_resolve =
2016 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
2017
2018 llvm::BasicBlock* block_cont =
2019 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
2020
2021 // Test: Is the string resolved and in the dex cache?
2022 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2023
2024 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2025
2026 // String is resolved, go to next basic block.
2027 irb_.SetInsertPoint(block_str_exist);
2028 irb_.CreateBr(block_cont);
2029
2030 // String is not resolved yet, resolve it now.
2031 irb_.SetInsertPoint(block_str_resolve);
2032
2033 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2034
2035 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2036
2037 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2038
2039 EmitUpdateDexPC(dex_pc);
2040
2041 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2042 string_idx_value);
2043
2044 EmitGuard_ExceptionLandingPad(dex_pc);
2045
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002046 irb_.CreateBr(block_cont);
2047
2048
TDYa127f71bf5a2012-07-29 20:09:52 -07002049 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2050
2051 irb_.SetInsertPoint(block_cont);
2052
2053 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2054
2055 phi->addIncoming(string_addr, block_str_exist);
2056 phi->addIncoming(result, block_pre_cont);
2057
2058 string_addr = phi;
2059 }
2060
2061 return string_addr;
2062}
2063
2064llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002065 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2066 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2067
2068 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2069
2070 return type_object_addr;
2071}
2072
2073void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002074 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2075 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002076 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002077
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002078 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002079
TDYa127ce4cc0d2012-11-18 16:59:53 -08002080 EmitUpdateDexPC(dex_pc);
2081
TDYa127f71bf5a2012-07-29 20:09:52 -07002082 irb_.Runtime().EmitLockObject(object_addr);
2083
2084 return;
2085}
2086
2087void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002088 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2089 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002090 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002091
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002092 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002093
2094 EmitUpdateDexPC(dex_pc);
2095
2096 irb_.Runtime().EmitUnlockObject(object_addr);
2097
2098 EmitGuard_ExceptionLandingPad(dex_pc);
2099
2100 return;
2101}
2102
2103void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002104 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2105 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2106 llvm::Value* object_addr = call_inst.getArgOperand(1);
2107
2108 llvm::BasicBlock* block_test_class =
2109 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2110
2111 llvm::BasicBlock* block_test_sub_class =
2112 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2113
2114 llvm::BasicBlock* block_cont =
2115 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2116
2117 // Test: Is the reference equal to null? Act as no-op when it is null.
2118 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2119
Ian Rogers8e696052013-03-04 09:00:40 -08002120 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002121
2122 // Test: Is the object instantiated from the given class?
2123 irb_.SetInsertPoint(block_test_class);
2124 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002125 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002126
2127 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2128
2129 llvm::Value* object_type_field_addr =
2130 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2131
2132 llvm::Value* object_type_object_addr =
2133 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2134
2135 llvm::Value* equal_class =
2136 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2137
Ian Rogers8e696052013-03-04 09:00:40 -08002138 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002139
2140 // Test: Is the object instantiated from the subclass of the given class?
2141 irb_.SetInsertPoint(block_test_sub_class);
2142
2143 EmitUpdateDexPC(dex_pc);
2144
2145 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2146 type_object_addr, object_type_object_addr);
2147
2148 EmitGuard_ExceptionLandingPad(dex_pc);
2149
2150 irb_.CreateBr(block_cont);
2151
2152 irb_.SetInsertPoint(block_cont);
2153
2154 return;
2155}
2156
2157llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002158 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2159 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2160 llvm::Value* object_addr = call_inst.getArgOperand(1);
2161
2162 llvm::BasicBlock* block_nullp =
2163 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2164
2165 llvm::BasicBlock* block_test_class =
2166 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2167
2168 llvm::BasicBlock* block_class_equals =
2169 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2170
2171 llvm::BasicBlock* block_test_sub_class =
2172 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2173
2174 llvm::BasicBlock* block_cont =
2175 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2176
2177 // Overview of the following code :
2178 // We check for null, if so, then false, otherwise check for class == . If so
2179 // then true, otherwise do callout slowpath.
2180 //
2181 // Test: Is the reference equal to null? Set 0 when it is null.
2182 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2183
Ian Rogers8e696052013-03-04 09:00:40 -08002184 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002185
2186 irb_.SetInsertPoint(block_nullp);
2187 irb_.CreateBr(block_cont);
2188
2189 // Test: Is the object instantiated from the given class?
2190 irb_.SetInsertPoint(block_test_class);
2191 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002192 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002193
2194 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2195
2196 llvm::Value* object_type_field_addr =
2197 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2198
2199 llvm::Value* object_type_object_addr =
2200 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2201
2202 llvm::Value* equal_class =
2203 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2204
Ian Rogers8e696052013-03-04 09:00:40 -08002205 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002206
2207 irb_.SetInsertPoint(block_class_equals);
2208 irb_.CreateBr(block_cont);
2209
2210 // Test: Is the object instantiated from the subclass of the given class?
2211 irb_.SetInsertPoint(block_test_sub_class);
2212 llvm::Value* result =
2213 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2214 type_object_addr, object_type_object_addr);
2215 irb_.CreateBr(block_cont);
2216
2217 irb_.SetInsertPoint(block_cont);
2218
2219 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2220
2221 phi->addIncoming(irb_.getJInt(0), block_nullp);
2222 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2223 phi->addIncoming(result, block_test_sub_class);
2224
2225 return phi;
2226}
2227
2228llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002229 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2230 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2231
2232 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002233 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2234 *dex_compilation_unit_->GetDexFile(),
2235 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002236 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2237 } else {
2238 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2239 }
2240
2241 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2242
2243 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2244
2245 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2246
2247 EmitUpdateDexPC(dex_pc);
2248
2249 llvm::Value* object_addr =
2250 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2251
2252 EmitGuard_ExceptionLandingPad(dex_pc);
2253
2254 return object_addr;
2255}
2256
2257llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002258 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2259 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002260
2261 if (!is_static) {
2262 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002263 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2264 llvm::Value* this_addr = call_inst.getArgOperand(3);
2265 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2266
2267 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002268 }
2269
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002270 llvm::Value* result = NULL;
2271 if (EmitIntrinsic(call_inst, &result)) {
2272 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002273 }
2274
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002275 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002276}
2277
2278llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002279 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2280 // Get the array object address
2281 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002282 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002283
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002284 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002285
2286 // Get the array length and store it to the register
2287 return EmitLoadArrayLength(array_addr);
2288}
2289
2290llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002291 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2292 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2293 llvm::Value* length = call_inst.getArgOperand(1);
2294
2295 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2296}
2297
2298llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002299 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2300 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2301 uint32_t length = call_inst.getNumArgOperands() - 3;
2302
2303 llvm::Value* object_addr =
2304 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2305
2306 if (length > 0) {
2307 // Check for the element type
2308 uint32_t type_desc_len = 0;
2309 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002310 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002311
2312 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2313 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2314 bool is_elem_int_ty = (type_desc[1] == 'I');
2315
2316 uint32_t alignment;
2317 llvm::Constant* elem_size;
2318 llvm::PointerType* field_type;
2319
2320 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2321 // as the element, thus we are only checking 2 cases: primitive int and
2322 // non-primitive type.
2323 if (is_elem_int_ty) {
2324 alignment = sizeof(int32_t);
2325 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2326 field_type = irb_.getJIntTy()->getPointerTo();
2327 } else {
2328 alignment = irb_.getSizeOfPtrEquivInt();
2329 elem_size = irb_.getSizeOfPtrEquivIntValue();
2330 field_type = irb_.getJObjectTy()->getPointerTo();
2331 }
2332
2333 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002334 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002335
2336 llvm::Value* data_field_addr =
2337 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2338
2339 // TODO: Tune this code. Currently we are generating one instruction for
2340 // one element which may be very space consuming. Maybe changing to use
2341 // memcpy may help; however, since we can't guarantee that the alloca of
2342 // dalvik register are continuous, we can't perform such optimization yet.
2343 for (uint32_t i = 0; i < length; ++i) {
2344 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2345
2346 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2347
2348 data_field_addr =
2349 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2350 }
2351 }
2352
2353 return object_addr;
2354}
2355
2356void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002357 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2358 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2359 LV2SInt(call_inst.getArgOperand(0));
2360 llvm::Value* array_addr = call_inst.getArgOperand(1);
2361
TDYa127920be7c2012-09-10 17:13:22 -07002362 const art::Instruction::ArrayDataPayload* payload =
2363 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002364 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002365
2366 if (payload->element_count == 0) {
2367 // When the number of the elements in the payload is zero, we don't have
2368 // to copy any numbers. However, we should check whether the array object
2369 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002370 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002371 } else {
2372 // To save the code size, we are going to call the runtime function to
2373 // copy the content from DexFile.
2374
2375 // NOTE: We will check for the NullPointerException in the runtime.
2376
2377 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2378
2379 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2380
2381 EmitUpdateDexPC(dex_pc);
2382
2383 irb_.CreateCall4(runtime_func,
2384 method_object_addr, irb_.getInt32(dex_pc),
2385 array_addr, irb_.getInt32(payload_offset));
2386
2387 EmitGuard_ExceptionLandingPad(dex_pc);
2388 }
2389
2390 return;
2391}
2392
2393llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2394 llvm::Value* array_length_value,
2395 uint32_t type_idx,
2396 bool is_filled_new_array) {
2397 llvm::Function* runtime_func;
2398
2399 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002400 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2401 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002402
2403
2404 if (is_filled_new_array) {
2405 runtime_func = skip_access_check ?
2406 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2407 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2408 } else {
2409 runtime_func = skip_access_check ?
2410 irb_.GetRuntime(runtime_support::AllocArray) :
2411 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2412 }
2413
2414 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2415
2416 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2417
2418 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2419
2420 EmitUpdateDexPC(dex_pc);
2421
2422 llvm::Value* object_addr =
2423 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2424 array_length_value, thread_object_addr);
2425
2426 EmitGuard_ExceptionLandingPad(dex_pc);
2427
2428 return object_addr;
2429}
2430
2431llvm::Value* GBCExpanderPass::
2432EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002433 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002434 llvm::Value* this_addr,
2435 uint32_t dex_pc,
2436 bool is_fast_path) {
2437
2438 llvm::Function* runtime_func = NULL;
2439
2440 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002441 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002442 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2443 break;
2444
TDYa127920be7c2012-09-10 17:13:22 -07002445 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002446 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2447 break;
2448
TDYa127920be7c2012-09-10 17:13:22 -07002449 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002450 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2451 break;
2452
TDYa127920be7c2012-09-10 17:13:22 -07002453 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002454 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2455 break;
2456
TDYa127920be7c2012-09-10 17:13:22 -07002457 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002458 if (is_fast_path) {
2459 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2460 } else {
2461 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2462 }
2463 break;
2464 }
2465
2466 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2467
2468 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002469 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002470 this_addr = irb_.getJNull();
2471 }
2472
2473 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2474
2475 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2476
2477 EmitUpdateDexPC(dex_pc);
2478
2479 llvm::Value* callee_method_object_addr =
2480 irb_.CreateCall4(runtime_func,
2481 callee_method_idx_value,
2482 this_addr,
2483 caller_method_object_addr,
2484 thread_object_addr);
2485
2486 EmitGuard_ExceptionLandingPad(dex_pc);
2487
2488 return callee_method_object_addr;
2489}
2490
TDYa1275e869b62012-07-25 00:45:39 -07002491void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2492 // Using runtime support, let the target can override by InlineAssembly.
2493 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2494}
2495
2496void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002497 if (shadow_frame_ == NULL) {
2498 return;
2499 }
TDYa1275e869b62012-07-25 00:45:39 -07002500 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002501 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002502 irb_.getInt32(dex_pc),
2503 kTBAAShadowFrame);
2504}
2505
2506void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2507 llvm::Value* denominator,
2508 JType op_jty) {
2509 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2510
2511 llvm::Constant* zero = irb_.getJZero(op_jty);
2512
2513 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2514
2515 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2516
2517 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2518
2519 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2520
2521 irb_.SetInsertPoint(block_exception);
2522 EmitUpdateDexPC(dex_pc);
2523 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2524 EmitBranchExceptionLandingPad(dex_pc);
2525
2526 irb_.SetInsertPoint(block_continue);
2527}
2528
2529void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002530 llvm::Value* object,
2531 int opt_flags) {
2532 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2533 if (ignore_null_check) {
2534 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2535 if (lpad) {
2536 // There is at least one catch: create a "fake" conditional branch to
2537 // keep the exception edge to the catch block.
2538 landing_pad_phi_mapping_[lpad].push_back(
2539 std::make_pair(current_bb_->getUniquePredecessor(),
2540 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002541
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002542 llvm::BasicBlock* block_continue =
2543 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002544
Ian Rogers8e696052013-03-04 09:00:40 -08002545 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002546
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002547 irb_.SetInsertPoint(block_continue);
2548 }
2549 } else {
2550 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002551
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002552 llvm::BasicBlock* block_exception =
2553 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002554
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002555 llvm::BasicBlock* block_continue =
2556 CreateBasicBlockWithDexPC(dex_pc, "cont");
2557
2558 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2559
2560 irb_.SetInsertPoint(block_exception);
2561 EmitUpdateDexPC(dex_pc);
2562 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2563 irb_.getInt32(dex_pc));
2564 EmitBranchExceptionLandingPad(dex_pc);
2565
2566 irb_.SetInsertPoint(block_continue);
2567 }
TDYa1275e869b62012-07-25 00:45:39 -07002568}
2569
2570void
2571GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2572 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002573 llvm::Value* index,
2574 int opt_flags) {
2575 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2576 if (ignore_range_check) {
2577 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2578 if (lpad) {
2579 // There is at least one catch: create a "fake" conditional branch to
2580 // keep the exception edge to the catch block.
2581 landing_pad_phi_mapping_[lpad].push_back(
2582 std::make_pair(current_bb_->getUniquePredecessor(),
2583 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002584
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002585 llvm::BasicBlock* block_continue =
2586 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002587
Ian Rogers8e696052013-03-04 09:00:40 -08002588 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002589
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002590 irb_.SetInsertPoint(block_continue);
2591 }
2592 } else {
2593 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002594
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002595 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002596
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002597 llvm::BasicBlock* block_exception =
2598 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002599
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002600 llvm::BasicBlock* block_continue =
2601 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002602
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002603 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2604
2605 irb_.SetInsertPoint(block_exception);
2606
2607 EmitUpdateDexPC(dex_pc);
2608 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2609 EmitBranchExceptionLandingPad(dex_pc);
2610
2611 irb_.SetInsertPoint(block_continue);
2612 }
TDYa1275e869b62012-07-25 00:45:39 -07002613}
2614
Ian Rogers8e696052013-03-04 09:00:40 -08002615llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002616 bool is_static) {
2617 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002618 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002619 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002620
2621 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002622 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002623 CHECK_GE(shorty_size, 1u);
2624
TDYa1275e869b62012-07-25 00:45:39 -07002625 // Get argument type
2626 std::vector<llvm::Type*> args_type;
2627
2628 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2629
2630 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002631 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002632 }
2633
2634 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002635 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002636 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002637 }
2638
2639 return llvm::FunctionType::get(ret_type, args_type, false);
2640}
2641
2642
2643llvm::BasicBlock* GBCExpanderPass::
2644CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2645 std::string name;
2646
2647#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002648 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002649#endif
2650
2651 return llvm::BasicBlock::Create(context_, name, func_);
2652}
2653
2654llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002655 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002656 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002657 return basic_blocks_[dex_pc];
2658}
2659
2660int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2661 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002662 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002663
2664 while (min <= max) {
2665 int32_t mid = min + (max - min) / 2;
2666
Ian Rogers89756f22013-03-04 16:40:02 -08002667 const art::DexFile::TryItem* ti =
2668 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002669 uint32_t start = ti->start_addr_;
2670 uint32_t end = start + ti->insn_count_;
2671
2672 if (dex_pc < start) {
2673 max = mid - 1;
2674 } else if (dex_pc >= end) {
2675 min = mid + 1;
2676 } else {
2677 return mid; // found
2678 }
2679 }
2680
2681 return -1; // not found
2682}
2683
2684llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2685 // Find the try item for this address in this method
2686 int32_t ti_offset = GetTryItemOffset(dex_pc);
2687
2688 if (ti_offset == -1) {
2689 return NULL; // No landing pad is available for this address.
2690 }
2691
2692 // Check for the existing landing pad basic block
2693 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2694 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2695
2696 if (block_lpad) {
2697 // We have generated landing pad for this try item already. Return the
2698 // same basic block.
2699 return block_lpad;
2700 }
2701
2702 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002703 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002704 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002705
2706 std::string lpadname;
2707
2708#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002709 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002710#endif
2711
2712 // Create landing pad basic block
2713 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2714
2715 // Change IRBuilder insert point
2716 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2717 irb_.SetInsertPoint(block_lpad);
2718
2719 // Find catch block with matching type
2720 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2721
2722 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2723
2724 llvm::Value* catch_handler_index_value =
2725 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2726 method_object_addr, ti_offset_value);
2727
2728 // Switch instruction (Go to unwind basic block by default)
2729 llvm::SwitchInst* sw =
2730 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2731
2732 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002733 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002734
2735 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2736 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2737 }
2738
2739 // Restore the orignal insert point for IRBuilder
2740 irb_.restoreIP(irb_ip_original);
2741
2742 // Cache this landing pad
2743 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2744 basic_block_landing_pads_[ti_offset] = block_lpad;
2745
2746 return block_lpad;
2747}
2748
2749llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2750 // Check the existing unwinding baisc block block
2751 if (basic_block_unwind_ != NULL) {
2752 return basic_block_unwind_;
2753 }
2754
2755 // Create new basic block for unwinding
2756 basic_block_unwind_ =
2757 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2758
2759 // Change IRBuilder insert point
2760 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2761 irb_.SetInsertPoint(basic_block_unwind_);
2762
2763 // Pop the shadow frame
2764 Expand_PopShadowFrame();
2765
2766 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002767 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002768 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002769 if (ret_shorty == 'V') {
2770 irb_.CreateRetVoid();
2771 } else {
2772 irb_.CreateRet(irb_.getJZero(ret_shorty));
2773 }
2774
2775 // Restore the orignal insert point for IRBuilder
2776 irb_.restoreIP(irb_ip_original);
2777
2778 return basic_block_unwind_;
2779}
2780
2781void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2782 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002783 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002784 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002785 irb_.CreateBr(lpad);
2786 } else {
2787 irb_.CreateBr(GetUnwindBasicBlock());
2788 }
2789}
2790
2791void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002792 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2793
TDYa1275e869b62012-07-25 00:45:39 -07002794 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2795
2796 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002797 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002798 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002799 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002800 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002801 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002802 }
2803
2804 irb_.SetInsertPoint(block_cont);
2805}
2806
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002807llvm::Value*
2808GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2809 llvm::CallInst& call_inst) {
2810 switch (intr_id) {
2811 //==- Thread -----------------------------------------------------------==//
2812 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002813 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002814 }
Logan Chien75e4b602012-07-23 14:24:12 -07002815 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002816 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002817 return NULL;
2818 }
2819 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002820 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002821 return NULL;
2822 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002823 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002824 Expand_MarkGCCard(call_inst);
2825 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002826 }
Logan Chien75e4b602012-07-23 14:24:12 -07002827
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002828 //==- Exception --------------------------------------------------------==//
2829 case IntrinsicHelper::ThrowException: {
2830 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2831 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002832 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002833 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2834
2835 EmitUpdateDexPC(dex_pc);
2836
2837 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2838 call_inst.getArgOperand(0));
2839
2840 EmitGuard_ExceptionLandingPad(dex_pc);
2841 return NULL;
2842 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002843 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002844 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002845 }
2846 case IntrinsicHelper::IsExceptionPending: {
2847 return irb_.Runtime().EmitIsExceptionPending();
2848 }
2849 case IntrinsicHelper::FindCatchBlock: {
2850 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2851 }
2852 case IntrinsicHelper::ThrowDivZeroException: {
2853 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2854 }
2855 case IntrinsicHelper::ThrowNullPointerException: {
2856 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2857 }
2858 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2859 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2860 }
Logan Chien75e4b602012-07-23 14:24:12 -07002861
2862 //==- Const String -----------------------------------------------------==//
2863 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002864 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002865 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002866 case IntrinsicHelper::LoadStringFromDexCache: {
2867 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2868 }
2869 case IntrinsicHelper::ResolveString: {
2870 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2871 }
Logan Chien75e4b602012-07-23 14:24:12 -07002872
2873 //==- Const Class ------------------------------------------------------==//
2874 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002875 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002876 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002877 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2878 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2879 }
2880 case IntrinsicHelper::LoadTypeFromDexCache: {
2881 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2882 }
2883 case IntrinsicHelper::InitializeType: {
2884 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2885 }
Logan Chien75e4b602012-07-23 14:24:12 -07002886
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002887 //==- Lock -------------------------------------------------------------==//
2888 case IntrinsicHelper::LockObject: {
2889 Expand_LockObject(call_inst.getArgOperand(0));
2890 return NULL;
2891 }
2892 case IntrinsicHelper::UnlockObject: {
2893 Expand_UnlockObject(call_inst.getArgOperand(0));
2894 return NULL;
2895 }
Logan Chien75e4b602012-07-23 14:24:12 -07002896
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002897 //==- Cast -------------------------------------------------------------==//
2898 case IntrinsicHelper::CheckCast: {
2899 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2900 }
Logan Chien75e4b602012-07-23 14:24:12 -07002901 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002902 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002903 return NULL;
2904 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002905 case IntrinsicHelper::IsAssignable: {
2906 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2907 }
Logan Chien75e4b602012-07-23 14:24:12 -07002908
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002909 //==- Alloc ------------------------------------------------------------==//
2910 case IntrinsicHelper::AllocObject: {
2911 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2912 }
2913 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2914 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2915 }
Logan Chien75e4b602012-07-23 14:24:12 -07002916
2917 //==- Instance ---------------------------------------------------------==//
2918 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002919 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002920 }
2921 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002922 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002923 }
2924
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002925 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002926 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002927 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002928 }
2929 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002930 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002931 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002932 case IntrinsicHelper::ArrayLength: {
2933 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2934 }
2935 case IntrinsicHelper::AllocArray: {
2936 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2937 }
2938 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2939 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2940 call_inst);
2941 }
2942 case IntrinsicHelper::CheckAndAllocArray: {
2943 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2944 }
2945 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2946 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2947 call_inst);
2948 }
2949 case IntrinsicHelper::ArrayGet: {
2950 return Expand_ArrayGet(call_inst.getArgOperand(0),
2951 call_inst.getArgOperand(1),
2952 kInt);
2953 }
2954 case IntrinsicHelper::ArrayGetWide: {
2955 return Expand_ArrayGet(call_inst.getArgOperand(0),
2956 call_inst.getArgOperand(1),
2957 kLong);
2958 }
2959 case IntrinsicHelper::ArrayGetObject: {
2960 return Expand_ArrayGet(call_inst.getArgOperand(0),
2961 call_inst.getArgOperand(1),
2962 kObject);
2963 }
2964 case IntrinsicHelper::ArrayGetBoolean: {
2965 return Expand_ArrayGet(call_inst.getArgOperand(0),
2966 call_inst.getArgOperand(1),
2967 kBoolean);
2968 }
2969 case IntrinsicHelper::ArrayGetByte: {
2970 return Expand_ArrayGet(call_inst.getArgOperand(0),
2971 call_inst.getArgOperand(1),
2972 kByte);
2973 }
2974 case IntrinsicHelper::ArrayGetChar: {
2975 return Expand_ArrayGet(call_inst.getArgOperand(0),
2976 call_inst.getArgOperand(1),
2977 kChar);
2978 }
2979 case IntrinsicHelper::ArrayGetShort: {
2980 return Expand_ArrayGet(call_inst.getArgOperand(0),
2981 call_inst.getArgOperand(1),
2982 kShort);
2983 }
2984 case IntrinsicHelper::ArrayPut: {
2985 Expand_ArrayPut(call_inst.getArgOperand(0),
2986 call_inst.getArgOperand(1),
2987 call_inst.getArgOperand(2),
2988 kInt);
2989 return NULL;
2990 }
2991 case IntrinsicHelper::ArrayPutWide: {
2992 Expand_ArrayPut(call_inst.getArgOperand(0),
2993 call_inst.getArgOperand(1),
2994 call_inst.getArgOperand(2),
2995 kLong);
2996 return NULL;
2997 }
2998 case IntrinsicHelper::ArrayPutObject: {
2999 Expand_ArrayPut(call_inst.getArgOperand(0),
3000 call_inst.getArgOperand(1),
3001 call_inst.getArgOperand(2),
3002 kObject);
3003 return NULL;
3004 }
3005 case IntrinsicHelper::ArrayPutBoolean: {
3006 Expand_ArrayPut(call_inst.getArgOperand(0),
3007 call_inst.getArgOperand(1),
3008 call_inst.getArgOperand(2),
3009 kBoolean);
3010 return NULL;
3011 }
3012 case IntrinsicHelper::ArrayPutByte: {
3013 Expand_ArrayPut(call_inst.getArgOperand(0),
3014 call_inst.getArgOperand(1),
3015 call_inst.getArgOperand(2),
3016 kByte);
3017 return NULL;
3018 }
3019 case IntrinsicHelper::ArrayPutChar: {
3020 Expand_ArrayPut(call_inst.getArgOperand(0),
3021 call_inst.getArgOperand(1),
3022 call_inst.getArgOperand(2),
3023 kChar);
3024 return NULL;
3025 }
3026 case IntrinsicHelper::ArrayPutShort: {
3027 Expand_ArrayPut(call_inst.getArgOperand(0),
3028 call_inst.getArgOperand(1),
3029 call_inst.getArgOperand(2),
3030 kShort);
3031 return NULL;
3032 }
3033 case IntrinsicHelper::CheckPutArrayElement: {
3034 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3035 }
3036 case IntrinsicHelper::FilledNewArray: {
3037 Expand_FilledNewArray(call_inst);
3038 return NULL;
3039 }
3040 case IntrinsicHelper::FillArrayData: {
3041 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3042 }
Logan Chien75e4b602012-07-23 14:24:12 -07003043 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003044 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003045 return NULL;
3046 }
3047 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003048 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003049 }
3050
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003051 //==- Instance Field ---------------------------------------------------==//
3052 case IntrinsicHelper::InstanceFieldGet:
3053 case IntrinsicHelper::InstanceFieldGetBoolean:
3054 case IntrinsicHelper::InstanceFieldGetByte:
3055 case IntrinsicHelper::InstanceFieldGetChar:
3056 case IntrinsicHelper::InstanceFieldGetShort: {
3057 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3058 }
3059 case IntrinsicHelper::InstanceFieldGetWide: {
3060 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3061 }
3062 case IntrinsicHelper::InstanceFieldGetObject: {
3063 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3064 }
3065 case IntrinsicHelper::InstanceFieldGetFast: {
3066 return Expand_IGetFast(call_inst.getArgOperand(0),
3067 call_inst.getArgOperand(1),
3068 call_inst.getArgOperand(2),
3069 kInt);
3070 }
3071 case IntrinsicHelper::InstanceFieldGetWideFast: {
3072 return Expand_IGetFast(call_inst.getArgOperand(0),
3073 call_inst.getArgOperand(1),
3074 call_inst.getArgOperand(2),
3075 kLong);
3076 }
3077 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3078 return Expand_IGetFast(call_inst.getArgOperand(0),
3079 call_inst.getArgOperand(1),
3080 call_inst.getArgOperand(2),
3081 kObject);
3082 }
3083 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3084 return Expand_IGetFast(call_inst.getArgOperand(0),
3085 call_inst.getArgOperand(1),
3086 call_inst.getArgOperand(2),
3087 kBoolean);
3088 }
3089 case IntrinsicHelper::InstanceFieldGetByteFast: {
3090 return Expand_IGetFast(call_inst.getArgOperand(0),
3091 call_inst.getArgOperand(1),
3092 call_inst.getArgOperand(2),
3093 kByte);
3094 }
3095 case IntrinsicHelper::InstanceFieldGetCharFast: {
3096 return Expand_IGetFast(call_inst.getArgOperand(0),
3097 call_inst.getArgOperand(1),
3098 call_inst.getArgOperand(2),
3099 kChar);
3100 }
3101 case IntrinsicHelper::InstanceFieldGetShortFast: {
3102 return Expand_IGetFast(call_inst.getArgOperand(0),
3103 call_inst.getArgOperand(1),
3104 call_inst.getArgOperand(2),
3105 kShort);
3106 }
3107 case IntrinsicHelper::InstanceFieldPut:
3108 case IntrinsicHelper::InstanceFieldPutBoolean:
3109 case IntrinsicHelper::InstanceFieldPutByte:
3110 case IntrinsicHelper::InstanceFieldPutChar:
3111 case IntrinsicHelper::InstanceFieldPutShort: {
3112 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3113 }
3114 case IntrinsicHelper::InstanceFieldPutWide: {
3115 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3116 }
3117 case IntrinsicHelper::InstanceFieldPutObject: {
3118 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3119 }
3120 case IntrinsicHelper::InstanceFieldPutFast: {
3121 Expand_IPutFast(call_inst.getArgOperand(0),
3122 call_inst.getArgOperand(1),
3123 call_inst.getArgOperand(2),
3124 call_inst.getArgOperand(3),
3125 kInt);
3126 return NULL;
3127 }
3128 case IntrinsicHelper::InstanceFieldPutWideFast: {
3129 Expand_IPutFast(call_inst.getArgOperand(0),
3130 call_inst.getArgOperand(1),
3131 call_inst.getArgOperand(2),
3132 call_inst.getArgOperand(3),
3133 kLong);
3134 return NULL;
3135 }
3136 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3137 Expand_IPutFast(call_inst.getArgOperand(0),
3138 call_inst.getArgOperand(1),
3139 call_inst.getArgOperand(2),
3140 call_inst.getArgOperand(3),
3141 kObject);
3142 return NULL;
3143 }
3144 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3145 Expand_IPutFast(call_inst.getArgOperand(0),
3146 call_inst.getArgOperand(1),
3147 call_inst.getArgOperand(2),
3148 call_inst.getArgOperand(3),
3149 kBoolean);
3150 return NULL;
3151 }
3152 case IntrinsicHelper::InstanceFieldPutByteFast: {
3153 Expand_IPutFast(call_inst.getArgOperand(0),
3154 call_inst.getArgOperand(1),
3155 call_inst.getArgOperand(2),
3156 call_inst.getArgOperand(3),
3157 kByte);
3158 return NULL;
3159 }
3160 case IntrinsicHelper::InstanceFieldPutCharFast: {
3161 Expand_IPutFast(call_inst.getArgOperand(0),
3162 call_inst.getArgOperand(1),
3163 call_inst.getArgOperand(2),
3164 call_inst.getArgOperand(3),
3165 kChar);
3166 return NULL;
3167 }
3168 case IntrinsicHelper::InstanceFieldPutShortFast: {
3169 Expand_IPutFast(call_inst.getArgOperand(0),
3170 call_inst.getArgOperand(1),
3171 call_inst.getArgOperand(2),
3172 call_inst.getArgOperand(3),
3173 kShort);
3174 return NULL;
3175 }
Logan Chien75e4b602012-07-23 14:24:12 -07003176
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003177 //==- Static Field -----------------------------------------------------==//
3178 case IntrinsicHelper::StaticFieldGet:
3179 case IntrinsicHelper::StaticFieldGetBoolean:
3180 case IntrinsicHelper::StaticFieldGetByte:
3181 case IntrinsicHelper::StaticFieldGetChar:
3182 case IntrinsicHelper::StaticFieldGetShort: {
3183 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3184 }
3185 case IntrinsicHelper::StaticFieldGetWide: {
3186 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3187 }
3188 case IntrinsicHelper::StaticFieldGetObject: {
3189 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3190 }
3191 case IntrinsicHelper::StaticFieldGetFast: {
3192 return Expand_SGetFast(call_inst.getArgOperand(0),
3193 call_inst.getArgOperand(1),
3194 call_inst.getArgOperand(2),
3195 kInt);
3196 }
3197 case IntrinsicHelper::StaticFieldGetWideFast: {
3198 return Expand_SGetFast(call_inst.getArgOperand(0),
3199 call_inst.getArgOperand(1),
3200 call_inst.getArgOperand(2),
3201 kLong);
3202 }
3203 case IntrinsicHelper::StaticFieldGetObjectFast: {
3204 return Expand_SGetFast(call_inst.getArgOperand(0),
3205 call_inst.getArgOperand(1),
3206 call_inst.getArgOperand(2),
3207 kObject);
3208 }
3209 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3210 return Expand_SGetFast(call_inst.getArgOperand(0),
3211 call_inst.getArgOperand(1),
3212 call_inst.getArgOperand(2),
3213 kBoolean);
3214 }
3215 case IntrinsicHelper::StaticFieldGetByteFast: {
3216 return Expand_SGetFast(call_inst.getArgOperand(0),
3217 call_inst.getArgOperand(1),
3218 call_inst.getArgOperand(2),
3219 kByte);
3220 }
3221 case IntrinsicHelper::StaticFieldGetCharFast: {
3222 return Expand_SGetFast(call_inst.getArgOperand(0),
3223 call_inst.getArgOperand(1),
3224 call_inst.getArgOperand(2),
3225 kChar);
3226 }
3227 case IntrinsicHelper::StaticFieldGetShortFast: {
3228 return Expand_SGetFast(call_inst.getArgOperand(0),
3229 call_inst.getArgOperand(1),
3230 call_inst.getArgOperand(2),
3231 kShort);
3232 }
3233 case IntrinsicHelper::StaticFieldPut:
3234 case IntrinsicHelper::StaticFieldPutBoolean:
3235 case IntrinsicHelper::StaticFieldPutByte:
3236 case IntrinsicHelper::StaticFieldPutChar:
3237 case IntrinsicHelper::StaticFieldPutShort: {
3238 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3239 }
3240 case IntrinsicHelper::StaticFieldPutWide: {
3241 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3242 }
3243 case IntrinsicHelper::StaticFieldPutObject: {
3244 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3245 }
3246 case IntrinsicHelper::StaticFieldPutFast: {
3247 Expand_SPutFast(call_inst.getArgOperand(0),
3248 call_inst.getArgOperand(1),
3249 call_inst.getArgOperand(2),
3250 call_inst.getArgOperand(3),
3251 kInt);
3252 return NULL;
3253 }
3254 case IntrinsicHelper::StaticFieldPutWideFast: {
3255 Expand_SPutFast(call_inst.getArgOperand(0),
3256 call_inst.getArgOperand(1),
3257 call_inst.getArgOperand(2),
3258 call_inst.getArgOperand(3),
3259 kLong);
3260 return NULL;
3261 }
3262 case IntrinsicHelper::StaticFieldPutObjectFast: {
3263 Expand_SPutFast(call_inst.getArgOperand(0),
3264 call_inst.getArgOperand(1),
3265 call_inst.getArgOperand(2),
3266 call_inst.getArgOperand(3),
3267 kObject);
3268 return NULL;
3269 }
3270 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3271 Expand_SPutFast(call_inst.getArgOperand(0),
3272 call_inst.getArgOperand(1),
3273 call_inst.getArgOperand(2),
3274 call_inst.getArgOperand(3),
3275 kBoolean);
3276 return NULL;
3277 }
3278 case IntrinsicHelper::StaticFieldPutByteFast: {
3279 Expand_SPutFast(call_inst.getArgOperand(0),
3280 call_inst.getArgOperand(1),
3281 call_inst.getArgOperand(2),
3282 call_inst.getArgOperand(3),
3283 kByte);
3284 return NULL;
3285 }
3286 case IntrinsicHelper::StaticFieldPutCharFast: {
3287 Expand_SPutFast(call_inst.getArgOperand(0),
3288 call_inst.getArgOperand(1),
3289 call_inst.getArgOperand(2),
3290 call_inst.getArgOperand(3),
3291 kChar);
3292 return NULL;
3293 }
3294 case IntrinsicHelper::StaticFieldPutShortFast: {
3295 Expand_SPutFast(call_inst.getArgOperand(0),
3296 call_inst.getArgOperand(1),
3297 call_inst.getArgOperand(2),
3298 call_inst.getArgOperand(3),
3299 kShort);
3300 return NULL;
3301 }
3302 case IntrinsicHelper::LoadDeclaringClassSSB: {
3303 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3304 }
3305 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3306 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3307 }
3308 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3309 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3310 }
Logan Chien75e4b602012-07-23 14:24:12 -07003311
3312 //==- High-level Array -------------------------------------------------==//
3313 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003314 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003315 }
3316 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003317 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003318 }
3319 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003320 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003321 }
3322 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003323 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003324 }
3325 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003326 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003327 }
3328 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003329 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003330 }
3331 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003332 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003333 }
3334 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003335 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003336 }
3337 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003338 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003339 }
3340 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003341 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003342 return NULL;
3343 }
3344 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003345 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003346 return NULL;
3347 }
3348 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003349 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003350 return NULL;
3351 }
3352 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003353 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003354 return NULL;
3355 }
3356 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003357 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003358 return NULL;
3359 }
3360 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003361 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003362 return NULL;
3363 }
3364 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003365 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003366 return NULL;
3367 }
3368 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003369 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003370 return NULL;
3371 }
3372 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003373 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003374 return NULL;
3375 }
3376
3377 //==- High-level Instance ----------------------------------------------==//
3378 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003379 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003380 }
3381 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003382 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003383 }
3384 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003385 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003386 }
3387 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003388 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003389 }
3390 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003391 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003392 }
3393 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003394 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003395 }
3396 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003397 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003398 }
3399 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003400 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003401 }
3402 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003403 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003404 }
3405 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003406 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003407 return NULL;
3408 }
3409 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003410 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003411 return NULL;
3412 }
3413 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003414 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003415 return NULL;
3416 }
3417 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003418 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003419 return NULL;
3420 }
3421 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003422 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003423 return NULL;
3424 }
3425 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003426 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003427 return NULL;
3428 }
3429 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003430 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003431 return NULL;
3432 }
3433 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003434 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003435 return NULL;
3436 }
3437 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003438 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003439 return NULL;
3440 }
3441
3442 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003443 case IntrinsicHelper::HLInvokeVoid:
3444 case IntrinsicHelper::HLInvokeObj:
3445 case IntrinsicHelper::HLInvokeInt:
3446 case IntrinsicHelper::HLInvokeFloat:
3447 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003448 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003449 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003450 }
3451
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003452 //==- Invoke -----------------------------------------------------------==//
3453 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3454 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3455 }
3456 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3457 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3458 }
3459 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3460 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3461 }
3462 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3463 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3464 }
3465 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3466 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3467 }
3468 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3469 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3470 }
3471 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3472 return Expand_GetVirtualCalleeMethodObjAddrFast(
3473 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3474 }
3475 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3476 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3477 }
3478 case IntrinsicHelper::InvokeRetVoid:
3479 case IntrinsicHelper::InvokeRetBoolean:
3480 case IntrinsicHelper::InvokeRetByte:
3481 case IntrinsicHelper::InvokeRetChar:
3482 case IntrinsicHelper::InvokeRetShort:
3483 case IntrinsicHelper::InvokeRetInt:
3484 case IntrinsicHelper::InvokeRetLong:
3485 case IntrinsicHelper::InvokeRetFloat:
3486 case IntrinsicHelper::InvokeRetDouble:
3487 case IntrinsicHelper::InvokeRetObject: {
3488 return Expand_Invoke(call_inst);
3489 }
Logan Chien75e4b602012-07-23 14:24:12 -07003490
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003491 //==- Math -------------------------------------------------------------==//
3492 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003493 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003494 }
3495 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003496 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003497 }
3498 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003499 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003500 }
3501 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003502 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003503 }
3504 case IntrinsicHelper::D2L: {
3505 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3506 }
3507 case IntrinsicHelper::D2I: {
3508 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3509 }
3510 case IntrinsicHelper::F2L: {
3511 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3512 }
3513 case IntrinsicHelper::F2I: {
3514 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3515 }
Logan Chien75e4b602012-07-23 14:24:12 -07003516
3517 //==- High-level Static ------------------------------------------------==//
3518 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003519 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003520 }
3521 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003522 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003523 }
3524 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003525 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003526 }
3527 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003528 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003529 }
3530 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003531 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003532 }
3533 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003534 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003535 }
3536 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003537 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003538 }
3539 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003540 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003541 }
3542 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003543 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003544 }
3545 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003546 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003547 return NULL;
3548 }
3549 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003550 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003551 return NULL;
3552 }
3553 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003554 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003555 return NULL;
3556 }
3557 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003558 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003559 return NULL;
3560 }
3561 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003562 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003563 return NULL;
3564 }
3565 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003566 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003567 return NULL;
3568 }
3569 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003570 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003571 return NULL;
3572 }
3573 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003574 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003575 return NULL;
3576 }
3577 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003578 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003579 return NULL;
3580 }
3581
3582 //==- High-level Monitor -----------------------------------------------==//
3583 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003584 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003585 return NULL;
3586 }
3587 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003588 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003589 return NULL;
3590 }
3591
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003592 //==- Shadow Frame -----------------------------------------------------==//
3593 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003594 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003595 return NULL;
3596 }
TDYa1278e950c12012-11-02 09:58:19 -07003597 case IntrinsicHelper::SetVReg: {
3598 Expand_SetVReg(call_inst.getArgOperand(0),
3599 call_inst.getArgOperand(1));
3600 return NULL;
3601 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003602 case IntrinsicHelper::PopShadowFrame: {
3603 Expand_PopShadowFrame();
3604 return NULL;
3605 }
3606 case IntrinsicHelper::UpdateDexPC: {
3607 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3608 return NULL;
3609 }
TDYa127a1b21852012-07-23 03:20:39 -07003610
Logan Chien75e4b602012-07-23 14:24:12 -07003611 //==- Comparison -------------------------------------------------------==//
3612 case IntrinsicHelper::CmplFloat:
3613 case IntrinsicHelper::CmplDouble: {
3614 return Expand_FPCompare(call_inst.getArgOperand(0),
3615 call_inst.getArgOperand(1),
3616 false);
3617 }
3618 case IntrinsicHelper::CmpgFloat:
3619 case IntrinsicHelper::CmpgDouble: {
3620 return Expand_FPCompare(call_inst.getArgOperand(0),
3621 call_inst.getArgOperand(1),
3622 true);
3623 }
3624 case IntrinsicHelper::CmpLong: {
3625 return Expand_LongCompare(call_inst.getArgOperand(0),
3626 call_inst.getArgOperand(1));
3627 }
TDYa127a1b21852012-07-23 03:20:39 -07003628
Logan Chien75e4b602012-07-23 14:24:12 -07003629 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003630 case IntrinsicHelper::ConstInt:
3631 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003632 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003633 }
TDYa127920be7c2012-09-10 17:13:22 -07003634 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003635 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3636 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003637 }
TDYa127920be7c2012-09-10 17:13:22 -07003638 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003639 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3640 irb_.getJDoubleTy());
3641 }
TDYa127920be7c2012-09-10 17:13:22 -07003642 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003643 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3644 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003645 }
3646
3647 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003648 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003649 // Nothing to be done, because MethodInfo carries optional hints that are
3650 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003651 return NULL;
3652 }
3653
3654 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003655 case IntrinsicHelper::CopyInt:
3656 case IntrinsicHelper::CopyFloat:
3657 case IntrinsicHelper::CopyLong:
3658 case IntrinsicHelper::CopyDouble:
3659 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003660 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003661 }
3662
3663 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003664 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003665 return Expand_IntegerShift(call_inst.getArgOperand(0),
3666 call_inst.getArgOperand(1),
3667 kIntegerSHL, kLong);
3668 }
TDYa127920be7c2012-09-10 17:13:22 -07003669 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003670 return Expand_IntegerShift(call_inst.getArgOperand(0),
3671 call_inst.getArgOperand(1),
3672 kIntegerSHR, kLong);
3673 }
TDYa127920be7c2012-09-10 17:13:22 -07003674 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003675 return Expand_IntegerShift(call_inst.getArgOperand(0),
3676 call_inst.getArgOperand(1),
3677 kIntegerUSHR, kLong);
3678 }
TDYa127920be7c2012-09-10 17:13:22 -07003679 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003680 return Expand_IntegerShift(call_inst.getArgOperand(0),
3681 call_inst.getArgOperand(1),
3682 kIntegerSHL, kInt);
3683 }
TDYa127920be7c2012-09-10 17:13:22 -07003684 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003685 return Expand_IntegerShift(call_inst.getArgOperand(0),
3686 call_inst.getArgOperand(1),
3687 kIntegerSHR, kInt);
3688 }
TDYa127920be7c2012-09-10 17:13:22 -07003689 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003690 return Expand_IntegerShift(call_inst.getArgOperand(0),
3691 call_inst.getArgOperand(1),
3692 kIntegerUSHR, kInt);
3693 }
3694
3695 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003696 case IntrinsicHelper::IntToChar: {
3697 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3698 irb_.getJIntTy());
3699 }
3700 case IntrinsicHelper::IntToShort: {
3701 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3702 irb_.getJIntTy());
3703 }
3704 case IntrinsicHelper::IntToByte: {
3705 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3706 irb_.getJIntTy());
3707 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003708
TDYa12787caa7e2012-08-25 23:23:27 -07003709 //==- Exception --------------------------------------------------------==//
3710 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003711 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003712 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3713 CHECK(si != NULL);
3714 irb_.CreateBr(si->getDefaultDest());
3715 si->eraseFromParent();
3716 return call_inst.getArgOperand(0);
3717 }
3718
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003719 //==- Constructor barrier-----------------------------------------------==//
3720 case IntrinsicHelper::ConstructorBarrier: {
3721 irb_.CreateMemoryBarrier(art::kStoreStore);
3722 return NULL;
3723 }
3724
Logan Chien75e4b602012-07-23 14:24:12 -07003725 //==- Unknown Cases ----------------------------------------------------==//
3726 case IntrinsicHelper::MaxIntrinsicId:
3727 case IntrinsicHelper::UnknownId:
3728 //default:
3729 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3730 // give some warning on unmatched cases.
3731 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003732 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003733 }
Logan Chien75e4b602012-07-23 14:24:12 -07003734 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003735 return NULL;
3736}
3737
3738} // anonymous namespace
3739
3740namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003741namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003742
Ian Rogers4c1c2832013-03-04 18:30:13 -08003743::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003744CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003745 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003746 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003747}
3748
Ian Rogers4c1c2832013-03-04 18:30:13 -08003749} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003750} // namespace art