blob: 9b71694a09bc38d8daa1640389602604a01d22a2 [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
buzbee395116c2013-02-27 14:30:25 -080029#include "compiler/dex/compiler_ir.h"
30#include "compiler/dex/quick/codegen.h"
TDYa127920be7c2012-09-10 17:13:22 -070031using art::kMIRIgnoreNullCheck;
32using art::kMIRIgnoreRangeCheck;
33
Shih-wei Liao21d28f52012-06-12 05:55:00 -070034#include <llvm/ADT/STLExtras.h>
35#include <llvm/Intrinsics.h>
Logan Chiend36a2ac2012-08-04 00:37:30 +080036#include <llvm/Metadata.h>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070037#include <llvm/Pass.h>
38#include <llvm/Support/CFG.h>
39#include <llvm/Support/InstIterator.h>
40
41#include <vector>
TDYa127aa558872012-08-16 05:11:07 -070042#include <map>
43#include <utility>
Shih-wei Liao21d28f52012-06-12 05:55:00 -070044
Ian Rogers4c1c2832013-03-04 18:30:13 -080045using namespace art::llvm;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070046
Ian Rogers4c1c2832013-03-04 18:30:13 -080047using art::llvm::IntrinsicHelper;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070048
Shih-wei Liaob2596522012-09-14 16:36:11 -070049namespace art {
buzbee26f10ee2012-12-21 11:16:29 -080050extern char RemapShorty(char shortyType);
Shih-wei Liaob2596522012-09-14 16:36:11 -070051};
52
Shih-wei Liao21d28f52012-06-12 05:55:00 -070053namespace {
54
55class GBCExpanderPass : public llvm::FunctionPass {
56 private:
57 const IntrinsicHelper& intrinsic_helper_;
58 IRBuilder& irb_;
59
60 llvm::LLVMContext& context_;
61 RuntimeSupportBuilder& rtb_;
62
63 private:
64 llvm::AllocaInst* shadow_frame_;
65 llvm::Value* old_shadow_frame_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -070066
67 private:
Ian Rogers1212a022013-03-04 10:48:41 -080068 art::CompilerDriver* const driver_;
TDYa1275e869b62012-07-25 00:45:39 -070069
Ian Rogers89756f22013-03-04 16:40:02 -080070 const art::DexCompilationUnit* const dex_compilation_unit_;
TDYa1275e869b62012-07-25 00:45:39 -070071
TDYa1275e869b62012-07-25 00:45:39 -070072 llvm::Function* func_;
73
74 std::vector<llvm::BasicBlock*> basic_blocks_;
75
76 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
TDYa12755e5e6c2012-09-11 15:14:42 -070077 llvm::BasicBlock* current_bb_;
TDYa127aa558872012-08-16 05:11:07 -070078 std::map<llvm::BasicBlock*, std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > >
79 landing_pad_phi_mapping_;
TDYa1275e869b62012-07-25 00:45:39 -070080 llvm::BasicBlock* basic_block_unwind_;
81
Logan Chien67645d82012-08-17 09:10:54 +080082 bool changed_;
83
TDYa1275e869b62012-07-25 00:45:39 -070084 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070085 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070086 // Constant for GBC expansion
87 //----------------------------------------------------------------------------
88 enum IntegerShiftKind {
89 kIntegerSHL,
90 kIntegerSHR,
91 kIntegerUSHR,
92 };
93
94 private:
95 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070096 // Helper function for GBC expansion
97 //----------------------------------------------------------------------------
98
Shih-wei Liao21d28f52012-06-12 05:55:00 -070099 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
100 llvm::CallInst& inst);
101
TDYa1275e869b62012-07-25 00:45:39 -0700102 uint64_t LV2UInt(llvm::Value* lv) {
103 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
104 }
105
106 int64_t LV2SInt(llvm::Value* lv) {
107 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
108 }
109
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700110 private:
111 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
112 // Refactor these utility functions from MethodCompiler to avoid forking.
113
Logan Chien67645d82012-08-17 09:10:54 +0800114 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
115
116 void RewriteFunction();
117
118 void RewriteBasicBlock(llvm::BasicBlock* original_block);
119
120 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
121 llvm::BasicBlock* new_basic_block);
122
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700123
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800124 // Sign or zero extend category 1 types < 32bits in size to 32bits.
125 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
126
127 // Truncate category 1 types from 32bits to the given JType size.
128 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
129
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700130 //----------------------------------------------------------------------------
131 // Dex cache code generation helper function
132 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700133 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700134
135 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
136
137 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
138
139 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
140
141 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
142
143 //----------------------------------------------------------------------------
144 // Code generation helper function
145 //----------------------------------------------------------------------------
146 llvm::Value* EmitLoadMethodObjectAddr();
147
148 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
149
150 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
151
152 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
153 llvm::Value* this_addr);
154
155 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
156 llvm::Value* index_value,
157 JType elem_jty);
158
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100159 //----------------------------------------------------------------------------
160 // Invoke helper function
161 //----------------------------------------------------------------------------
162 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
163
164 //----------------------------------------------------------------------------
165 // Inlining helper functions
166 //----------------------------------------------------------------------------
167 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
168
169 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
170 llvm::Value** result, bool is_empty);
171
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700172 private:
173 //----------------------------------------------------------------------------
174 // Expand Greenland intrinsics
175 //----------------------------------------------------------------------------
176 void Expand_TestSuspend(llvm::CallInst& call_inst);
177
TDYa1279a129452012-07-19 03:10:08 -0700178 void Expand_MarkGCCard(llvm::CallInst& call_inst);
179
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700180 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
181
182 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
183
184 void Expand_LockObject(llvm::Value* obj);
185
186 void Expand_UnlockObject(llvm::Value* obj);
187
188 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
189 llvm::Value* index_value,
190 JType elem_jty);
191
192 void Expand_ArrayPut(llvm::Value* new_value,
193 llvm::Value* array_addr,
194 llvm::Value* index_value,
195 JType elem_jty);
196
197 void Expand_FilledNewArray(llvm::CallInst& call_inst);
198
199 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
200 llvm::Value* is_volatile_value,
201 llvm::Value* object_addr,
202 JType field_jty);
203
204 void Expand_IPutFast(llvm::Value* field_offset_value,
205 llvm::Value* is_volatile_value,
206 llvm::Value* object_addr,
207 llvm::Value* new_value,
208 JType field_jty);
209
210 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
211 llvm::Value* field_offset_value,
212 llvm::Value* is_volatile_value,
213 JType field_jty);
214
215 void Expand_SPutFast(llvm::Value* static_storage_addr,
216 llvm::Value* field_offset_value,
217 llvm::Value* is_volatile_value,
218 llvm::Value* new_value,
219 JType field_jty);
220
221 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
222
223 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
224
225 llvm::Value*
226 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
227
228 llvm::Value*
229 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
230 llvm::Value* this_addr);
231
232 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
233
TDYa1274ec8ccd2012-08-11 07:04:57 -0700234 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700235
TDYa127ce4cc0d2012-11-18 16:59:53 -0800236 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700237
TDYa1278e950c12012-11-02 09:58:19 -0700238 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
239
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700240 void Expand_PopShadowFrame();
241
242 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
243
TDYa127a1b21852012-07-23 03:20:39 -0700244 //----------------------------------------------------------------------------
245 // Quick
246 //----------------------------------------------------------------------------
247
248 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
249 llvm::Value* src2_value,
250 bool gt_bias);
251
252 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
253
254 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
255 llvm::Value* cmp_lt);
256
TDYa127f71bf5a2012-07-29 20:09:52 -0700257 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700258 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
259
TDYa1275e869b62012-07-25 00:45:39 -0700260 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
261 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
262
TDYa1275a26d442012-07-26 18:58:38 -0700263 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
264 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
265
266 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
267 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
268
TDYa127f71bf5a2012-07-29 20:09:52 -0700269 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
270 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
271
272 void Expand_MonitorEnter(llvm::CallInst& call_inst);
273 void Expand_MonitorExit(llvm::CallInst& call_inst);
274
275 void Expand_HLCheckCast(llvm::CallInst& call_inst);
276 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
277
278 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
279
280 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
281
282 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
283 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
284 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
285 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
286
287 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
288 llvm::Value* array_length_value,
289 uint32_t type_idx,
290 bool is_filled_new_array);
291
292 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700293 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700294 llvm::Value* this_addr,
295 uint32_t dex_pc,
296 bool is_fast_path);
297
TDYa1275e869b62012-07-25 00:45:39 -0700298 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
299
300 void EmitUpdateDexPC(uint32_t dex_pc);
301
302 void EmitGuard_DivZeroException(uint32_t dex_pc,
303 llvm::Value* denominator,
304 JType op_jty);
305
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100306 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
307 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700308
309 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
310 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100311 llvm::Value* index,
312 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700313
Ian Rogers8e696052013-03-04 09:00:40 -0800314 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700315
316 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
317
318 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
319 const char* postfix);
320
321 int32_t GetTryItemOffset(uint32_t dex_pc);
322
323 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
324
325 llvm::BasicBlock* GetUnwindBasicBlock();
326
327 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
328
329 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
330
Logan Chien75e4b602012-07-23 14:24:12 -0700331 //----------------------------------------------------------------------------
332 // Expand Arithmetic Helper Intrinsics
333 //----------------------------------------------------------------------------
334
335 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
336 llvm::Value* src2_value,
337 IntegerShiftKind kind,
338 JType op_jty);
339
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700340 public:
341 static char ID;
342
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700343 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800344 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700345 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
346 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700347 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800348 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800349 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800350 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700351
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700352 bool runOnFunction(llvm::Function& func);
353
354 private:
Logan Chien67645d82012-08-17 09:10:54 +0800355 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700356
357 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
358 llvm::CallInst& call_inst);
359
360};
361
362char GBCExpanderPass::ID = 0;
363
364bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800365 VLOG(compiler) << "GBC expansion on " << func.getName().str();
366
TDYa127b672d1e2012-06-28 21:21:45 -0700367 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800368 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700369 return false;
370 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700371
Logan Chien67645d82012-08-17 09:10:54 +0800372 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700373 shadow_frame_ = NULL;
374 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700375 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800376 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700377
Ian Rogers89756f22013-03-04 16:40:02 -0800378 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
379 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700380 basic_block_unwind_ = NULL;
381 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
382 bb_iter != bb_end;
383 ++bb_iter) {
384 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
385 continue;
386 }
387 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
388 basic_blocks_[dex_pc] = bb_iter;
389 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700390
Logan Chien67645d82012-08-17 09:10:54 +0800391 // Insert stack overflow check
392 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700393
Logan Chien67645d82012-08-17 09:10:54 +0800394 // Rewrite the intrinsics
395 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700396
397 VERIFY_LLVM_FUNCTION(func);
398
Logan Chien67645d82012-08-17 09:10:54 +0800399 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700400}
401
Logan Chien67645d82012-08-17 09:10:54 +0800402void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
403 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700404
Logan Chien67645d82012-08-17 09:10:54 +0800405 llvm::BasicBlock::iterator inst_iter = original_block->begin();
406 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700407
Logan Chien67645d82012-08-17 09:10:54 +0800408 while (inst_iter != inst_end) {
409 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
410 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700411
Logan Chien67645d82012-08-17 09:10:54 +0800412 if (call_inst) {
413 llvm::Function* callee_func = call_inst->getCalledFunction();
414 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
415 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700416
Logan Chien67645d82012-08-17 09:10:54 +0800417 if (intr_id == IntrinsicHelper::UnknownId) {
418 // This is not intrinsic call. Skip this instruction.
419 ++inst_iter;
420 continue;
421 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700422
Logan Chien67645d82012-08-17 09:10:54 +0800423 // Rewrite the intrinsic and change the function
424 changed_ = true;
425 irb_.SetInsertPoint(inst_iter);
426
427 // Expand the intrinsic
428 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
429 inst_iter->replaceAllUsesWith(new_value);
430 }
431
432 // Remove the old intrinsic call instruction
433 llvm::BasicBlock::iterator old_inst = inst_iter++;
434 old_inst->eraseFromParent();
435
436 // Splice the instruction to the new basic block
437 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
438 if (next_basic_block != curr_basic_block) {
439 next_basic_block->getInstList().splice(
440 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
441 inst_iter, inst_end);
442 curr_basic_block = next_basic_block;
443 inst_end = curr_basic_block->end();
444 }
445 }
446}
447
448
449void GBCExpanderPass::RewriteFunction() {
450 size_t num_basic_blocks = func_->getBasicBlockList().size();
451 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
452 // because we will create new basic block while expanding the intrinsics.
453 // We only want to iterate through the input basic blocks.
454
TDYa127aa558872012-08-16 05:11:07 -0700455 landing_pad_phi_mapping_.clear();
456
Logan Chien67645d82012-08-17 09:10:54 +0800457 for (llvm::Function::iterator bb_iter = func_->begin();
458 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700459 // Set insert point to current basic block.
460 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800461
TDYa12755e5e6c2012-09-11 15:14:42 -0700462 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700463
Logan Chien67645d82012-08-17 09:10:54 +0800464 // Rewrite the basic block
465 RewriteBasicBlock(bb_iter);
466
467 // Update the phi-instructions in the successor basic block
468 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
469 if (last_block != bb_iter) {
470 UpdatePhiInstruction(bb_iter, last_block);
471 }
472 }
TDYa127aa558872012-08-16 05:11:07 -0700473
474 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
475 HandlerPHIMap handler_phi;
476 // Iterate every used landing pad basic block
477 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
478 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
479 if (lbb == NULL) {
480 continue;
481 }
482
483 llvm::TerminatorInst* term_inst = lbb->getTerminator();
484 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
485 = landing_pad_phi_mapping_[lbb];
486 irb_.SetInsertPoint(lbb->begin());
487
488 // Iterate every succeeding basic block (catch block)
489 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
490 succ_iter != succ_end; ++succ_iter) {
491 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
492
493 // Iterate every phi instructions in the succeeding basic block
494 for (llvm::BasicBlock::iterator
495 inst_iter = succ_basic_block->begin(),
496 inst_end = succ_basic_block->end();
497 inst_iter != inst_end; ++inst_iter) {
498 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
499
500 if (!phi) {
501 break; // Meet non-phi instruction. Done.
502 }
503
504 if (handler_phi[phi] == NULL) {
505 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
506 }
507
508 // Create new_phi in landing pad
509 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
510 // Insert all incoming value into new_phi by rewrite_pair
511 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
512 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
513 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
514 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
515 }
516 // Delete all incoming value from phi by rewrite_pair
517 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
518 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
519 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
520 if (old_bb_idx >= 0) {
521 phi->removeIncomingValue(old_bb_idx, false);
522 }
523 }
524 // Insert new_phi into new handler phi
525 handler_phi[phi]->addIncoming(new_phi, lbb);
526 }
527 }
528 }
529
530 // Replace all handler phi
531 // We can't just use the old handler phi, because some exception edges will disappear after we
532 // compute fast-path.
533 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
534 llvm::PHINode* old_phi = it->first;
535 llvm::PHINode* new_phi = it->second;
536 new_phi->insertBefore(old_phi);
537 old_phi->replaceAllUsesWith(new_phi);
538 old_phi->eraseFromParent();
539 }
Logan Chien67645d82012-08-17 09:10:54 +0800540}
541
542void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
543 llvm::BasicBlock* new_basic_block) {
544 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
545
546 if (!term_inst) {
547 return; // No terminating instruction in new_basic_block. Nothing to do.
548 }
549
550 // Iterate every succeeding basic block
551 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
552 succ_iter != succ_end; ++succ_iter) {
553 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
554
555 // Iterate every phi instructions in the succeeding basic block
556 for (llvm::BasicBlock::iterator
557 inst_iter = succ_basic_block->begin(),
558 inst_end = succ_basic_block->end();
559 inst_iter != inst_end; ++inst_iter) {
560 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
561
562 if (!phi) {
563 break; // Meet non-phi instruction. Done.
564 }
565
566 // Update the incoming block of this phi instruction
567 for (llvm::PHINode::block_iterator
568 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
569 ibb_iter != ibb_end; ++ibb_iter) {
570 if (*ibb_iter == old_basic_block) {
571 *ibb_iter = new_basic_block;
572 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700573 }
574 }
575 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700576}
577
578llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
579 llvm::CallInst& inst) {
580 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
581 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
582 // function, therefore only called function is needed to change.
583 unsigned num_args = inst.getNumArgOperands();
584
585 if (num_args <= 0) {
586 return irb_.CreateCall(irb_.GetRuntime(rt));
587 } else {
588 std::vector<llvm::Value*> args;
589 for (unsigned i = 0; i < num_args; i++) {
590 args.push_back(inst.getArgOperand(i));
591 }
592
593 return irb_.CreateCall(irb_.GetRuntime(rt), args);
594 }
595}
596
Logan Chien67645d82012-08-17 09:10:54 +0800597void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700598GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
599 llvm::Function* func = first_non_alloca->getParent()->getParent();
600 llvm::Module* module = func->getParent();
601
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700602 // Call llvm intrinsic function to get frame address.
603 llvm::Function* frameaddress =
604 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
605
606 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
607 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
608
609 // Cast i8* to int
610 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
611
612 // Get thread.stack_end_
613 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700614 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700615 irb_.getPtrEquivIntTy(),
616 kTBAARuntimeInfo);
617
618 // Check the frame address < thread.stack_end_ ?
619 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
620
621 llvm::BasicBlock* block_exception =
622 llvm::BasicBlock::Create(context_, "stack_overflow", func);
623
624 llvm::BasicBlock* block_continue =
625 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
626
627 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
628
629 // If stack overflow, throw exception.
630 irb_.SetInsertPoint(block_exception);
631 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
632
633 // Unwind.
634 llvm::Type* ret_type = func->getReturnType();
635 if (ret_type->isVoidTy()) {
636 irb_.CreateRetVoid();
637 } else {
638 // The return value is ignored when there's an exception. MethodCompiler
639 // returns zero value under the the corresponding return type in this case.
640 // GBCExpander returns LLVM undef value here for brevity
641 irb_.CreateRet(llvm::UndefValue::get(ret_type));
642 }
643
644 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700645}
646
TDYa127920be7c2012-09-10 17:13:22 -0700647llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700648 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
649
650 return irb_.LoadFromObjectOffset(method_object_addr,
651 offset.Int32Value(),
652 irb_.getJObjectTy(),
653 kTBAAConstJObject);
654}
655
656llvm::Value*
657GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
658 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800659 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700660
661 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
662
663 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
664}
665
666llvm::Value*
667GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
668 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800669 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700670
671 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
672
673 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
674}
675
676llvm::Value* GBCExpanderPass::
677EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
678 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800679 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700680
681 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
682
683 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
684}
685
686llvm::Value* GBCExpanderPass::
687EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
688 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800689 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700690
691 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
692
693 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
694}
695
696llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
697 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
698 return parent_func->arg_begin();
699}
700
701llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
702 // Load array length
703 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800704 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700705 irb_.getJIntTy(),
706 kTBAAConstJObject);
707
708}
709
710llvm::Value*
711GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
712 llvm::Value* callee_method_object_field_addr =
713 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
714
TDYa127ce4cc0d2012-11-18 16:59:53 -0800715 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700716}
717
718llvm::Value* GBCExpanderPass::
719EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
720 // Load class object of *this* pointer
721 llvm::Value* class_object_addr =
722 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800723 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700724 irb_.getJObjectTy(),
725 kTBAAConstJObject);
726
727 // Load vtable address
728 llvm::Value* vtable_addr =
729 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800730 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700731 irb_.getJObjectTy(),
732 kTBAAConstJObject);
733
734 // Load callee method object
735 llvm::Value* vtable_idx_value =
736 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
737
738 llvm::Value* method_field_addr =
739 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
740
741 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
742}
743
744// Emit Array GetElementPtr
745llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
746 llvm::Value* index_value,
747 JType elem_jty) {
748
749 int data_offset;
750 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800751 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
752 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700753 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800754 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700755 }
756
757 llvm::Constant* data_offset_value =
758 irb_.getPtrEquivInt(data_offset);
759
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800760 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700761
762 llvm::Value* array_data_addr =
763 irb_.CreatePtrDisp(array_addr, data_offset_value,
764 elem_type->getPointerTo());
765
766 return irb_.CreateGEP(array_data_addr, index_value);
767}
768
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100769llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
770 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
771 art::InvokeType invoke_type =
772 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
773 bool is_static = (invoke_type == art::kStatic);
774 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
775
776 // Load *this* actual parameter
777 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
778
779 // Compute invoke related information for compiler decision
780 int vtable_idx = -1;
781 uintptr_t direct_code = 0;
782 uintptr_t direct_method = 0;
783 bool is_fast_path = driver_->
784 ComputeInvokeInfo(callee_method_idx, dex_compilation_unit_,
785 invoke_type, vtable_idx, direct_code, direct_method);
786
787 // Load the method object
788 llvm::Value* callee_method_object_addr = NULL;
789
790 if (!is_fast_path) {
791 callee_method_object_addr =
792 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
793 this_addr, dex_pc, is_fast_path);
794 } else {
795 switch (invoke_type) {
796 case art::kStatic:
797 case art::kDirect:
798 if (direct_method != 0u &&
799 direct_method != static_cast<uintptr_t>(-1)) {
800 callee_method_object_addr =
801 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
802 irb_.getJObjectTy());
803 } else {
804 callee_method_object_addr =
805 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
806 }
807 break;
808
809 case art::kVirtual:
810 DCHECK(vtable_idx != -1);
811 callee_method_object_addr =
812 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
813 break;
814
815 case art::kSuper:
816 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
817 "the fast path.";
818 break;
819
820 case art::kInterface:
821 callee_method_object_addr =
822 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
823 invoke_type, this_addr,
824 dex_pc, is_fast_path);
825 break;
826 }
827 }
828
829 // Load the actual parameter
830 std::vector<llvm::Value*> args;
831
832 args.push_back(callee_method_object_addr); // method object for callee
833
834 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
835 args.push_back(call_inst.getArgOperand(i));
836 }
837
838 llvm::Value* code_addr;
839 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
840 callee_method_idx, is_static);
841 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
842 code_addr =
843 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
844 func_type->getPointerTo());
845 } else {
846 code_addr =
847 irb_.LoadFromObjectOffset(callee_method_object_addr,
848 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
849 func_type->getPointerTo(), kTBAARuntimeInfo);
850 }
851
852 // Invoke callee
853 EmitUpdateDexPC(dex_pc);
854 llvm::Value* retval = irb_.CreateCall(code_addr, args);
855 EmitGuard_ExceptionLandingPad(dex_pc);
856
857 return retval;
858}
859
860bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
861 llvm::Value** result) {
862 DCHECK(result != NULL);
863
864 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
865 std::string callee_method_name(
866 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
867
868 if (callee_method_name == "int java.lang.String.length()") {
869 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
870 false /* is_empty */);
871 }
872 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
873 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
874 true /* is_empty */);
875 }
876
877 *result = NULL;
878 return false;
879}
880
881bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
882 llvm::Value** result,
883 bool is_empty) {
884 art::InvokeType invoke_type =
885 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
886 DCHECK_NE(invoke_type, art::kStatic);
887 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
888
889 llvm::Value* this_object = call_inst.getArgOperand(3);
890 llvm::Value* string_count =
891 irb_.LoadFromObjectOffset(this_object,
892 art::mirror::String::CountOffset().Int32Value(),
893 irb_.getJIntTy(),
894 kTBAAConstJObject);
895 if (is_empty) {
896 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
897 irb_.getJInt(0));
898 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
899 irb_.getJBoolean(true),
900 irb_.getJBoolean(false));
901 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
902 *result = is_empty;
903 } else {
904 *result = string_count;
905 }
906 return true;
907}
908
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700909void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800910 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
911
912 llvm::Value* suspend_count =
913 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
914 irb_.getInt16Ty(),
915 kTBAARuntimeInfo);
916 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
917
918 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
919 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
920
921 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
922
923 irb_.SetInsertPoint(basic_block_suspend);
924 if (dex_pc != art::DexFile::kDexNoIndex) {
925 EmitUpdateDexPC(dex_pc);
926 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700927 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800928
929 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
930 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
931 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
932
933 irb_.SetInsertPoint(basic_block_exception);
934 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
935 if (ret_type->isVoidTy()) {
936 irb_.CreateRetVoid();
937 } else {
938 // The return value is ignored when there's an exception.
939 irb_.CreateRet(llvm::UndefValue::get(ret_type));
940 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800941
942 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700943 return;
944}
945
TDYa1279a129452012-07-19 03:10:08 -0700946void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700947 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700948 return;
949}
950
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700951llvm::Value*
952GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
953 uint32_t string_idx =
954 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
955
956 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
957
TDYa127ce4cc0d2012-11-18 16:59:53 -0800958 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700959}
960
961llvm::Value*
962GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
963 uint32_t type_idx =
964 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
965
966 llvm::Value* type_field_addr =
967 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
968
TDYa127ce4cc0d2012-11-18 16:59:53 -0800969 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700970}
971
972void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700973 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700974 return;
975}
976
977void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700978 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700979 return;
980}
981
982llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
983 llvm::Value* index_value,
984 JType elem_jty) {
985 llvm::Value* array_elem_addr =
986 EmitArrayGEP(array_addr, index_value, elem_jty);
987
988 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
989}
990
991void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
992 llvm::Value* array_addr,
993 llvm::Value* index_value,
994 JType elem_jty) {
995 llvm::Value* array_elem_addr =
996 EmitArrayGEP(array_addr, index_value, elem_jty);
997
998 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
999
1000 return;
1001}
1002
1003void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1004 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1005 llvm::Value* array = call_inst.getArgOperand(0);
1006
1007 uint32_t element_jty =
1008 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1009
1010 DCHECK(call_inst.getNumArgOperands() > 2);
1011 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1012
1013 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1014
1015 uint32_t alignment;
1016 llvm::Constant* elem_size;
1017 llvm::PointerType* field_type;
1018
1019 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1020 // as the element, thus we are only checking 2 cases: primitive int and
1021 // non-primitive type.
1022 if (is_elem_int_ty) {
1023 alignment = sizeof(int32_t);
1024 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1025 field_type = irb_.getJIntTy()->getPointerTo();
1026 } else {
1027 alignment = irb_.getSizeOfPtrEquivInt();
1028 elem_size = irb_.getSizeOfPtrEquivIntValue();
1029 field_type = irb_.getJObjectTy()->getPointerTo();
1030 }
1031
1032 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001033 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001034
1035 llvm::Value* data_field_addr =
1036 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1037
1038 for (unsigned i = 0; i < num_elements; ++i) {
1039 // Values to fill the array begin at the 3rd argument
1040 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1041
1042 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1043
1044 data_field_addr =
1045 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1046 }
1047
1048 return;
1049}
1050
1051llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1052 llvm::Value* /*is_volatile_value*/,
1053 llvm::Value* object_addr,
1054 JType field_jty) {
1055 int field_offset =
1056 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1057
1058 DCHECK_GE(field_offset, 0);
1059
1060 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001061 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001062
1063 field_offset_value = irb_.getPtrEquivInt(field_offset);
1064
1065 llvm::Value* field_addr =
1066 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1067
1068 // TODO: Check is_volatile. We need to generate atomic load instruction
1069 // when is_volatile is true.
1070 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1071}
1072
1073void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1074 llvm::Value* /* is_volatile_value */,
1075 llvm::Value* object_addr,
1076 llvm::Value* new_value,
1077 JType field_jty) {
1078 int field_offset =
1079 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1080
1081 DCHECK_GE(field_offset, 0);
1082
1083 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001084 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001085
1086 field_offset_value = irb_.getPtrEquivInt(field_offset);
1087
1088 llvm::Value* field_addr =
1089 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1090
1091 // TODO: Check is_volatile. We need to generate atomic store instruction
1092 // when is_volatile is true.
1093 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1094
1095 return;
1096}
1097
1098llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1099 llvm::Value* field_offset_value,
1100 llvm::Value* /*is_volatile_value*/,
1101 JType field_jty) {
1102 int field_offset =
1103 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1104
1105 DCHECK_GE(field_offset, 0);
1106
1107 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1108
1109 llvm::Value* static_field_addr =
1110 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001111 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001112
1113 // TODO: Check is_volatile. We need to generate atomic store instruction
1114 // when is_volatile is true.
1115 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1116}
1117
1118void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1119 llvm::Value* field_offset_value,
1120 llvm::Value* /* is_volatile_value */,
1121 llvm::Value* new_value,
1122 JType field_jty) {
1123 int field_offset =
1124 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1125
1126 DCHECK_GE(field_offset, 0);
1127
1128 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1129
1130 llvm::Value* static_field_addr =
1131 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001132 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001133
1134 // TODO: Check is_volatile. We need to generate atomic store instruction
1135 // when is_volatile is true.
1136 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1137
1138 return;
1139}
1140
1141llvm::Value*
1142GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1143 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001144 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001145 irb_.getJObjectTy(),
1146 kTBAAConstJObject);
1147}
1148
1149llvm::Value*
1150GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1151 uint32_t type_idx =
1152 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1153
1154 llvm::Value* storage_field_addr =
1155 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1156
TDYa127ce4cc0d2012-11-18 16:59:53 -08001157 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001158}
1159
1160llvm::Value*
1161GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1162 uint32_t callee_method_idx =
1163 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1164
1165 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1166}
1167
1168llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1169 llvm::Value* vtable_idx_value,
1170 llvm::Value* this_addr) {
1171 int vtable_idx =
1172 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1173
1174 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1175}
1176
1177llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1178 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1179 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1180 unsigned num_args = call_inst.getNumArgOperands();
1181 llvm::Type* ret_type = call_inst.getType();
1182
1183 // Determine the function type of the callee method
1184 std::vector<llvm::Type*> args_type;
1185 std::vector<llvm::Value*> args;
1186 for (unsigned i = 0; i < num_args; i++) {
1187 args.push_back(call_inst.getArgOperand(i));
1188 args_type.push_back(args[i]->getType());
1189 }
1190
1191 llvm::FunctionType* callee_method_type =
1192 llvm::FunctionType::get(ret_type, args_type, false);
1193
1194 llvm::Value* code_addr =
1195 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001196 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001197 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001198 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001199
1200 // Invoke callee
1201 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1202
1203 return retval;
1204}
1205
TDYa1274ec8ccd2012-08-11 07:04:57 -07001206llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001207 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001208 llvm::Value* dividend = call_inst.getArgOperand(0);
1209 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001210 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1211 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001212 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1213
1214 // Check the special case: MININT / -1 = MININT
1215 // That case will cause overflow, which is undefined behavior in llvm.
1216 // So we check the divisor is -1 or not, if the divisor is -1, we do
1217 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001218 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001219 llvm::Value* zero = irb_.getJZero(op_jty);
1220 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1221
TDYa1275e869b62012-07-25 00:45:39 -07001222 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001223 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1224 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1225 llvm::BasicBlock* neg_one_cont =
1226 llvm::BasicBlock::Create(context_, "", parent);
1227
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001228 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1229 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1230
1231 // If divisor == -1
1232 irb_.SetInsertPoint(eq_neg_one);
1233 llvm::Value* eq_result;
1234 if (is_div) {
1235 // We can just change from "dividend div -1" to "neg dividend". The sub
1236 // don't care the sign/unsigned because of two's complement representation.
1237 // And the behavior is what we want:
1238 // -(2^n) (2^n)-1
1239 // MININT < k <= MAXINT -> mul k -1 = -k
1240 // MININT == k -> mul k -1 = k
1241 //
1242 // LLVM use sub to represent 'neg'
1243 eq_result = irb_.CreateSub(zero, dividend);
1244 } else {
1245 // Everything modulo -1 will be 0.
1246 eq_result = zero;
1247 }
1248 irb_.CreateBr(neg_one_cont);
1249
1250 // If divisor != -1, just do the division.
1251 irb_.SetInsertPoint(ne_neg_one);
1252 llvm::Value* ne_result;
1253 if (is_div) {
1254 ne_result = irb_.CreateSDiv(dividend, divisor);
1255 } else {
1256 ne_result = irb_.CreateSRem(dividend, divisor);
1257 }
1258 irb_.CreateBr(neg_one_cont);
1259
1260 irb_.SetInsertPoint(neg_one_cont);
1261 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1262 result->addIncoming(eq_result, eq_neg_one);
1263 result->addIncoming(ne_result, ne_neg_one);
1264
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001265 return result;
1266}
1267
TDYa127ce4cc0d2012-11-18 16:59:53 -08001268void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001269 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1270 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001271 uint16_t num_vregs =
1272 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001273
1274 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001275 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001276
Sebastien Hertz77209702013-02-28 16:34:13 +01001277 // Create allocas at the start of entry block.
1278 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1279 llvm::BasicBlock* entry_block = &func_->front();
1280 irb_.SetInsertPoint(&entry_block->front());
1281
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001282 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1283
1284 // Alloca a pointer to old shadow frame
1285 old_shadow_frame_ =
1286 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1287
Sebastien Hertz77209702013-02-28 16:34:13 +01001288 irb_.restoreIP(irb_ip_original);
1289
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001290 // Push the shadow frame
1291 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1292
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001293 llvm::Value* shadow_frame_upcast =
1294 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1295
1296 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1297 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001298 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001299
1300 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1301
1302 return;
1303}
1304
TDYa1278e950c12012-11-02 09:58:19 -07001305void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1306 llvm::Value* value) {
1307 DCHECK(shadow_frame_ != NULL);
1308
1309 llvm::Value* gep_index[] = {
1310 irb_.getInt32(0), // No pointer displacement
TDYa127ce4cc0d2012-11-18 16:59:53 -08001311 irb_.getInt32(1), // VRegs
TDYa1278e950c12012-11-02 09:58:19 -07001312 entry_idx // Pointer field
1313 };
1314
1315 llvm::Value* vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1316
1317 irb_.CreateStore(value,
1318 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1319 kTBAAShadowFrame);
1320 return;
1321}
1322
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001323void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001324 if (old_shadow_frame_ == NULL) {
1325 return;
1326 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001327 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1328 return;
1329}
1330
1331void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1332 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001333 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001334 dex_pc_value,
1335 kTBAAShadowFrame);
1336 return;
1337}
1338
Logan Chien67645d82012-08-17 09:10:54 +08001339void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001340 // All alloca instructions are generated in the first basic block of the
1341 // function, and there are no alloca instructions after the first non-alloca
1342 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001343
Logan Chien67645d82012-08-17 09:10:54 +08001344 llvm::BasicBlock* first_basic_block = &func.front();
1345
1346 // Look for first non-alloca instruction
1347 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001348 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1349 ++first_non_alloca;
1350 }
1351
Logan Chien67645d82012-08-17 09:10:54 +08001352 irb_.SetInsertPoint(first_non_alloca);
1353
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001354 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1355 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001356 EmitStackOverflowCheck(&*first_non_alloca);
1357
TDYa127890ea892012-08-22 10:49:42 -07001358 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001359
Logan Chien67645d82012-08-17 09:10:54 +08001360 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1361 if (next_basic_block != first_basic_block) {
1362 // Splice the rest of the instruction to the continuing basic block
1363 next_basic_block->getInstList().splice(
1364 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1365 first_non_alloca, first_basic_block->end());
1366
1367 // Rewrite the basic block
1368 RewriteBasicBlock(next_basic_block);
1369
1370 // Update the phi-instructions in the successor basic block
1371 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1372 }
1373
1374 // We have changed the basic block
1375 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001376}
1377
TDYa1275e869b62012-07-25 00:45:39 -07001378// ==== High-level intrinsic expander ==========================================
1379
TDYa127a1b21852012-07-23 03:20:39 -07001380llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1381 llvm::Value* src2_value,
1382 bool gt_bias) {
1383 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1384 llvm::Value* cmp_lt;
1385
1386 if (gt_bias) {
1387 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1388 } else {
1389 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1390 }
1391
1392 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1393}
1394
1395llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1396 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1397 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1398
1399 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1400}
1401
1402llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1403 llvm::Value* cmp_lt) {
1404
1405 llvm::Constant* zero = irb_.getJInt(0);
1406 llvm::Constant* pos1 = irb_.getJInt(1);
1407 llvm::Constant* neg1 = irb_.getJInt(-1);
1408
1409 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1410 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1411
1412 return result_eq;
1413}
1414
Logan Chien75e4b602012-07-23 14:24:12 -07001415llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1416 llvm::Value* src2_value,
1417 IntegerShiftKind kind,
1418 JType op_jty) {
1419 DCHECK(op_jty == kInt || op_jty == kLong);
1420
1421 // Mask and zero-extend RHS properly
1422 if (op_jty == kInt) {
1423 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1424 } else {
1425 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1426 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1427 }
1428
1429 // Create integer shift llvm instruction
1430 switch (kind) {
1431 case kIntegerSHL:
1432 return irb_.CreateShl(src1_value, src2_value);
1433
1434 case kIntegerSHR:
1435 return irb_.CreateAShr(src1_value, src2_value);
1436
1437 case kIntegerUSHR:
1438 return irb_.CreateLShr(src1_value, src2_value);
1439
1440 default:
1441 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1442 return NULL;
1443 }
1444}
1445
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001446llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1447 switch (jty) {
1448 case kBoolean:
1449 case kChar:
1450 return irb_.CreateZExt(value, irb_.getJType(kInt));
1451 case kByte:
1452 case kShort:
1453 return irb_.CreateSExt(value, irb_.getJType(kInt));
1454 case kVoid:
1455 case kInt:
1456 case kLong:
1457 case kFloat:
1458 case kDouble:
1459 case kObject:
1460 return value; // Nothing to do.
1461 default:
1462 LOG(FATAL) << "Unknown java type: " << jty;
1463 return NULL;
1464 }
1465}
1466
1467llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1468 switch (jty) {
1469 case kBoolean:
1470 case kChar:
1471 case kByte:
1472 case kShort:
1473 return irb_.CreateTrunc(value, irb_.getJType(jty));
1474 case kVoid:
1475 case kInt:
1476 case kLong:
1477 case kFloat:
1478 case kDouble:
1479 case kObject:
1480 return value; // Nothing to do.
1481 default:
1482 LOG(FATAL) << "Unknown java type: " << jty;
1483 return NULL;
1484 }
1485}
1486
TDYa1275a26d442012-07-26 18:58:38 -07001487llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1488 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001489 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1490 llvm::Value* array_addr = call_inst.getArgOperand(1);
1491 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001492 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001493
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001494 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1495 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1496 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001497
1498 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1499
1500 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1501
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001502 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001503}
1504
1505
1506void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1507 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001508 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1509 llvm::Value* new_value = call_inst.getArgOperand(1);
1510 llvm::Value* array_addr = call_inst.getArgOperand(2);
1511 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001512 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001513
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001514 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1515 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1516 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001517
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001518 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001519
1520 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1521
1522 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1523 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1524
1525 irb_.CreateCall2(runtime_func, new_value, array_addr);
1526
1527 EmitGuard_ExceptionLandingPad(dex_pc);
1528
1529 EmitMarkGCCard(new_value, array_addr);
1530 }
1531
1532 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1533
1534 return;
1535}
1536
TDYa1275e869b62012-07-25 00:45:39 -07001537llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1538 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001539 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1540 llvm::Value* object_addr = call_inst.getArgOperand(1);
1541 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001542 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001543
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001544 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001545
1546 llvm::Value* field_value;
1547
1548 int field_offset;
1549 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001550 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001551 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001552
1553 if (!is_fast_path) {
1554 llvm::Function* runtime_func;
1555
1556 if (field_jty == kObject) {
1557 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1558 } else if (field_jty == kLong || field_jty == kDouble) {
1559 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1560 } else {
1561 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1562 }
1563
1564 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1565
1566 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1567
1568 EmitUpdateDexPC(dex_pc);
1569
1570 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1571 method_object_addr, object_addr);
1572
1573 EmitGuard_ExceptionLandingPad(dex_pc);
1574
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001575 if (field_jty == kFloat || field_jty == kDouble) {
1576 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1577 }
TDYa1275e869b62012-07-25 00:45:39 -07001578 } else {
1579 DCHECK_GE(field_offset, 0);
1580
1581 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001582 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001583
1584 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1585
1586 llvm::Value* field_addr =
1587 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1588
TDYa1275e869b62012-07-25 00:45:39 -07001589 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001590 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001591
1592 if (is_volatile) {
1593 irb_.CreateMemoryBarrier(art::kLoadLoad);
1594 }
TDYa1275e869b62012-07-25 00:45:39 -07001595 }
1596
TDYa1275e869b62012-07-25 00:45:39 -07001597 return field_value;
1598}
1599
1600void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1601 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001602 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001603 llvm::Value* new_value = call_inst.getArgOperand(1);
1604 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001605 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001606 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001607
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001608 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001609
1610 int field_offset;
1611 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001612 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001613 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001614
1615 if (!is_fast_path) {
1616 llvm::Function* runtime_func;
1617
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001618 if (field_jty == kFloat) {
1619 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1620 } else if (field_jty == kDouble) {
1621 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1622 }
1623
TDYa1275e869b62012-07-25 00:45:39 -07001624 if (field_jty == kObject) {
1625 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1626 } else if (field_jty == kLong || field_jty == kDouble) {
1627 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1628 } else {
1629 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1630 }
1631
1632 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1633
1634 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1635
1636 EmitUpdateDexPC(dex_pc);
1637
1638 irb_.CreateCall4(runtime_func, field_idx_value,
1639 method_object_addr, object_addr, new_value);
1640
1641 EmitGuard_ExceptionLandingPad(dex_pc);
1642
1643 } else {
1644 DCHECK_GE(field_offset, 0);
1645
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001646 if (is_volatile) {
1647 irb_.CreateMemoryBarrier(art::kStoreStore);
1648 }
1649
TDYa1275e869b62012-07-25 00:45:39 -07001650 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001651 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001652
1653 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1654
1655 llvm::Value* field_addr =
1656 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1657
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001658 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001659 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1660
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001661 if (is_volatile) {
1662 irb_.CreateMemoryBarrier(art::kLoadLoad);
1663 }
1664
TDYa1275e869b62012-07-25 00:45:39 -07001665 if (field_jty == kObject) { // If put an object, mark the GC card table.
1666 EmitMarkGCCard(new_value, object_addr);
1667 }
1668 }
1669
1670 return;
1671}
1672
TDYa127f71bf5a2012-07-29 20:09:52 -07001673llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1674 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001675 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1676 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001677 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1678
1679 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1680
1681 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1682
1683 llvm::Function* runtime_func =
1684 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1685
1686 EmitUpdateDexPC(dex_pc);
1687
1688 llvm::Value* type_object_addr =
1689 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1690
1691 EmitGuard_ExceptionLandingPad(dex_pc);
1692
1693 return type_object_addr;
1694
1695 } else {
1696 // Try to load the class (type) object from the test cache.
1697 llvm::Value* type_field_addr =
1698 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1699
TDYa127ce4cc0d2012-11-18 16:59:53 -08001700 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001701
Ian Rogers89756f22013-03-04 16:40:02 -08001702 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001703 return type_object_addr;
1704 }
1705
1706 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1707
1708 // Test whether class (type) object is in the dex cache or not
1709 llvm::Value* equal_null =
1710 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1711
1712 llvm::BasicBlock* block_cont =
1713 CreateBasicBlockWithDexPC(dex_pc, "cont");
1714
1715 llvm::BasicBlock* block_load_class =
1716 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1717
1718 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1719
1720 // Failback routine to load the class object
1721 irb_.SetInsertPoint(block_load_class);
1722
1723 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1724
1725 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1726
1727 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1728
1729 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1730
1731 EmitUpdateDexPC(dex_pc);
1732
1733 llvm::Value* loaded_type_object_addr =
1734 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1735
1736 EmitGuard_ExceptionLandingPad(dex_pc);
1737
1738 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1739
1740 irb_.CreateBr(block_cont);
1741
1742 // Now the class object must be loaded
1743 irb_.SetInsertPoint(block_cont);
1744
1745 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1746
1747 phi->addIncoming(type_object_addr, block_original);
1748 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1749
1750 return phi;
1751 }
1752}
1753
TDYa1275a26d442012-07-26 18:58:38 -07001754llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1755 uint32_t type_idx) {
1756 llvm::BasicBlock* block_load_static =
1757 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1758
1759 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1760
1761 // Load static storage from dex cache
1762 llvm::Value* storage_field_addr =
1763 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1764
TDYa127ce4cc0d2012-11-18 16:59:53 -08001765 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001766
1767 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1768
1769 // Test: Is the static storage of this class initialized?
1770 llvm::Value* equal_null =
1771 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1772
1773 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1774
1775 // Failback routine to load the class object
1776 irb_.SetInsertPoint(block_load_static);
1777
1778 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1779
1780 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1781
1782 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1783
1784 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1785
1786 EmitUpdateDexPC(dex_pc);
1787
1788 llvm::Value* loaded_storage_object_addr =
1789 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1790
1791 EmitGuard_ExceptionLandingPad(dex_pc);
1792
1793 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1794
1795 irb_.CreateBr(block_cont);
1796
1797 // Now the class object must be loaded
1798 irb_.SetInsertPoint(block_cont);
1799
1800 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1801
1802 phi->addIncoming(storage_object_addr, block_original);
1803 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1804
1805 return phi;
1806}
1807
1808llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1809 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001810 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1811 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1812
1813 int field_offset;
1814 int ssb_index;
1815 bool is_referrers_class;
1816 bool is_volatile;
1817
Ian Rogers1212a022013-03-04 10:48:41 -08001818 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001819 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001820 is_referrers_class, is_volatile, false);
1821
1822 llvm::Value* static_field_value;
1823
1824 if (!is_fast_path) {
1825 llvm::Function* runtime_func;
1826
1827 if (field_jty == kObject) {
1828 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1829 } else if (field_jty == kLong || field_jty == kDouble) {
1830 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1831 } else {
1832 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1833 }
1834
1835 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1836
1837 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1838
1839 EmitUpdateDexPC(dex_pc);
1840
1841 static_field_value =
1842 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1843
1844 EmitGuard_ExceptionLandingPad(dex_pc);
1845
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001846 if (field_jty == kFloat || field_jty == kDouble) {
1847 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1848 }
TDYa1275a26d442012-07-26 18:58:38 -07001849 } else {
1850 DCHECK_GE(field_offset, 0);
1851
1852 llvm::Value* static_storage_addr = NULL;
1853
1854 if (is_referrers_class) {
1855 // Fast path, static storage base is this method's class
1856 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1857
1858 static_storage_addr =
1859 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001860 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001861 irb_.getJObjectTy(),
1862 kTBAAConstJObject);
1863 } else {
1864 // Medium path, static storage base in a different class which
1865 // requires checks that the other class is initialized
1866 DCHECK_GE(ssb_index, 0);
1867 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1868 }
1869
1870 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1871
1872 llvm::Value* static_field_addr =
1873 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001874 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001875
TDYa1275a26d442012-07-26 18:58:38 -07001876 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001877 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001878
1879 if (is_volatile) {
1880 irb_.CreateMemoryBarrier(art::kLoadLoad);
1881 }
TDYa1275a26d442012-07-26 18:58:38 -07001882 }
1883
TDYa1275a26d442012-07-26 18:58:38 -07001884 return static_field_value;
1885}
1886
1887void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1888 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001889 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1890 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1891 llvm::Value* new_value = call_inst.getArgOperand(1);
1892
1893 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001894 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001895 }
1896
1897 int field_offset;
1898 int ssb_index;
1899 bool is_referrers_class;
1900 bool is_volatile;
1901
Ian Rogers1212a022013-03-04 10:48:41 -08001902 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001903 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001904 is_referrers_class, is_volatile, true);
1905
1906 if (!is_fast_path) {
1907 llvm::Function* runtime_func;
1908
1909 if (field_jty == kObject) {
1910 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1911 } else if (field_jty == kLong || field_jty == kDouble) {
1912 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1913 } else {
1914 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1915 }
1916
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001917 if (field_jty == kFloat) {
1918 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1919 } else if (field_jty == kDouble) {
1920 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1921 }
1922
TDYa1275a26d442012-07-26 18:58:38 -07001923 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1924
1925 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1926
1927 EmitUpdateDexPC(dex_pc);
1928
1929 irb_.CreateCall3(runtime_func, field_idx_value,
1930 method_object_addr, new_value);
1931
1932 EmitGuard_ExceptionLandingPad(dex_pc);
1933
1934 } else {
1935 DCHECK_GE(field_offset, 0);
1936
1937 llvm::Value* static_storage_addr = NULL;
1938
1939 if (is_referrers_class) {
1940 // Fast path, static storage base is this method's class
1941 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1942
1943 static_storage_addr =
1944 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001945 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001946 irb_.getJObjectTy(),
1947 kTBAAConstJObject);
1948 } else {
1949 // Medium path, static storage base in a different class which
1950 // requires checks that the other class is initialized
1951 DCHECK_GE(ssb_index, 0);
1952 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1953 }
1954
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001955 if (is_volatile) {
1956 irb_.CreateMemoryBarrier(art::kStoreStore);
1957 }
1958
TDYa1275a26d442012-07-26 18:58:38 -07001959 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1960
1961 llvm::Value* static_field_addr =
1962 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001963 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001964
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001965 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001966 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1967
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001968 if (is_volatile) {
1969 irb_.CreateMemoryBarrier(art::kStoreLoad);
1970 }
1971
TDYa1275a26d442012-07-26 18:58:38 -07001972 if (field_jty == kObject) { // If put an object, mark the GC card table.
1973 EmitMarkGCCard(new_value, static_storage_addr);
1974 }
1975 }
1976
1977 return;
1978}
1979
TDYa127f71bf5a2012-07-29 20:09:52 -07001980llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001981 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1982 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
1983
1984 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1985
TDYa127ce4cc0d2012-11-18 16:59:53 -08001986 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001987
Ian Rogers89756f22013-03-04 16:40:02 -08001988 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
1989 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001990 llvm::BasicBlock* block_str_exist =
1991 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1992
1993 llvm::BasicBlock* block_str_resolve =
1994 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1995
1996 llvm::BasicBlock* block_cont =
1997 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
1998
1999 // Test: Is the string resolved and in the dex cache?
2000 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2001
2002 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2003
2004 // String is resolved, go to next basic block.
2005 irb_.SetInsertPoint(block_str_exist);
2006 irb_.CreateBr(block_cont);
2007
2008 // String is not resolved yet, resolve it now.
2009 irb_.SetInsertPoint(block_str_resolve);
2010
2011 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2012
2013 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2014
2015 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2016
2017 EmitUpdateDexPC(dex_pc);
2018
2019 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2020 string_idx_value);
2021
2022 EmitGuard_ExceptionLandingPad(dex_pc);
2023
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002024 irb_.CreateBr(block_cont);
2025
2026
TDYa127f71bf5a2012-07-29 20:09:52 -07002027 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2028
2029 irb_.SetInsertPoint(block_cont);
2030
2031 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2032
2033 phi->addIncoming(string_addr, block_str_exist);
2034 phi->addIncoming(result, block_pre_cont);
2035
2036 string_addr = phi;
2037 }
2038
2039 return string_addr;
2040}
2041
2042llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002043 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2044 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2045
2046 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2047
2048 return type_object_addr;
2049}
2050
2051void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002052 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2053 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002054 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002055
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002056 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002057
TDYa127ce4cc0d2012-11-18 16:59:53 -08002058 EmitUpdateDexPC(dex_pc);
2059
TDYa127f71bf5a2012-07-29 20:09:52 -07002060 irb_.Runtime().EmitLockObject(object_addr);
2061
2062 return;
2063}
2064
2065void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002066 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2067 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002068 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002069
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002070 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002071
2072 EmitUpdateDexPC(dex_pc);
2073
2074 irb_.Runtime().EmitUnlockObject(object_addr);
2075
2076 EmitGuard_ExceptionLandingPad(dex_pc);
2077
2078 return;
2079}
2080
2081void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002082 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2083 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2084 llvm::Value* object_addr = call_inst.getArgOperand(1);
2085
2086 llvm::BasicBlock* block_test_class =
2087 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2088
2089 llvm::BasicBlock* block_test_sub_class =
2090 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2091
2092 llvm::BasicBlock* block_cont =
2093 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2094
2095 // Test: Is the reference equal to null? Act as no-op when it is null.
2096 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2097
Ian Rogers8e696052013-03-04 09:00:40 -08002098 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002099
2100 // Test: Is the object instantiated from the given class?
2101 irb_.SetInsertPoint(block_test_class);
2102 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002103 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002104
2105 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2106
2107 llvm::Value* object_type_field_addr =
2108 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2109
2110 llvm::Value* object_type_object_addr =
2111 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2112
2113 llvm::Value* equal_class =
2114 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2115
Ian Rogers8e696052013-03-04 09:00:40 -08002116 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002117
2118 // Test: Is the object instantiated from the subclass of the given class?
2119 irb_.SetInsertPoint(block_test_sub_class);
2120
2121 EmitUpdateDexPC(dex_pc);
2122
2123 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2124 type_object_addr, object_type_object_addr);
2125
2126 EmitGuard_ExceptionLandingPad(dex_pc);
2127
2128 irb_.CreateBr(block_cont);
2129
2130 irb_.SetInsertPoint(block_cont);
2131
2132 return;
2133}
2134
2135llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002136 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2137 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2138 llvm::Value* object_addr = call_inst.getArgOperand(1);
2139
2140 llvm::BasicBlock* block_nullp =
2141 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2142
2143 llvm::BasicBlock* block_test_class =
2144 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2145
2146 llvm::BasicBlock* block_class_equals =
2147 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2148
2149 llvm::BasicBlock* block_test_sub_class =
2150 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2151
2152 llvm::BasicBlock* block_cont =
2153 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2154
2155 // Overview of the following code :
2156 // We check for null, if so, then false, otherwise check for class == . If so
2157 // then true, otherwise do callout slowpath.
2158 //
2159 // Test: Is the reference equal to null? Set 0 when it is null.
2160 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2161
Ian Rogers8e696052013-03-04 09:00:40 -08002162 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002163
2164 irb_.SetInsertPoint(block_nullp);
2165 irb_.CreateBr(block_cont);
2166
2167 // Test: Is the object instantiated from the given class?
2168 irb_.SetInsertPoint(block_test_class);
2169 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002170 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002171
2172 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2173
2174 llvm::Value* object_type_field_addr =
2175 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2176
2177 llvm::Value* object_type_object_addr =
2178 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2179
2180 llvm::Value* equal_class =
2181 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2182
Ian Rogers8e696052013-03-04 09:00:40 -08002183 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002184
2185 irb_.SetInsertPoint(block_class_equals);
2186 irb_.CreateBr(block_cont);
2187
2188 // Test: Is the object instantiated from the subclass of the given class?
2189 irb_.SetInsertPoint(block_test_sub_class);
2190 llvm::Value* result =
2191 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2192 type_object_addr, object_type_object_addr);
2193 irb_.CreateBr(block_cont);
2194
2195 irb_.SetInsertPoint(block_cont);
2196
2197 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2198
2199 phi->addIncoming(irb_.getJInt(0), block_nullp);
2200 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2201 phi->addIncoming(result, block_test_sub_class);
2202
2203 return phi;
2204}
2205
2206llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002207 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2208 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2209
2210 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002211 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2212 *dex_compilation_unit_->GetDexFile(),
2213 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002214 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2215 } else {
2216 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2217 }
2218
2219 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2220
2221 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2222
2223 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2224
2225 EmitUpdateDexPC(dex_pc);
2226
2227 llvm::Value* object_addr =
2228 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2229
2230 EmitGuard_ExceptionLandingPad(dex_pc);
2231
2232 return object_addr;
2233}
2234
2235llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002236 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2237 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002238
2239 if (!is_static) {
2240 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002241 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2242 llvm::Value* this_addr = call_inst.getArgOperand(3);
2243 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2244
2245 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002246 }
2247
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002248 llvm::Value* result = NULL;
2249 if (EmitIntrinsic(call_inst, &result)) {
2250 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002251 }
2252
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002253 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002254}
2255
2256llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002257 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2258 // Get the array object address
2259 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002260 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002261
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002262 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002263
2264 // Get the array length and store it to the register
2265 return EmitLoadArrayLength(array_addr);
2266}
2267
2268llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002269 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2270 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2271 llvm::Value* length = call_inst.getArgOperand(1);
2272
2273 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2274}
2275
2276llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002277 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2278 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2279 uint32_t length = call_inst.getNumArgOperands() - 3;
2280
2281 llvm::Value* object_addr =
2282 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2283
2284 if (length > 0) {
2285 // Check for the element type
2286 uint32_t type_desc_len = 0;
2287 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002288 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002289
2290 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2291 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2292 bool is_elem_int_ty = (type_desc[1] == 'I');
2293
2294 uint32_t alignment;
2295 llvm::Constant* elem_size;
2296 llvm::PointerType* field_type;
2297
2298 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2299 // as the element, thus we are only checking 2 cases: primitive int and
2300 // non-primitive type.
2301 if (is_elem_int_ty) {
2302 alignment = sizeof(int32_t);
2303 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2304 field_type = irb_.getJIntTy()->getPointerTo();
2305 } else {
2306 alignment = irb_.getSizeOfPtrEquivInt();
2307 elem_size = irb_.getSizeOfPtrEquivIntValue();
2308 field_type = irb_.getJObjectTy()->getPointerTo();
2309 }
2310
2311 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002312 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002313
2314 llvm::Value* data_field_addr =
2315 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2316
2317 // TODO: Tune this code. Currently we are generating one instruction for
2318 // one element which may be very space consuming. Maybe changing to use
2319 // memcpy may help; however, since we can't guarantee that the alloca of
2320 // dalvik register are continuous, we can't perform such optimization yet.
2321 for (uint32_t i = 0; i < length; ++i) {
2322 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2323
2324 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2325
2326 data_field_addr =
2327 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2328 }
2329 }
2330
2331 return object_addr;
2332}
2333
2334void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002335 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2336 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2337 LV2SInt(call_inst.getArgOperand(0));
2338 llvm::Value* array_addr = call_inst.getArgOperand(1);
2339
TDYa127920be7c2012-09-10 17:13:22 -07002340 const art::Instruction::ArrayDataPayload* payload =
2341 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002342 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002343
2344 if (payload->element_count == 0) {
2345 // When the number of the elements in the payload is zero, we don't have
2346 // to copy any numbers. However, we should check whether the array object
2347 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002348 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002349 } else {
2350 // To save the code size, we are going to call the runtime function to
2351 // copy the content from DexFile.
2352
2353 // NOTE: We will check for the NullPointerException in the runtime.
2354
2355 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2356
2357 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2358
2359 EmitUpdateDexPC(dex_pc);
2360
2361 irb_.CreateCall4(runtime_func,
2362 method_object_addr, irb_.getInt32(dex_pc),
2363 array_addr, irb_.getInt32(payload_offset));
2364
2365 EmitGuard_ExceptionLandingPad(dex_pc);
2366 }
2367
2368 return;
2369}
2370
2371llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2372 llvm::Value* array_length_value,
2373 uint32_t type_idx,
2374 bool is_filled_new_array) {
2375 llvm::Function* runtime_func;
2376
2377 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002378 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2379 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002380
2381
2382 if (is_filled_new_array) {
2383 runtime_func = skip_access_check ?
2384 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2385 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2386 } else {
2387 runtime_func = skip_access_check ?
2388 irb_.GetRuntime(runtime_support::AllocArray) :
2389 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2390 }
2391
2392 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2393
2394 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2395
2396 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2397
2398 EmitUpdateDexPC(dex_pc);
2399
2400 llvm::Value* object_addr =
2401 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2402 array_length_value, thread_object_addr);
2403
2404 EmitGuard_ExceptionLandingPad(dex_pc);
2405
2406 return object_addr;
2407}
2408
2409llvm::Value* GBCExpanderPass::
2410EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002411 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002412 llvm::Value* this_addr,
2413 uint32_t dex_pc,
2414 bool is_fast_path) {
2415
2416 llvm::Function* runtime_func = NULL;
2417
2418 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002419 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002420 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2421 break;
2422
TDYa127920be7c2012-09-10 17:13:22 -07002423 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002424 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2425 break;
2426
TDYa127920be7c2012-09-10 17:13:22 -07002427 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002428 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2429 break;
2430
TDYa127920be7c2012-09-10 17:13:22 -07002431 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002432 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2433 break;
2434
TDYa127920be7c2012-09-10 17:13:22 -07002435 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002436 if (is_fast_path) {
2437 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2438 } else {
2439 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2440 }
2441 break;
2442 }
2443
2444 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2445
2446 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002447 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002448 this_addr = irb_.getJNull();
2449 }
2450
2451 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2452
2453 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2454
2455 EmitUpdateDexPC(dex_pc);
2456
2457 llvm::Value* callee_method_object_addr =
2458 irb_.CreateCall4(runtime_func,
2459 callee_method_idx_value,
2460 this_addr,
2461 caller_method_object_addr,
2462 thread_object_addr);
2463
2464 EmitGuard_ExceptionLandingPad(dex_pc);
2465
2466 return callee_method_object_addr;
2467}
2468
TDYa1275e869b62012-07-25 00:45:39 -07002469void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2470 // Using runtime support, let the target can override by InlineAssembly.
2471 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2472}
2473
2474void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002475 if (shadow_frame_ == NULL) {
2476 return;
2477 }
TDYa1275e869b62012-07-25 00:45:39 -07002478 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002479 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002480 irb_.getInt32(dex_pc),
2481 kTBAAShadowFrame);
2482}
2483
2484void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2485 llvm::Value* denominator,
2486 JType op_jty) {
2487 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2488
2489 llvm::Constant* zero = irb_.getJZero(op_jty);
2490
2491 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2492
2493 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2494
2495 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2496
2497 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2498
2499 irb_.SetInsertPoint(block_exception);
2500 EmitUpdateDexPC(dex_pc);
2501 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2502 EmitBranchExceptionLandingPad(dex_pc);
2503
2504 irb_.SetInsertPoint(block_continue);
2505}
2506
2507void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002508 llvm::Value* object,
2509 int opt_flags) {
2510 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2511 if (ignore_null_check) {
2512 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2513 if (lpad) {
2514 // There is at least one catch: create a "fake" conditional branch to
2515 // keep the exception edge to the catch block.
2516 landing_pad_phi_mapping_[lpad].push_back(
2517 std::make_pair(current_bb_->getUniquePredecessor(),
2518 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002519
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002520 llvm::BasicBlock* block_continue =
2521 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002522
Ian Rogers8e696052013-03-04 09:00:40 -08002523 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002524
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002525 irb_.SetInsertPoint(block_continue);
2526 }
2527 } else {
2528 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002529
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002530 llvm::BasicBlock* block_exception =
2531 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002532
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002533 llvm::BasicBlock* block_continue =
2534 CreateBasicBlockWithDexPC(dex_pc, "cont");
2535
2536 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2537
2538 irb_.SetInsertPoint(block_exception);
2539 EmitUpdateDexPC(dex_pc);
2540 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2541 irb_.getInt32(dex_pc));
2542 EmitBranchExceptionLandingPad(dex_pc);
2543
2544 irb_.SetInsertPoint(block_continue);
2545 }
TDYa1275e869b62012-07-25 00:45:39 -07002546}
2547
2548void
2549GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2550 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002551 llvm::Value* index,
2552 int opt_flags) {
2553 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2554 if (ignore_range_check) {
2555 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2556 if (lpad) {
2557 // There is at least one catch: create a "fake" conditional branch to
2558 // keep the exception edge to the catch block.
2559 landing_pad_phi_mapping_[lpad].push_back(
2560 std::make_pair(current_bb_->getUniquePredecessor(),
2561 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002562
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002563 llvm::BasicBlock* block_continue =
2564 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002565
Ian Rogers8e696052013-03-04 09:00:40 -08002566 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002567
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002568 irb_.SetInsertPoint(block_continue);
2569 }
2570 } else {
2571 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002572
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002573 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002574
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002575 llvm::BasicBlock* block_exception =
2576 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002577
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002578 llvm::BasicBlock* block_continue =
2579 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002580
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002581 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2582
2583 irb_.SetInsertPoint(block_exception);
2584
2585 EmitUpdateDexPC(dex_pc);
2586 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2587 EmitBranchExceptionLandingPad(dex_pc);
2588
2589 irb_.SetInsertPoint(block_continue);
2590 }
TDYa1275e869b62012-07-25 00:45:39 -07002591}
2592
Ian Rogers8e696052013-03-04 09:00:40 -08002593llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002594 bool is_static) {
2595 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002596 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002597 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002598
2599 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002600 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002601 CHECK_GE(shorty_size, 1u);
2602
TDYa1275e869b62012-07-25 00:45:39 -07002603 // Get argument type
2604 std::vector<llvm::Type*> args_type;
2605
2606 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2607
2608 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002609 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002610 }
2611
2612 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002613 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002614 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002615 }
2616
2617 return llvm::FunctionType::get(ret_type, args_type, false);
2618}
2619
2620
2621llvm::BasicBlock* GBCExpanderPass::
2622CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2623 std::string name;
2624
2625#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002626 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002627#endif
2628
2629 return llvm::BasicBlock::Create(context_, name, func_);
2630}
2631
2632llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002633 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002634 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002635 return basic_blocks_[dex_pc];
2636}
2637
2638int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2639 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002640 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002641
2642 while (min <= max) {
2643 int32_t mid = min + (max - min) / 2;
2644
Ian Rogers89756f22013-03-04 16:40:02 -08002645 const art::DexFile::TryItem* ti =
2646 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002647 uint32_t start = ti->start_addr_;
2648 uint32_t end = start + ti->insn_count_;
2649
2650 if (dex_pc < start) {
2651 max = mid - 1;
2652 } else if (dex_pc >= end) {
2653 min = mid + 1;
2654 } else {
2655 return mid; // found
2656 }
2657 }
2658
2659 return -1; // not found
2660}
2661
2662llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2663 // Find the try item for this address in this method
2664 int32_t ti_offset = GetTryItemOffset(dex_pc);
2665
2666 if (ti_offset == -1) {
2667 return NULL; // No landing pad is available for this address.
2668 }
2669
2670 // Check for the existing landing pad basic block
2671 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2672 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2673
2674 if (block_lpad) {
2675 // We have generated landing pad for this try item already. Return the
2676 // same basic block.
2677 return block_lpad;
2678 }
2679
2680 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002681 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002682 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002683
2684 std::string lpadname;
2685
2686#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002687 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002688#endif
2689
2690 // Create landing pad basic block
2691 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2692
2693 // Change IRBuilder insert point
2694 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2695 irb_.SetInsertPoint(block_lpad);
2696
2697 // Find catch block with matching type
2698 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2699
2700 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2701
2702 llvm::Value* catch_handler_index_value =
2703 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2704 method_object_addr, ti_offset_value);
2705
2706 // Switch instruction (Go to unwind basic block by default)
2707 llvm::SwitchInst* sw =
2708 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2709
2710 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002711 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002712
2713 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2714 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2715 }
2716
2717 // Restore the orignal insert point for IRBuilder
2718 irb_.restoreIP(irb_ip_original);
2719
2720 // Cache this landing pad
2721 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2722 basic_block_landing_pads_[ti_offset] = block_lpad;
2723
2724 return block_lpad;
2725}
2726
2727llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2728 // Check the existing unwinding baisc block block
2729 if (basic_block_unwind_ != NULL) {
2730 return basic_block_unwind_;
2731 }
2732
2733 // Create new basic block for unwinding
2734 basic_block_unwind_ =
2735 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2736
2737 // Change IRBuilder insert point
2738 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2739 irb_.SetInsertPoint(basic_block_unwind_);
2740
2741 // Pop the shadow frame
2742 Expand_PopShadowFrame();
2743
2744 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002745 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002746 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002747 if (ret_shorty == 'V') {
2748 irb_.CreateRetVoid();
2749 } else {
2750 irb_.CreateRet(irb_.getJZero(ret_shorty));
2751 }
2752
2753 // Restore the orignal insert point for IRBuilder
2754 irb_.restoreIP(irb_ip_original);
2755
2756 return basic_block_unwind_;
2757}
2758
2759void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2760 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002761 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002762 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002763 irb_.CreateBr(lpad);
2764 } else {
2765 irb_.CreateBr(GetUnwindBasicBlock());
2766 }
2767}
2768
2769void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002770 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2771
TDYa1275e869b62012-07-25 00:45:39 -07002772 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2773
2774 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002775 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002776 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002777 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002778 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002779 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002780 }
2781
2782 irb_.SetInsertPoint(block_cont);
2783}
2784
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002785llvm::Value*
2786GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2787 llvm::CallInst& call_inst) {
2788 switch (intr_id) {
2789 //==- Thread -----------------------------------------------------------==//
2790 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002791 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002792 }
Logan Chien75e4b602012-07-23 14:24:12 -07002793 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002794 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002795 return NULL;
2796 }
2797 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002798 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002799 return NULL;
2800 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002801 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002802 Expand_MarkGCCard(call_inst);
2803 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002804 }
Logan Chien75e4b602012-07-23 14:24:12 -07002805
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002806 //==- Exception --------------------------------------------------------==//
2807 case IntrinsicHelper::ThrowException: {
2808 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2809 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002810 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002811 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2812
2813 EmitUpdateDexPC(dex_pc);
2814
2815 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2816 call_inst.getArgOperand(0));
2817
2818 EmitGuard_ExceptionLandingPad(dex_pc);
2819 return NULL;
2820 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002821 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002822 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002823 }
2824 case IntrinsicHelper::IsExceptionPending: {
2825 return irb_.Runtime().EmitIsExceptionPending();
2826 }
2827 case IntrinsicHelper::FindCatchBlock: {
2828 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2829 }
2830 case IntrinsicHelper::ThrowDivZeroException: {
2831 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2832 }
2833 case IntrinsicHelper::ThrowNullPointerException: {
2834 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2835 }
2836 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2837 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2838 }
Logan Chien75e4b602012-07-23 14:24:12 -07002839
2840 //==- Const String -----------------------------------------------------==//
2841 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002842 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002843 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002844 case IntrinsicHelper::LoadStringFromDexCache: {
2845 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2846 }
2847 case IntrinsicHelper::ResolveString: {
2848 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2849 }
Logan Chien75e4b602012-07-23 14:24:12 -07002850
2851 //==- Const Class ------------------------------------------------------==//
2852 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002853 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002854 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002855 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2856 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2857 }
2858 case IntrinsicHelper::LoadTypeFromDexCache: {
2859 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2860 }
2861 case IntrinsicHelper::InitializeType: {
2862 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2863 }
Logan Chien75e4b602012-07-23 14:24:12 -07002864
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002865 //==- Lock -------------------------------------------------------------==//
2866 case IntrinsicHelper::LockObject: {
2867 Expand_LockObject(call_inst.getArgOperand(0));
2868 return NULL;
2869 }
2870 case IntrinsicHelper::UnlockObject: {
2871 Expand_UnlockObject(call_inst.getArgOperand(0));
2872 return NULL;
2873 }
Logan Chien75e4b602012-07-23 14:24:12 -07002874
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002875 //==- Cast -------------------------------------------------------------==//
2876 case IntrinsicHelper::CheckCast: {
2877 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2878 }
Logan Chien75e4b602012-07-23 14:24:12 -07002879 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002880 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002881 return NULL;
2882 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002883 case IntrinsicHelper::IsAssignable: {
2884 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2885 }
Logan Chien75e4b602012-07-23 14:24:12 -07002886
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002887 //==- Alloc ------------------------------------------------------------==//
2888 case IntrinsicHelper::AllocObject: {
2889 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2890 }
2891 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2892 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2893 }
Logan Chien75e4b602012-07-23 14:24:12 -07002894
2895 //==- Instance ---------------------------------------------------------==//
2896 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002897 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002898 }
2899 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002900 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002901 }
2902
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002903 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002904 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002905 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002906 }
2907 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002908 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002909 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002910 case IntrinsicHelper::ArrayLength: {
2911 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2912 }
2913 case IntrinsicHelper::AllocArray: {
2914 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2915 }
2916 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2917 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2918 call_inst);
2919 }
2920 case IntrinsicHelper::CheckAndAllocArray: {
2921 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2922 }
2923 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2924 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2925 call_inst);
2926 }
2927 case IntrinsicHelper::ArrayGet: {
2928 return Expand_ArrayGet(call_inst.getArgOperand(0),
2929 call_inst.getArgOperand(1),
2930 kInt);
2931 }
2932 case IntrinsicHelper::ArrayGetWide: {
2933 return Expand_ArrayGet(call_inst.getArgOperand(0),
2934 call_inst.getArgOperand(1),
2935 kLong);
2936 }
2937 case IntrinsicHelper::ArrayGetObject: {
2938 return Expand_ArrayGet(call_inst.getArgOperand(0),
2939 call_inst.getArgOperand(1),
2940 kObject);
2941 }
2942 case IntrinsicHelper::ArrayGetBoolean: {
2943 return Expand_ArrayGet(call_inst.getArgOperand(0),
2944 call_inst.getArgOperand(1),
2945 kBoolean);
2946 }
2947 case IntrinsicHelper::ArrayGetByte: {
2948 return Expand_ArrayGet(call_inst.getArgOperand(0),
2949 call_inst.getArgOperand(1),
2950 kByte);
2951 }
2952 case IntrinsicHelper::ArrayGetChar: {
2953 return Expand_ArrayGet(call_inst.getArgOperand(0),
2954 call_inst.getArgOperand(1),
2955 kChar);
2956 }
2957 case IntrinsicHelper::ArrayGetShort: {
2958 return Expand_ArrayGet(call_inst.getArgOperand(0),
2959 call_inst.getArgOperand(1),
2960 kShort);
2961 }
2962 case IntrinsicHelper::ArrayPut: {
2963 Expand_ArrayPut(call_inst.getArgOperand(0),
2964 call_inst.getArgOperand(1),
2965 call_inst.getArgOperand(2),
2966 kInt);
2967 return NULL;
2968 }
2969 case IntrinsicHelper::ArrayPutWide: {
2970 Expand_ArrayPut(call_inst.getArgOperand(0),
2971 call_inst.getArgOperand(1),
2972 call_inst.getArgOperand(2),
2973 kLong);
2974 return NULL;
2975 }
2976 case IntrinsicHelper::ArrayPutObject: {
2977 Expand_ArrayPut(call_inst.getArgOperand(0),
2978 call_inst.getArgOperand(1),
2979 call_inst.getArgOperand(2),
2980 kObject);
2981 return NULL;
2982 }
2983 case IntrinsicHelper::ArrayPutBoolean: {
2984 Expand_ArrayPut(call_inst.getArgOperand(0),
2985 call_inst.getArgOperand(1),
2986 call_inst.getArgOperand(2),
2987 kBoolean);
2988 return NULL;
2989 }
2990 case IntrinsicHelper::ArrayPutByte: {
2991 Expand_ArrayPut(call_inst.getArgOperand(0),
2992 call_inst.getArgOperand(1),
2993 call_inst.getArgOperand(2),
2994 kByte);
2995 return NULL;
2996 }
2997 case IntrinsicHelper::ArrayPutChar: {
2998 Expand_ArrayPut(call_inst.getArgOperand(0),
2999 call_inst.getArgOperand(1),
3000 call_inst.getArgOperand(2),
3001 kChar);
3002 return NULL;
3003 }
3004 case IntrinsicHelper::ArrayPutShort: {
3005 Expand_ArrayPut(call_inst.getArgOperand(0),
3006 call_inst.getArgOperand(1),
3007 call_inst.getArgOperand(2),
3008 kShort);
3009 return NULL;
3010 }
3011 case IntrinsicHelper::CheckPutArrayElement: {
3012 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3013 }
3014 case IntrinsicHelper::FilledNewArray: {
3015 Expand_FilledNewArray(call_inst);
3016 return NULL;
3017 }
3018 case IntrinsicHelper::FillArrayData: {
3019 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3020 }
Logan Chien75e4b602012-07-23 14:24:12 -07003021 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003022 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003023 return NULL;
3024 }
3025 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003026 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003027 }
3028
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003029 //==- Instance Field ---------------------------------------------------==//
3030 case IntrinsicHelper::InstanceFieldGet:
3031 case IntrinsicHelper::InstanceFieldGetBoolean:
3032 case IntrinsicHelper::InstanceFieldGetByte:
3033 case IntrinsicHelper::InstanceFieldGetChar:
3034 case IntrinsicHelper::InstanceFieldGetShort: {
3035 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3036 }
3037 case IntrinsicHelper::InstanceFieldGetWide: {
3038 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3039 }
3040 case IntrinsicHelper::InstanceFieldGetObject: {
3041 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3042 }
3043 case IntrinsicHelper::InstanceFieldGetFast: {
3044 return Expand_IGetFast(call_inst.getArgOperand(0),
3045 call_inst.getArgOperand(1),
3046 call_inst.getArgOperand(2),
3047 kInt);
3048 }
3049 case IntrinsicHelper::InstanceFieldGetWideFast: {
3050 return Expand_IGetFast(call_inst.getArgOperand(0),
3051 call_inst.getArgOperand(1),
3052 call_inst.getArgOperand(2),
3053 kLong);
3054 }
3055 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3056 return Expand_IGetFast(call_inst.getArgOperand(0),
3057 call_inst.getArgOperand(1),
3058 call_inst.getArgOperand(2),
3059 kObject);
3060 }
3061 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3062 return Expand_IGetFast(call_inst.getArgOperand(0),
3063 call_inst.getArgOperand(1),
3064 call_inst.getArgOperand(2),
3065 kBoolean);
3066 }
3067 case IntrinsicHelper::InstanceFieldGetByteFast: {
3068 return Expand_IGetFast(call_inst.getArgOperand(0),
3069 call_inst.getArgOperand(1),
3070 call_inst.getArgOperand(2),
3071 kByte);
3072 }
3073 case IntrinsicHelper::InstanceFieldGetCharFast: {
3074 return Expand_IGetFast(call_inst.getArgOperand(0),
3075 call_inst.getArgOperand(1),
3076 call_inst.getArgOperand(2),
3077 kChar);
3078 }
3079 case IntrinsicHelper::InstanceFieldGetShortFast: {
3080 return Expand_IGetFast(call_inst.getArgOperand(0),
3081 call_inst.getArgOperand(1),
3082 call_inst.getArgOperand(2),
3083 kShort);
3084 }
3085 case IntrinsicHelper::InstanceFieldPut:
3086 case IntrinsicHelper::InstanceFieldPutBoolean:
3087 case IntrinsicHelper::InstanceFieldPutByte:
3088 case IntrinsicHelper::InstanceFieldPutChar:
3089 case IntrinsicHelper::InstanceFieldPutShort: {
3090 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3091 }
3092 case IntrinsicHelper::InstanceFieldPutWide: {
3093 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3094 }
3095 case IntrinsicHelper::InstanceFieldPutObject: {
3096 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3097 }
3098 case IntrinsicHelper::InstanceFieldPutFast: {
3099 Expand_IPutFast(call_inst.getArgOperand(0),
3100 call_inst.getArgOperand(1),
3101 call_inst.getArgOperand(2),
3102 call_inst.getArgOperand(3),
3103 kInt);
3104 return NULL;
3105 }
3106 case IntrinsicHelper::InstanceFieldPutWideFast: {
3107 Expand_IPutFast(call_inst.getArgOperand(0),
3108 call_inst.getArgOperand(1),
3109 call_inst.getArgOperand(2),
3110 call_inst.getArgOperand(3),
3111 kLong);
3112 return NULL;
3113 }
3114 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3115 Expand_IPutFast(call_inst.getArgOperand(0),
3116 call_inst.getArgOperand(1),
3117 call_inst.getArgOperand(2),
3118 call_inst.getArgOperand(3),
3119 kObject);
3120 return NULL;
3121 }
3122 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3123 Expand_IPutFast(call_inst.getArgOperand(0),
3124 call_inst.getArgOperand(1),
3125 call_inst.getArgOperand(2),
3126 call_inst.getArgOperand(3),
3127 kBoolean);
3128 return NULL;
3129 }
3130 case IntrinsicHelper::InstanceFieldPutByteFast: {
3131 Expand_IPutFast(call_inst.getArgOperand(0),
3132 call_inst.getArgOperand(1),
3133 call_inst.getArgOperand(2),
3134 call_inst.getArgOperand(3),
3135 kByte);
3136 return NULL;
3137 }
3138 case IntrinsicHelper::InstanceFieldPutCharFast: {
3139 Expand_IPutFast(call_inst.getArgOperand(0),
3140 call_inst.getArgOperand(1),
3141 call_inst.getArgOperand(2),
3142 call_inst.getArgOperand(3),
3143 kChar);
3144 return NULL;
3145 }
3146 case IntrinsicHelper::InstanceFieldPutShortFast: {
3147 Expand_IPutFast(call_inst.getArgOperand(0),
3148 call_inst.getArgOperand(1),
3149 call_inst.getArgOperand(2),
3150 call_inst.getArgOperand(3),
3151 kShort);
3152 return NULL;
3153 }
Logan Chien75e4b602012-07-23 14:24:12 -07003154
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003155 //==- Static Field -----------------------------------------------------==//
3156 case IntrinsicHelper::StaticFieldGet:
3157 case IntrinsicHelper::StaticFieldGetBoolean:
3158 case IntrinsicHelper::StaticFieldGetByte:
3159 case IntrinsicHelper::StaticFieldGetChar:
3160 case IntrinsicHelper::StaticFieldGetShort: {
3161 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3162 }
3163 case IntrinsicHelper::StaticFieldGetWide: {
3164 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3165 }
3166 case IntrinsicHelper::StaticFieldGetObject: {
3167 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3168 }
3169 case IntrinsicHelper::StaticFieldGetFast: {
3170 return Expand_SGetFast(call_inst.getArgOperand(0),
3171 call_inst.getArgOperand(1),
3172 call_inst.getArgOperand(2),
3173 kInt);
3174 }
3175 case IntrinsicHelper::StaticFieldGetWideFast: {
3176 return Expand_SGetFast(call_inst.getArgOperand(0),
3177 call_inst.getArgOperand(1),
3178 call_inst.getArgOperand(2),
3179 kLong);
3180 }
3181 case IntrinsicHelper::StaticFieldGetObjectFast: {
3182 return Expand_SGetFast(call_inst.getArgOperand(0),
3183 call_inst.getArgOperand(1),
3184 call_inst.getArgOperand(2),
3185 kObject);
3186 }
3187 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3188 return Expand_SGetFast(call_inst.getArgOperand(0),
3189 call_inst.getArgOperand(1),
3190 call_inst.getArgOperand(2),
3191 kBoolean);
3192 }
3193 case IntrinsicHelper::StaticFieldGetByteFast: {
3194 return Expand_SGetFast(call_inst.getArgOperand(0),
3195 call_inst.getArgOperand(1),
3196 call_inst.getArgOperand(2),
3197 kByte);
3198 }
3199 case IntrinsicHelper::StaticFieldGetCharFast: {
3200 return Expand_SGetFast(call_inst.getArgOperand(0),
3201 call_inst.getArgOperand(1),
3202 call_inst.getArgOperand(2),
3203 kChar);
3204 }
3205 case IntrinsicHelper::StaticFieldGetShortFast: {
3206 return Expand_SGetFast(call_inst.getArgOperand(0),
3207 call_inst.getArgOperand(1),
3208 call_inst.getArgOperand(2),
3209 kShort);
3210 }
3211 case IntrinsicHelper::StaticFieldPut:
3212 case IntrinsicHelper::StaticFieldPutBoolean:
3213 case IntrinsicHelper::StaticFieldPutByte:
3214 case IntrinsicHelper::StaticFieldPutChar:
3215 case IntrinsicHelper::StaticFieldPutShort: {
3216 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3217 }
3218 case IntrinsicHelper::StaticFieldPutWide: {
3219 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3220 }
3221 case IntrinsicHelper::StaticFieldPutObject: {
3222 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3223 }
3224 case IntrinsicHelper::StaticFieldPutFast: {
3225 Expand_SPutFast(call_inst.getArgOperand(0),
3226 call_inst.getArgOperand(1),
3227 call_inst.getArgOperand(2),
3228 call_inst.getArgOperand(3),
3229 kInt);
3230 return NULL;
3231 }
3232 case IntrinsicHelper::StaticFieldPutWideFast: {
3233 Expand_SPutFast(call_inst.getArgOperand(0),
3234 call_inst.getArgOperand(1),
3235 call_inst.getArgOperand(2),
3236 call_inst.getArgOperand(3),
3237 kLong);
3238 return NULL;
3239 }
3240 case IntrinsicHelper::StaticFieldPutObjectFast: {
3241 Expand_SPutFast(call_inst.getArgOperand(0),
3242 call_inst.getArgOperand(1),
3243 call_inst.getArgOperand(2),
3244 call_inst.getArgOperand(3),
3245 kObject);
3246 return NULL;
3247 }
3248 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3249 Expand_SPutFast(call_inst.getArgOperand(0),
3250 call_inst.getArgOperand(1),
3251 call_inst.getArgOperand(2),
3252 call_inst.getArgOperand(3),
3253 kBoolean);
3254 return NULL;
3255 }
3256 case IntrinsicHelper::StaticFieldPutByteFast: {
3257 Expand_SPutFast(call_inst.getArgOperand(0),
3258 call_inst.getArgOperand(1),
3259 call_inst.getArgOperand(2),
3260 call_inst.getArgOperand(3),
3261 kByte);
3262 return NULL;
3263 }
3264 case IntrinsicHelper::StaticFieldPutCharFast: {
3265 Expand_SPutFast(call_inst.getArgOperand(0),
3266 call_inst.getArgOperand(1),
3267 call_inst.getArgOperand(2),
3268 call_inst.getArgOperand(3),
3269 kChar);
3270 return NULL;
3271 }
3272 case IntrinsicHelper::StaticFieldPutShortFast: {
3273 Expand_SPutFast(call_inst.getArgOperand(0),
3274 call_inst.getArgOperand(1),
3275 call_inst.getArgOperand(2),
3276 call_inst.getArgOperand(3),
3277 kShort);
3278 return NULL;
3279 }
3280 case IntrinsicHelper::LoadDeclaringClassSSB: {
3281 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3282 }
3283 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3284 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3285 }
3286 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3287 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3288 }
Logan Chien75e4b602012-07-23 14:24:12 -07003289
3290 //==- High-level Array -------------------------------------------------==//
3291 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003292 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003293 }
3294 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003295 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003296 }
3297 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003298 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003299 }
3300 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003301 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003302 }
3303 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003304 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003305 }
3306 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003307 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003308 }
3309 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003310 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003311 }
3312 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003313 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 }
3315 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003316 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003317 }
3318 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003319 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003320 return NULL;
3321 }
3322 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003323 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003324 return NULL;
3325 }
3326 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003327 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003328 return NULL;
3329 }
3330 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003331 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003332 return NULL;
3333 }
3334 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003335 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003336 return NULL;
3337 }
3338 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003339 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003340 return NULL;
3341 }
3342 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003343 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003344 return NULL;
3345 }
3346 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003347 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003348 return NULL;
3349 }
3350 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003351 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003352 return NULL;
3353 }
3354
3355 //==- High-level Instance ----------------------------------------------==//
3356 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003357 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003358 }
3359 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003360 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003361 }
3362 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003363 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003364 }
3365 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003366 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003367 }
3368 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003369 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003370 }
3371 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003372 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003373 }
3374 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003375 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003376 }
3377 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003378 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003379 }
3380 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003381 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003382 }
3383 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003384 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003385 return NULL;
3386 }
3387 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003388 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003389 return NULL;
3390 }
3391 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003392 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003393 return NULL;
3394 }
3395 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003396 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003397 return NULL;
3398 }
3399 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003400 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003401 return NULL;
3402 }
3403 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003404 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003405 return NULL;
3406 }
3407 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003408 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003409 return NULL;
3410 }
3411 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003412 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003413 return NULL;
3414 }
3415 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003416 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003417 return NULL;
3418 }
3419
3420 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003421 case IntrinsicHelper::HLInvokeVoid:
3422 case IntrinsicHelper::HLInvokeObj:
3423 case IntrinsicHelper::HLInvokeInt:
3424 case IntrinsicHelper::HLInvokeFloat:
3425 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003426 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003427 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003428 }
3429
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003430 //==- Invoke -----------------------------------------------------------==//
3431 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3432 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3433 }
3434 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3435 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3436 }
3437 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3438 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3439 }
3440 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3441 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3442 }
3443 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3444 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3445 }
3446 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3447 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3448 }
3449 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3450 return Expand_GetVirtualCalleeMethodObjAddrFast(
3451 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3452 }
3453 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3454 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3455 }
3456 case IntrinsicHelper::InvokeRetVoid:
3457 case IntrinsicHelper::InvokeRetBoolean:
3458 case IntrinsicHelper::InvokeRetByte:
3459 case IntrinsicHelper::InvokeRetChar:
3460 case IntrinsicHelper::InvokeRetShort:
3461 case IntrinsicHelper::InvokeRetInt:
3462 case IntrinsicHelper::InvokeRetLong:
3463 case IntrinsicHelper::InvokeRetFloat:
3464 case IntrinsicHelper::InvokeRetDouble:
3465 case IntrinsicHelper::InvokeRetObject: {
3466 return Expand_Invoke(call_inst);
3467 }
Logan Chien75e4b602012-07-23 14:24:12 -07003468
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003469 //==- Math -------------------------------------------------------------==//
3470 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003471 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003472 }
3473 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003474 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003475 }
3476 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003477 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003478 }
3479 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003480 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003481 }
3482 case IntrinsicHelper::D2L: {
3483 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3484 }
3485 case IntrinsicHelper::D2I: {
3486 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3487 }
3488 case IntrinsicHelper::F2L: {
3489 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3490 }
3491 case IntrinsicHelper::F2I: {
3492 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3493 }
Logan Chien75e4b602012-07-23 14:24:12 -07003494
3495 //==- High-level Static ------------------------------------------------==//
3496 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003497 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003498 }
3499 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003500 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003501 }
3502 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003503 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003504 }
3505 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003506 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003507 }
3508 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003509 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003510 }
3511 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003512 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003513 }
3514 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003515 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003516 }
3517 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003518 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003519 }
3520 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003521 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003522 }
3523 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003524 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 return NULL;
3526 }
3527 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003528 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003529 return NULL;
3530 }
3531 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003532 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003533 return NULL;
3534 }
3535 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003536 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003537 return NULL;
3538 }
3539 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003540 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003541 return NULL;
3542 }
3543 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003544 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003545 return NULL;
3546 }
3547 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003548 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003549 return NULL;
3550 }
3551 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003552 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003553 return NULL;
3554 }
3555 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003556 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003557 return NULL;
3558 }
3559
3560 //==- High-level Monitor -----------------------------------------------==//
3561 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003562 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003563 return NULL;
3564 }
3565 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003566 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003567 return NULL;
3568 }
3569
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003570 //==- Shadow Frame -----------------------------------------------------==//
3571 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003572 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003573 return NULL;
3574 }
TDYa1278e950c12012-11-02 09:58:19 -07003575 case IntrinsicHelper::SetVReg: {
3576 Expand_SetVReg(call_inst.getArgOperand(0),
3577 call_inst.getArgOperand(1));
3578 return NULL;
3579 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003580 case IntrinsicHelper::PopShadowFrame: {
3581 Expand_PopShadowFrame();
3582 return NULL;
3583 }
3584 case IntrinsicHelper::UpdateDexPC: {
3585 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3586 return NULL;
3587 }
TDYa127a1b21852012-07-23 03:20:39 -07003588
Logan Chien75e4b602012-07-23 14:24:12 -07003589 //==- Comparison -------------------------------------------------------==//
3590 case IntrinsicHelper::CmplFloat:
3591 case IntrinsicHelper::CmplDouble: {
3592 return Expand_FPCompare(call_inst.getArgOperand(0),
3593 call_inst.getArgOperand(1),
3594 false);
3595 }
3596 case IntrinsicHelper::CmpgFloat:
3597 case IntrinsicHelper::CmpgDouble: {
3598 return Expand_FPCompare(call_inst.getArgOperand(0),
3599 call_inst.getArgOperand(1),
3600 true);
3601 }
3602 case IntrinsicHelper::CmpLong: {
3603 return Expand_LongCompare(call_inst.getArgOperand(0),
3604 call_inst.getArgOperand(1));
3605 }
TDYa127a1b21852012-07-23 03:20:39 -07003606
Logan Chien75e4b602012-07-23 14:24:12 -07003607 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003608 case IntrinsicHelper::ConstInt:
3609 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003610 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003611 }
TDYa127920be7c2012-09-10 17:13:22 -07003612 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003613 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3614 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003615 }
TDYa127920be7c2012-09-10 17:13:22 -07003616 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003617 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3618 irb_.getJDoubleTy());
3619 }
TDYa127920be7c2012-09-10 17:13:22 -07003620 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003621 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3622 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003623 }
3624
3625 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003626 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003627 // Nothing to be done, because MethodInfo carries optional hints that are
3628 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003629 return NULL;
3630 }
3631
3632 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003633 case IntrinsicHelper::CopyInt:
3634 case IntrinsicHelper::CopyFloat:
3635 case IntrinsicHelper::CopyLong:
3636 case IntrinsicHelper::CopyDouble:
3637 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003638 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003639 }
3640
3641 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003642 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003643 return Expand_IntegerShift(call_inst.getArgOperand(0),
3644 call_inst.getArgOperand(1),
3645 kIntegerSHL, kLong);
3646 }
TDYa127920be7c2012-09-10 17:13:22 -07003647 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003648 return Expand_IntegerShift(call_inst.getArgOperand(0),
3649 call_inst.getArgOperand(1),
3650 kIntegerSHR, kLong);
3651 }
TDYa127920be7c2012-09-10 17:13:22 -07003652 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003653 return Expand_IntegerShift(call_inst.getArgOperand(0),
3654 call_inst.getArgOperand(1),
3655 kIntegerUSHR, kLong);
3656 }
TDYa127920be7c2012-09-10 17:13:22 -07003657 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003658 return Expand_IntegerShift(call_inst.getArgOperand(0),
3659 call_inst.getArgOperand(1),
3660 kIntegerSHL, kInt);
3661 }
TDYa127920be7c2012-09-10 17:13:22 -07003662 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003663 return Expand_IntegerShift(call_inst.getArgOperand(0),
3664 call_inst.getArgOperand(1),
3665 kIntegerSHR, kInt);
3666 }
TDYa127920be7c2012-09-10 17:13:22 -07003667 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003668 return Expand_IntegerShift(call_inst.getArgOperand(0),
3669 call_inst.getArgOperand(1),
3670 kIntegerUSHR, kInt);
3671 }
3672
3673 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003674 case IntrinsicHelper::IntToChar: {
3675 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3676 irb_.getJIntTy());
3677 }
3678 case IntrinsicHelper::IntToShort: {
3679 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3680 irb_.getJIntTy());
3681 }
3682 case IntrinsicHelper::IntToByte: {
3683 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3684 irb_.getJIntTy());
3685 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003686
TDYa12787caa7e2012-08-25 23:23:27 -07003687 //==- Exception --------------------------------------------------------==//
3688 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003689 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003690 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3691 CHECK(si != NULL);
3692 irb_.CreateBr(si->getDefaultDest());
3693 si->eraseFromParent();
3694 return call_inst.getArgOperand(0);
3695 }
3696
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003697 //==- Constructor barrier-----------------------------------------------==//
3698 case IntrinsicHelper::ConstructorBarrier: {
3699 irb_.CreateMemoryBarrier(art::kStoreStore);
3700 return NULL;
3701 }
3702
Logan Chien75e4b602012-07-23 14:24:12 -07003703 //==- Unknown Cases ----------------------------------------------------==//
3704 case IntrinsicHelper::MaxIntrinsicId:
3705 case IntrinsicHelper::UnknownId:
3706 //default:
3707 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3708 // give some warning on unmatched cases.
3709 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003710 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003711 }
Logan Chien75e4b602012-07-23 14:24:12 -07003712 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003713 return NULL;
3714}
3715
3716} // anonymous namespace
3717
3718namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003719namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003720
Ian Rogers4c1c2832013-03-04 18:30:13 -08003721::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003722CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003723 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003724 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003725}
3726
Ian Rogers4c1c2832013-03-04 18:30:13 -08003727} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003728} // namespace art