blob: 70c54ea839c01a41abf97b0ebde19bfa1070aa45 [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
Sebastien Hertzf11afa02013-03-19 17:39:29 +010082 // Maps each vreg to its shadow frame address.
83 std::vector<llvm::Value*> shadow_frame_vreg_addresses_;
84
Logan Chien67645d82012-08-17 09:10:54 +080085 bool changed_;
86
TDYa1275e869b62012-07-25 00:45:39 -070087 private:
Shih-wei Liao21d28f52012-06-12 05:55:00 -070088 //----------------------------------------------------------------------------
Logan Chien75e4b602012-07-23 14:24:12 -070089 // Constant for GBC expansion
90 //----------------------------------------------------------------------------
91 enum IntegerShiftKind {
92 kIntegerSHL,
93 kIntegerSHR,
94 kIntegerUSHR,
95 };
96
97 private:
98 //----------------------------------------------------------------------------
Shih-wei Liao21d28f52012-06-12 05:55:00 -070099 // Helper function for GBC expansion
100 //----------------------------------------------------------------------------
101
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700102 llvm::Value* ExpandToRuntime(runtime_support::RuntimeId rt,
103 llvm::CallInst& inst);
104
TDYa1275e869b62012-07-25 00:45:39 -0700105 uint64_t LV2UInt(llvm::Value* lv) {
106 return llvm::cast<llvm::ConstantInt>(lv)->getZExtValue();
107 }
108
109 int64_t LV2SInt(llvm::Value* lv) {
110 return llvm::cast<llvm::ConstantInt>(lv)->getSExtValue();
111 }
112
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700113 private:
114 // TODO: Almost all Emit* are directly copy-n-paste from MethodCompiler.
115 // Refactor these utility functions from MethodCompiler to avoid forking.
116
Logan Chien67645d82012-08-17 09:10:54 +0800117 void EmitStackOverflowCheck(llvm::Instruction* first_non_alloca);
118
119 void RewriteFunction();
120
121 void RewriteBasicBlock(llvm::BasicBlock* original_block);
122
123 void UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
124 llvm::BasicBlock* new_basic_block);
125
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700126
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800127 // Sign or zero extend category 1 types < 32bits in size to 32bits.
128 llvm::Value* SignOrZeroExtendCat1Types(llvm::Value* value, JType jty);
129
130 // Truncate category 1 types from 32bits to the given JType size.
131 llvm::Value* TruncateCat1Types(llvm::Value* value, JType jty);
132
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700133 //----------------------------------------------------------------------------
134 // Dex cache code generation helper function
135 //----------------------------------------------------------------------------
TDYa127920be7c2012-09-10 17:13:22 -0700136 llvm::Value* EmitLoadDexCacheAddr(art::MemberOffset dex_cache_offset);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700137
138 llvm::Value* EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx);
139
140 llvm::Value* EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx);
141
142 llvm::Value* EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx);
143
144 llvm::Value* EmitLoadDexCacheStringFieldAddr(uint32_t string_idx);
145
146 //----------------------------------------------------------------------------
147 // Code generation helper function
148 //----------------------------------------------------------------------------
149 llvm::Value* EmitLoadMethodObjectAddr();
150
151 llvm::Value* EmitLoadArrayLength(llvm::Value* array);
152
153 llvm::Value* EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx);
154
155 llvm::Value* EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
156 llvm::Value* this_addr);
157
158 llvm::Value* EmitArrayGEP(llvm::Value* array_addr,
159 llvm::Value* index_value,
160 JType elem_jty);
161
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100162 //----------------------------------------------------------------------------
163 // Invoke helper function
164 //----------------------------------------------------------------------------
165 llvm::Value* EmitInvoke(llvm::CallInst& call_inst);
166
167 //----------------------------------------------------------------------------
168 // Inlining helper functions
169 //----------------------------------------------------------------------------
170 bool EmitIntrinsic(llvm::CallInst& call_inst, llvm::Value** result);
171
172 bool EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
173 llvm::Value** result, bool is_empty);
174
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700175 private:
176 //----------------------------------------------------------------------------
177 // Expand Greenland intrinsics
178 //----------------------------------------------------------------------------
179 void Expand_TestSuspend(llvm::CallInst& call_inst);
180
TDYa1279a129452012-07-19 03:10:08 -0700181 void Expand_MarkGCCard(llvm::CallInst& call_inst);
182
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700183 llvm::Value* Expand_LoadStringFromDexCache(llvm::Value* string_idx_value);
184
185 llvm::Value* Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value);
186
187 void Expand_LockObject(llvm::Value* obj);
188
189 void Expand_UnlockObject(llvm::Value* obj);
190
191 llvm::Value* Expand_ArrayGet(llvm::Value* array_addr,
192 llvm::Value* index_value,
193 JType elem_jty);
194
195 void Expand_ArrayPut(llvm::Value* new_value,
196 llvm::Value* array_addr,
197 llvm::Value* index_value,
198 JType elem_jty);
199
200 void Expand_FilledNewArray(llvm::CallInst& call_inst);
201
202 llvm::Value* Expand_IGetFast(llvm::Value* field_offset_value,
203 llvm::Value* is_volatile_value,
204 llvm::Value* object_addr,
205 JType field_jty);
206
207 void Expand_IPutFast(llvm::Value* field_offset_value,
208 llvm::Value* is_volatile_value,
209 llvm::Value* object_addr,
210 llvm::Value* new_value,
211 JType field_jty);
212
213 llvm::Value* Expand_SGetFast(llvm::Value* static_storage_addr,
214 llvm::Value* field_offset_value,
215 llvm::Value* is_volatile_value,
216 JType field_jty);
217
218 void Expand_SPutFast(llvm::Value* static_storage_addr,
219 llvm::Value* field_offset_value,
220 llvm::Value* is_volatile_value,
221 llvm::Value* new_value,
222 JType field_jty);
223
224 llvm::Value* Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr);
225
226 llvm::Value* Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value);
227
228 llvm::Value*
229 Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value);
230
231 llvm::Value*
232 Expand_GetVirtualCalleeMethodObjAddrFast(llvm::Value* vtable_idx_value,
233 llvm::Value* this_addr);
234
235 llvm::Value* Expand_Invoke(llvm::CallInst& call_inst);
236
TDYa1274ec8ccd2012-08-11 07:04:57 -0700237 llvm::Value* Expand_DivRem(llvm::CallInst& call_inst, bool is_div, JType op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700238
TDYa127ce4cc0d2012-11-18 16:59:53 -0800239 void Expand_AllocaShadowFrame(llvm::Value* num_vregs_value);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700240
TDYa1278e950c12012-11-02 09:58:19 -0700241 void Expand_SetVReg(llvm::Value* entry_idx, llvm::Value* obj);
242
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700243 void Expand_PopShadowFrame();
244
245 void Expand_UpdateDexPC(llvm::Value* dex_pc_value);
246
TDYa127a1b21852012-07-23 03:20:39 -0700247 //----------------------------------------------------------------------------
248 // Quick
249 //----------------------------------------------------------------------------
250
251 llvm::Value* Expand_FPCompare(llvm::Value* src1_value,
252 llvm::Value* src2_value,
253 bool gt_bias);
254
255 llvm::Value* Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value);
256
257 llvm::Value* EmitCompareResultSelection(llvm::Value* cmp_eq,
258 llvm::Value* cmp_lt);
259
TDYa127f71bf5a2012-07-29 20:09:52 -0700260 llvm::Value* EmitLoadConstantClass(uint32_t dex_pc, uint32_t type_idx);
TDYa1275a26d442012-07-26 18:58:38 -0700261 llvm::Value* EmitLoadStaticStorage(uint32_t dex_pc, uint32_t type_idx);
262
TDYa1275e869b62012-07-25 00:45:39 -0700263 llvm::Value* Expand_HLIGet(llvm::CallInst& call_inst, JType field_jty);
264 void Expand_HLIPut(llvm::CallInst& call_inst, JType field_jty);
265
TDYa1275a26d442012-07-26 18:58:38 -0700266 llvm::Value* Expand_HLSget(llvm::CallInst& call_inst, JType field_jty);
267 void Expand_HLSput(llvm::CallInst& call_inst, JType field_jty);
268
269 llvm::Value* Expand_HLArrayGet(llvm::CallInst& call_inst, JType field_jty);
270 void Expand_HLArrayPut(llvm::CallInst& call_inst, JType field_jty);
271
TDYa127f71bf5a2012-07-29 20:09:52 -0700272 llvm::Value* Expand_ConstString(llvm::CallInst& call_inst);
273 llvm::Value* Expand_ConstClass(llvm::CallInst& call_inst);
274
275 void Expand_MonitorEnter(llvm::CallInst& call_inst);
276 void Expand_MonitorExit(llvm::CallInst& call_inst);
277
278 void Expand_HLCheckCast(llvm::CallInst& call_inst);
279 llvm::Value* Expand_InstanceOf(llvm::CallInst& call_inst);
280
281 llvm::Value* Expand_NewInstance(llvm::CallInst& call_inst);
282
283 llvm::Value* Expand_HLInvoke(llvm::CallInst& call_inst);
284
285 llvm::Value* Expand_OptArrayLength(llvm::CallInst& call_inst);
286 llvm::Value* Expand_NewArray(llvm::CallInst& call_inst);
287 llvm::Value* Expand_HLFilledNewArray(llvm::CallInst& call_inst);
288 void Expand_HLFillArrayData(llvm::CallInst& call_inst);
289
290 llvm::Value* EmitAllocNewArray(uint32_t dex_pc,
291 llvm::Value* array_length_value,
292 uint32_t type_idx,
293 bool is_filled_new_array);
294
295 llvm::Value* EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -0700296 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -0700297 llvm::Value* this_addr,
298 uint32_t dex_pc,
299 bool is_fast_path);
300
TDYa1275e869b62012-07-25 00:45:39 -0700301 void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
302
303 void EmitUpdateDexPC(uint32_t dex_pc);
304
305 void EmitGuard_DivZeroException(uint32_t dex_pc,
306 llvm::Value* denominator,
307 JType op_jty);
308
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100309 void EmitGuard_NullPointerException(uint32_t dex_pc, llvm::Value* object,
310 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700311
312 void EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
313 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +0100314 llvm::Value* index,
315 int opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -0700316
Ian Rogers8e696052013-03-04 09:00:40 -0800317 llvm::FunctionType* GetFunctionType(llvm::Type* ret_type, uint32_t method_idx, bool is_static);
TDYa1275e869b62012-07-25 00:45:39 -0700318
319 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
320
321 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
322 const char* postfix);
323
324 int32_t GetTryItemOffset(uint32_t dex_pc);
325
326 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
327
328 llvm::BasicBlock* GetUnwindBasicBlock();
329
330 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
331
332 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
333
Logan Chien75e4b602012-07-23 14:24:12 -0700334 //----------------------------------------------------------------------------
335 // Expand Arithmetic Helper Intrinsics
336 //----------------------------------------------------------------------------
337
338 llvm::Value* Expand_IntegerShift(llvm::Value* src1_value,
339 llvm::Value* src2_value,
340 IntegerShiftKind kind,
341 JType op_jty);
342
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700343 public:
344 static char ID;
345
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700346 GBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Ian Rogers0d94eb62013-03-06 15:20:50 -0800347 art::CompilerDriver* driver, const art::DexCompilationUnit* dex_compilation_unit)
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700348 : llvm::FunctionPass(ID), intrinsic_helper_(intrinsic_helper), irb_(irb),
349 context_(irb.getContext()), rtb_(irb.Runtime()),
TDYa1278e950c12012-11-02 09:58:19 -0700350 shadow_frame_(NULL), old_shadow_frame_(NULL),
Ian Rogers0d94eb62013-03-06 15:20:50 -0800351 driver_(driver),
Ian Rogers89756f22013-03-04 16:40:02 -0800352 dex_compilation_unit_(dex_compilation_unit),
Ian Rogers8e696052013-03-04 09:00:40 -0800353 func_(NULL), current_bb_(NULL), basic_block_unwind_(NULL), changed_(false) {}
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700354
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700355 bool runOnFunction(llvm::Function& func);
356
357 private:
Logan Chien67645d82012-08-17 09:10:54 +0800358 void InsertStackOverflowCheck(llvm::Function& func);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700359
360 llvm::Value* ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
361 llvm::CallInst& call_inst);
362
363};
364
365char GBCExpanderPass::ID = 0;
366
367bool GBCExpanderPass::runOnFunction(llvm::Function& func) {
Ian Rogers8e696052013-03-04 09:00:40 -0800368 VLOG(compiler) << "GBC expansion on " << func.getName().str();
369
TDYa127b672d1e2012-06-28 21:21:45 -0700370 // Runtime support or stub
Brian Carlstrom265091e2013-01-30 14:08:26 -0800371 if (dex_compilation_unit_ == NULL) {
TDYa127b672d1e2012-06-28 21:21:45 -0700372 return false;
373 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700374
Logan Chien67645d82012-08-17 09:10:54 +0800375 // Setup rewrite context
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700376 shadow_frame_ = NULL;
377 old_shadow_frame_ = NULL;
TDYa1275e869b62012-07-25 00:45:39 -0700378 func_ = &func;
Logan Chien67645d82012-08-17 09:10:54 +0800379 changed_ = false; // Assume unchanged
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700380
Sebastien Hertzf11afa02013-03-19 17:39:29 +0100381 shadow_frame_vreg_addresses_.resize(dex_compilation_unit_->GetCodeItem()->registers_size_, NULL);
Ian Rogers89756f22013-03-04 16:40:02 -0800382 basic_blocks_.resize(dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
383 basic_block_landing_pads_.resize(dex_compilation_unit_->GetCodeItem()->tries_size_, NULL);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700384 basic_block_unwind_ = NULL;
385 for (llvm::Function::iterator bb_iter = func_->begin(), bb_end = func_->end();
386 bb_iter != bb_end;
387 ++bb_iter) {
388 if (bb_iter->begin()->getMetadata("DexOff") == NULL) {
389 continue;
390 }
391 uint32_t dex_pc = LV2UInt(bb_iter->begin()->getMetadata("DexOff")->getOperand(0));
392 basic_blocks_[dex_pc] = bb_iter;
393 }
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -0700394
Logan Chien67645d82012-08-17 09:10:54 +0800395 // Insert stack overflow check
396 InsertStackOverflowCheck(func); // TODO: Use intrinsic.
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700397
Logan Chien67645d82012-08-17 09:10:54 +0800398 // Rewrite the intrinsics
399 RewriteFunction();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700400
401 VERIFY_LLVM_FUNCTION(func);
402
Logan Chien67645d82012-08-17 09:10:54 +0800403 return changed_;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700404}
405
Logan Chien67645d82012-08-17 09:10:54 +0800406void GBCExpanderPass::RewriteBasicBlock(llvm::BasicBlock* original_block) {
407 llvm::BasicBlock* curr_basic_block = original_block;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700408
Logan Chien67645d82012-08-17 09:10:54 +0800409 llvm::BasicBlock::iterator inst_iter = original_block->begin();
410 llvm::BasicBlock::iterator inst_end = original_block->end();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700411
Logan Chien67645d82012-08-17 09:10:54 +0800412 while (inst_iter != inst_end) {
413 llvm::CallInst* call_inst = llvm::dyn_cast<llvm::CallInst>(inst_iter);
414 IntrinsicHelper::IntrinsicId intr_id = IntrinsicHelper::UnknownId;
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700415
Logan Chien67645d82012-08-17 09:10:54 +0800416 if (call_inst) {
417 llvm::Function* callee_func = call_inst->getCalledFunction();
418 intr_id = intrinsic_helper_.GetIntrinsicId(callee_func);
419 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700420
Logan Chien67645d82012-08-17 09:10:54 +0800421 if (intr_id == IntrinsicHelper::UnknownId) {
422 // This is not intrinsic call. Skip this instruction.
423 ++inst_iter;
424 continue;
425 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700426
Logan Chien67645d82012-08-17 09:10:54 +0800427 // Rewrite the intrinsic and change the function
428 changed_ = true;
429 irb_.SetInsertPoint(inst_iter);
430
431 // Expand the intrinsic
432 if (llvm::Value* new_value = ExpandIntrinsic(intr_id, *call_inst)) {
433 inst_iter->replaceAllUsesWith(new_value);
434 }
435
436 // Remove the old intrinsic call instruction
437 llvm::BasicBlock::iterator old_inst = inst_iter++;
438 old_inst->eraseFromParent();
439
440 // Splice the instruction to the new basic block
441 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
442 if (next_basic_block != curr_basic_block) {
443 next_basic_block->getInstList().splice(
444 irb_.GetInsertPoint(), curr_basic_block->getInstList(),
445 inst_iter, inst_end);
446 curr_basic_block = next_basic_block;
447 inst_end = curr_basic_block->end();
448 }
449 }
450}
451
452
453void GBCExpanderPass::RewriteFunction() {
454 size_t num_basic_blocks = func_->getBasicBlockList().size();
455 // NOTE: We are not using (bb_iter != bb_end) as the for-loop condition,
456 // because we will create new basic block while expanding the intrinsics.
457 // We only want to iterate through the input basic blocks.
458
TDYa127aa558872012-08-16 05:11:07 -0700459 landing_pad_phi_mapping_.clear();
460
Logan Chien67645d82012-08-17 09:10:54 +0800461 for (llvm::Function::iterator bb_iter = func_->begin();
462 num_basic_blocks > 0; ++bb_iter, --num_basic_blocks) {
Shih-wei Liao627d8c42012-08-24 08:31:18 -0700463 // Set insert point to current basic block.
464 irb_.SetInsertPoint(bb_iter);
Logan Chien67645d82012-08-17 09:10:54 +0800465
TDYa12755e5e6c2012-09-11 15:14:42 -0700466 current_bb_ = bb_iter;
TDYa127aa558872012-08-16 05:11:07 -0700467
Logan Chien67645d82012-08-17 09:10:54 +0800468 // Rewrite the basic block
469 RewriteBasicBlock(bb_iter);
470
471 // Update the phi-instructions in the successor basic block
472 llvm::BasicBlock* last_block = irb_.GetInsertBlock();
473 if (last_block != bb_iter) {
474 UpdatePhiInstruction(bb_iter, last_block);
475 }
476 }
TDYa127aa558872012-08-16 05:11:07 -0700477
478 typedef std::map<llvm::PHINode*, llvm::PHINode*> HandlerPHIMap;
479 HandlerPHIMap handler_phi;
480 // Iterate every used landing pad basic block
481 for (size_t i = 0, ei = basic_block_landing_pads_.size(); i != ei; ++i) {
482 llvm::BasicBlock* lbb = basic_block_landing_pads_[i];
483 if (lbb == NULL) {
484 continue;
485 }
486
487 llvm::TerminatorInst* term_inst = lbb->getTerminator();
488 std::vector<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> >& rewrite_pair
489 = landing_pad_phi_mapping_[lbb];
490 irb_.SetInsertPoint(lbb->begin());
491
492 // Iterate every succeeding basic block (catch block)
493 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
494 succ_iter != succ_end; ++succ_iter) {
495 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
496
497 // Iterate every phi instructions in the succeeding basic block
498 for (llvm::BasicBlock::iterator
499 inst_iter = succ_basic_block->begin(),
500 inst_end = succ_basic_block->end();
501 inst_iter != inst_end; ++inst_iter) {
502 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
503
504 if (!phi) {
505 break; // Meet non-phi instruction. Done.
506 }
507
508 if (handler_phi[phi] == NULL) {
509 handler_phi[phi] = llvm::PHINode::Create(phi->getType(), 1);
510 }
511
512 // Create new_phi in landing pad
513 llvm::PHINode* new_phi = irb_.CreatePHI(phi->getType(), rewrite_pair.size());
514 // Insert all incoming value into new_phi by rewrite_pair
515 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
516 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
517 llvm::BasicBlock* new_bb = rewrite_pair[j].second;
518 new_phi->addIncoming(phi->getIncomingValueForBlock(old_bb), new_bb);
519 }
520 // Delete all incoming value from phi by rewrite_pair
521 for (size_t j = 0, ej = rewrite_pair.size(); j != ej; ++j) {
522 llvm::BasicBlock* old_bb = rewrite_pair[j].first;
523 int old_bb_idx = phi->getBasicBlockIndex(old_bb);
524 if (old_bb_idx >= 0) {
525 phi->removeIncomingValue(old_bb_idx, false);
526 }
527 }
528 // Insert new_phi into new handler phi
529 handler_phi[phi]->addIncoming(new_phi, lbb);
530 }
531 }
532 }
533
534 // Replace all handler phi
535 // We can't just use the old handler phi, because some exception edges will disappear after we
536 // compute fast-path.
537 for (HandlerPHIMap::iterator it = handler_phi.begin(); it != handler_phi.end(); ++it) {
538 llvm::PHINode* old_phi = it->first;
539 llvm::PHINode* new_phi = it->second;
540 new_phi->insertBefore(old_phi);
541 old_phi->replaceAllUsesWith(new_phi);
542 old_phi->eraseFromParent();
543 }
Logan Chien67645d82012-08-17 09:10:54 +0800544}
545
546void GBCExpanderPass::UpdatePhiInstruction(llvm::BasicBlock* old_basic_block,
547 llvm::BasicBlock* new_basic_block) {
548 llvm::TerminatorInst* term_inst = new_basic_block->getTerminator();
549
550 if (!term_inst) {
551 return; // No terminating instruction in new_basic_block. Nothing to do.
552 }
553
554 // Iterate every succeeding basic block
555 for (unsigned succ_iter = 0, succ_end = term_inst->getNumSuccessors();
556 succ_iter != succ_end; ++succ_iter) {
557 llvm::BasicBlock* succ_basic_block = term_inst->getSuccessor(succ_iter);
558
559 // Iterate every phi instructions in the succeeding basic block
560 for (llvm::BasicBlock::iterator
561 inst_iter = succ_basic_block->begin(),
562 inst_end = succ_basic_block->end();
563 inst_iter != inst_end; ++inst_iter) {
564 llvm::PHINode *phi = llvm::dyn_cast<llvm::PHINode>(inst_iter);
565
566 if (!phi) {
567 break; // Meet non-phi instruction. Done.
568 }
569
570 // Update the incoming block of this phi instruction
571 for (llvm::PHINode::block_iterator
572 ibb_iter = phi->block_begin(), ibb_end = phi->block_end();
573 ibb_iter != ibb_end; ++ibb_iter) {
574 if (*ibb_iter == old_basic_block) {
575 *ibb_iter = new_basic_block;
576 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700577 }
578 }
579 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700580}
581
582llvm::Value* GBCExpanderPass::ExpandToRuntime(runtime_support::RuntimeId rt,
583 llvm::CallInst& inst) {
584 // Some GBC intrinsic can directly replace with IBC runtime. "Directly" means
585 // the arguments passed to the GBC intrinsic are as the same as IBC runtime
586 // function, therefore only called function is needed to change.
587 unsigned num_args = inst.getNumArgOperands();
588
589 if (num_args <= 0) {
590 return irb_.CreateCall(irb_.GetRuntime(rt));
591 } else {
592 std::vector<llvm::Value*> args;
593 for (unsigned i = 0; i < num_args; i++) {
594 args.push_back(inst.getArgOperand(i));
595 }
596
597 return irb_.CreateCall(irb_.GetRuntime(rt), args);
598 }
599}
600
Logan Chien67645d82012-08-17 09:10:54 +0800601void
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700602GBCExpanderPass::EmitStackOverflowCheck(llvm::Instruction* first_non_alloca) {
603 llvm::Function* func = first_non_alloca->getParent()->getParent();
604 llvm::Module* module = func->getParent();
605
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700606 // Call llvm intrinsic function to get frame address.
607 llvm::Function* frameaddress =
608 llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::frameaddress);
609
610 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
611 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
612
613 // Cast i8* to int
614 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
615
616 // Get thread.stack_end_
617 llvm::Value* stack_end =
TDYa127920be7c2012-09-10 17:13:22 -0700618 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::StackEndOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700619 irb_.getPtrEquivIntTy(),
620 kTBAARuntimeInfo);
621
622 // Check the frame address < thread.stack_end_ ?
623 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
624
625 llvm::BasicBlock* block_exception =
626 llvm::BasicBlock::Create(context_, "stack_overflow", func);
627
628 llvm::BasicBlock* block_continue =
629 llvm::BasicBlock::Create(context_, "stack_overflow_cont", func);
630
631 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
632
633 // If stack overflow, throw exception.
634 irb_.SetInsertPoint(block_exception);
635 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowStackOverflowException));
636
637 // Unwind.
638 llvm::Type* ret_type = func->getReturnType();
639 if (ret_type->isVoidTy()) {
640 irb_.CreateRetVoid();
641 } else {
642 // The return value is ignored when there's an exception. MethodCompiler
643 // returns zero value under the the corresponding return type in this case.
644 // GBCExpander returns LLVM undef value here for brevity
645 irb_.CreateRet(llvm::UndefValue::get(ret_type));
646 }
647
648 irb_.SetInsertPoint(block_continue);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700649}
650
TDYa127920be7c2012-09-10 17:13:22 -0700651llvm::Value* GBCExpanderPass::EmitLoadDexCacheAddr(art::MemberOffset offset) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700652 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
653
654 return irb_.LoadFromObjectOffset(method_object_addr,
655 offset.Int32Value(),
656 irb_.getJObjectTy(),
657 kTBAAConstJObject);
658}
659
660llvm::Value*
661GBCExpanderPass::EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
662 llvm::Value* static_storage_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800663 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700664
665 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
666
667 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
668}
669
670llvm::Value*
671GBCExpanderPass::EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
672 llvm::Value* resolved_type_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800673 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedTypesOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700674
675 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
676
677 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
678}
679
680llvm::Value* GBCExpanderPass::
681EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
682 llvm::Value* resolved_method_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800683 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheResolvedMethodsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700684
685 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
686
687 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
688}
689
690llvm::Value* GBCExpanderPass::
691EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
692 llvm::Value* string_dex_cache_addr =
Ian Rogers98573f92013-01-30 17:26:32 -0800693 EmitLoadDexCacheAddr(art::mirror::AbstractMethod::DexCacheStringsOffset());
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700694
695 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
696
697 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
698}
699
700llvm::Value* GBCExpanderPass::EmitLoadMethodObjectAddr() {
701 llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
702 return parent_func->arg_begin();
703}
704
705llvm::Value* GBCExpanderPass::EmitLoadArrayLength(llvm::Value* array) {
706 // Load array length
707 return irb_.LoadFromObjectOffset(array,
Ian Rogers98573f92013-01-30 17:26:32 -0800708 art::mirror::Array::LengthOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700709 irb_.getJIntTy(),
710 kTBAAConstJObject);
711
712}
713
714llvm::Value*
715GBCExpanderPass::EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
716 llvm::Value* callee_method_object_field_addr =
717 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
718
TDYa127ce4cc0d2012-11-18 16:59:53 -0800719 return irb_.CreateLoad(callee_method_object_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700720}
721
722llvm::Value* GBCExpanderPass::
723EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, llvm::Value* this_addr) {
724 // Load class object of *this* pointer
725 llvm::Value* class_object_addr =
726 irb_.LoadFromObjectOffset(this_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800727 art::mirror::Object::ClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700728 irb_.getJObjectTy(),
729 kTBAAConstJObject);
730
731 // Load vtable address
732 llvm::Value* vtable_addr =
733 irb_.LoadFromObjectOffset(class_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -0800734 art::mirror::Class::VTableOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700735 irb_.getJObjectTy(),
736 kTBAAConstJObject);
737
738 // Load callee method object
739 llvm::Value* vtable_idx_value =
740 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
741
742 llvm::Value* method_field_addr =
743 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
744
745 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
746}
747
748// Emit Array GetElementPtr
749llvm::Value* GBCExpanderPass::EmitArrayGEP(llvm::Value* array_addr,
750 llvm::Value* index_value,
751 JType elem_jty) {
752
753 int data_offset;
754 if (elem_jty == kLong || elem_jty == kDouble ||
Ian Rogers98573f92013-01-30 17:26:32 -0800755 (elem_jty == kObject && sizeof(uint64_t) == sizeof(art::mirror::Object*))) {
756 data_offset = art::mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700757 } else {
Ian Rogers98573f92013-01-30 17:26:32 -0800758 data_offset = art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700759 }
760
761 llvm::Constant* data_offset_value =
762 irb_.getPtrEquivInt(data_offset);
763
Ian Rogers76ae4fe2013-02-27 16:03:41 -0800764 llvm::Type* elem_type = irb_.getJType(elem_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700765
766 llvm::Value* array_data_addr =
767 irb_.CreatePtrDisp(array_addr, data_offset_value,
768 elem_type->getPointerTo());
769
770 return irb_.CreateGEP(array_data_addr, index_value);
771}
772
Sebastien Hertz901d5ba2013-03-06 15:19:34 +0100773llvm::Value* GBCExpanderPass::EmitInvoke(llvm::CallInst& call_inst) {
774 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
775 art::InvokeType invoke_type =
776 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
777 bool is_static = (invoke_type == art::kStatic);
778 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
779
780 // Load *this* actual parameter
781 llvm::Value* this_addr = (!is_static) ? call_inst.getArgOperand(3) : NULL;
782
783 // Compute invoke related information for compiler decision
784 int vtable_idx = -1;
785 uintptr_t direct_code = 0;
786 uintptr_t direct_method = 0;
787 bool is_fast_path = driver_->
788 ComputeInvokeInfo(callee_method_idx, dex_compilation_unit_,
789 invoke_type, vtable_idx, direct_code, direct_method);
790
791 // Load the method object
792 llvm::Value* callee_method_object_addr = NULL;
793
794 if (!is_fast_path) {
795 callee_method_object_addr =
796 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
797 this_addr, dex_pc, is_fast_path);
798 } else {
799 switch (invoke_type) {
800 case art::kStatic:
801 case art::kDirect:
802 if (direct_method != 0u &&
803 direct_method != static_cast<uintptr_t>(-1)) {
804 callee_method_object_addr =
805 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
806 irb_.getJObjectTy());
807 } else {
808 callee_method_object_addr =
809 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
810 }
811 break;
812
813 case art::kVirtual:
814 DCHECK(vtable_idx != -1);
815 callee_method_object_addr =
816 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
817 break;
818
819 case art::kSuper:
820 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
821 "the fast path.";
822 break;
823
824 case art::kInterface:
825 callee_method_object_addr =
826 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
827 invoke_type, this_addr,
828 dex_pc, is_fast_path);
829 break;
830 }
831 }
832
833 // Load the actual parameter
834 std::vector<llvm::Value*> args;
835
836 args.push_back(callee_method_object_addr); // method object for callee
837
838 for (uint32_t i = 3; i < call_inst.getNumArgOperands(); ++i) {
839 args.push_back(call_inst.getArgOperand(i));
840 }
841
842 llvm::Value* code_addr;
843 llvm::Type* func_type = GetFunctionType(call_inst.getType(),
844 callee_method_idx, is_static);
845 if (direct_code != 0u && direct_code != static_cast<uintptr_t>(-1)) {
846 code_addr =
847 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_code),
848 func_type->getPointerTo());
849 } else {
850 code_addr =
851 irb_.LoadFromObjectOffset(callee_method_object_addr,
852 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
853 func_type->getPointerTo(), kTBAARuntimeInfo);
854 }
855
856 // Invoke callee
857 EmitUpdateDexPC(dex_pc);
858 llvm::Value* retval = irb_.CreateCall(code_addr, args);
859 EmitGuard_ExceptionLandingPad(dex_pc);
860
861 return retval;
862}
863
864bool GBCExpanderPass::EmitIntrinsic(llvm::CallInst& call_inst,
865 llvm::Value** result) {
866 DCHECK(result != NULL);
867
868 uint32_t callee_method_idx = LV2UInt(call_inst.getArgOperand(1));
869 std::string callee_method_name(
870 PrettyMethod(callee_method_idx, *dex_compilation_unit_->GetDexFile()));
871
872 if (callee_method_name == "int java.lang.String.length()") {
873 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
874 false /* is_empty */);
875 }
876 if (callee_method_name == "boolean java.lang.String.isEmpty()") {
877 return EmitIntrinsicStringLengthOrIsEmpty(call_inst, result,
878 true /* is_empty */);
879 }
880
881 *result = NULL;
882 return false;
883}
884
885bool GBCExpanderPass::EmitIntrinsicStringLengthOrIsEmpty(llvm::CallInst& call_inst,
886 llvm::Value** result,
887 bool is_empty) {
888 art::InvokeType invoke_type =
889 static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
890 DCHECK_NE(invoke_type, art::kStatic);
891 DCHECK_EQ(call_inst.getNumArgOperands(), 4U);
892
893 llvm::Value* this_object = call_inst.getArgOperand(3);
894 llvm::Value* string_count =
895 irb_.LoadFromObjectOffset(this_object,
896 art::mirror::String::CountOffset().Int32Value(),
897 irb_.getJIntTy(),
898 kTBAAConstJObject);
899 if (is_empty) {
900 llvm::Value* count_equals_zero = irb_.CreateICmpEQ(string_count,
901 irb_.getJInt(0));
902 llvm::Value* is_empty = irb_.CreateSelect(count_equals_zero,
903 irb_.getJBoolean(true),
904 irb_.getJBoolean(false));
905 is_empty = SignOrZeroExtendCat1Types(is_empty, kBoolean);
906 *result = is_empty;
907 } else {
908 *result = string_count;
909 }
910 return true;
911}
912
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700913void GBCExpanderPass::Expand_TestSuspend(llvm::CallInst& call_inst) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800914 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
915
916 llvm::Value* suspend_count =
917 irb_.Runtime().EmitLoadFromThreadOffset(art::Thread::ThreadFlagsOffset().Int32Value(),
918 irb_.getInt16Ty(),
919 kTBAARuntimeInfo);
920 llvm::Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
921
922 llvm::BasicBlock* basic_block_suspend = CreateBasicBlockWithDexPC(dex_pc, "suspend");
923 llvm::BasicBlock* basic_block_cont = CreateBasicBlockWithDexPC(dex_pc, "suspend_cont");
924
925 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
926
927 irb_.SetInsertPoint(basic_block_suspend);
928 if (dex_pc != art::DexFile::kDexNoIndex) {
929 EmitUpdateDexPC(dex_pc);
930 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700931 irb_.Runtime().EmitTestSuspend();
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800932
933 llvm::BasicBlock* basic_block_exception = CreateBasicBlockWithDexPC(dex_pc, "exception");
934 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
935 irb_.CreateCondBr(exception_pending, basic_block_exception, basic_block_cont, kUnlikely);
936
937 irb_.SetInsertPoint(basic_block_exception);
938 llvm::Type* ret_type = call_inst.getParent()->getParent()->getReturnType();
939 if (ret_type->isVoidTy()) {
940 irb_.CreateRetVoid();
941 } else {
942 // The return value is ignored when there's an exception.
943 irb_.CreateRet(llvm::UndefValue::get(ret_type));
944 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800945
946 irb_.SetInsertPoint(basic_block_cont);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700947 return;
948}
949
TDYa1279a129452012-07-19 03:10:08 -0700950void GBCExpanderPass::Expand_MarkGCCard(llvm::CallInst& call_inst) {
TDYa1279a129452012-07-19 03:10:08 -0700951 irb_.Runtime().EmitMarkGCCard(call_inst.getArgOperand(0), call_inst.getArgOperand(1));
TDYa1279a129452012-07-19 03:10:08 -0700952 return;
953}
954
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700955llvm::Value*
956GBCExpanderPass::Expand_LoadStringFromDexCache(llvm::Value* string_idx_value) {
957 uint32_t string_idx =
958 llvm::cast<llvm::ConstantInt>(string_idx_value)->getZExtValue();
959
960 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
961
TDYa127ce4cc0d2012-11-18 16:59:53 -0800962 return irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700963}
964
965llvm::Value*
966GBCExpanderPass::Expand_LoadTypeFromDexCache(llvm::Value* type_idx_value) {
967 uint32_t type_idx =
968 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
969
970 llvm::Value* type_field_addr =
971 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
972
TDYa127ce4cc0d2012-11-18 16:59:53 -0800973 return irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700974}
975
976void GBCExpanderPass::Expand_LockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700977 rtb_.EmitLockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700978 return;
979}
980
981void GBCExpanderPass::Expand_UnlockObject(llvm::Value* obj) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700982 rtb_.EmitUnlockObject(obj);
Shih-wei Liao21d28f52012-06-12 05:55:00 -0700983 return;
984}
985
986llvm::Value* GBCExpanderPass::Expand_ArrayGet(llvm::Value* array_addr,
987 llvm::Value* index_value,
988 JType elem_jty) {
989 llvm::Value* array_elem_addr =
990 EmitArrayGEP(array_addr, index_value, elem_jty);
991
992 return irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
993}
994
995void GBCExpanderPass::Expand_ArrayPut(llvm::Value* new_value,
996 llvm::Value* array_addr,
997 llvm::Value* index_value,
998 JType elem_jty) {
999 llvm::Value* array_elem_addr =
1000 EmitArrayGEP(array_addr, index_value, elem_jty);
1001
1002 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1003
1004 return;
1005}
1006
1007void GBCExpanderPass::Expand_FilledNewArray(llvm::CallInst& call_inst) {
1008 // Most of the codes refer to MethodCompiler::EmitInsn_FilledNewArray
1009 llvm::Value* array = call_inst.getArgOperand(0);
1010
1011 uint32_t element_jty =
1012 llvm::cast<llvm::ConstantInt>(call_inst.getArgOperand(1))->getZExtValue();
1013
1014 DCHECK(call_inst.getNumArgOperands() > 2);
1015 unsigned num_elements = (call_inst.getNumArgOperands() - 2);
1016
1017 bool is_elem_int_ty = (static_cast<JType>(element_jty) == kInt);
1018
1019 uint32_t alignment;
1020 llvm::Constant* elem_size;
1021 llvm::PointerType* field_type;
1022
1023 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1024 // as the element, thus we are only checking 2 cases: primitive int and
1025 // non-primitive type.
1026 if (is_elem_int_ty) {
1027 alignment = sizeof(int32_t);
1028 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
1029 field_type = irb_.getJIntTy()->getPointerTo();
1030 } else {
1031 alignment = irb_.getSizeOfPtrEquivInt();
1032 elem_size = irb_.getSizeOfPtrEquivIntValue();
1033 field_type = irb_.getJObjectTy()->getPointerTo();
1034 }
1035
1036 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08001037 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001038
1039 llvm::Value* data_field_addr =
1040 irb_.CreatePtrDisp(array, data_field_offset, field_type);
1041
1042 for (unsigned i = 0; i < num_elements; ++i) {
1043 // Values to fill the array begin at the 3rd argument
1044 llvm::Value* reg_value = call_inst.getArgOperand(2 + i);
1045
1046 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
1047
1048 data_field_addr =
1049 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
1050 }
1051
1052 return;
1053}
1054
1055llvm::Value* GBCExpanderPass::Expand_IGetFast(llvm::Value* field_offset_value,
1056 llvm::Value* /*is_volatile_value*/,
1057 llvm::Value* object_addr,
1058 JType field_jty) {
1059 int field_offset =
1060 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1061
1062 DCHECK_GE(field_offset, 0);
1063
1064 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001065 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001066
1067 field_offset_value = irb_.getPtrEquivInt(field_offset);
1068
1069 llvm::Value* field_addr =
1070 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1071
1072 // TODO: Check is_volatile. We need to generate atomic load instruction
1073 // when is_volatile is true.
1074 return irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
1075}
1076
1077void GBCExpanderPass::Expand_IPutFast(llvm::Value* field_offset_value,
1078 llvm::Value* /* is_volatile_value */,
1079 llvm::Value* object_addr,
1080 llvm::Value* new_value,
1081 JType field_jty) {
1082 int field_offset =
1083 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1084
1085 DCHECK_GE(field_offset, 0);
1086
1087 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001088 irb_.getJType(field_jty)->getPointerTo();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001089
1090 field_offset_value = irb_.getPtrEquivInt(field_offset);
1091
1092 llvm::Value* field_addr =
1093 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1094
1095 // TODO: Check is_volatile. We need to generate atomic store instruction
1096 // when is_volatile is true.
1097 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1098
1099 return;
1100}
1101
1102llvm::Value* GBCExpanderPass::Expand_SGetFast(llvm::Value* static_storage_addr,
1103 llvm::Value* field_offset_value,
1104 llvm::Value* /*is_volatile_value*/,
1105 JType field_jty) {
1106 int field_offset =
1107 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1108
1109 DCHECK_GE(field_offset, 0);
1110
1111 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1112
1113 llvm::Value* static_field_addr =
1114 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001115 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001116
1117 // TODO: Check is_volatile. We need to generate atomic store instruction
1118 // when is_volatile is true.
1119 return irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
1120}
1121
1122void GBCExpanderPass::Expand_SPutFast(llvm::Value* static_storage_addr,
1123 llvm::Value* field_offset_value,
1124 llvm::Value* /* is_volatile_value */,
1125 llvm::Value* new_value,
1126 JType field_jty) {
1127 int field_offset =
1128 llvm::cast<llvm::ConstantInt>(field_offset_value)->getSExtValue();
1129
1130 DCHECK_GE(field_offset, 0);
1131
1132 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1133
1134 llvm::Value* static_field_addr =
1135 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001136 irb_.getJType(field_jty)->getPointerTo());
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001137
1138 // TODO: Check is_volatile. We need to generate atomic store instruction
1139 // when is_volatile is true.
1140 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1141
1142 return;
1143}
1144
1145llvm::Value*
1146GBCExpanderPass::Expand_LoadDeclaringClassSSB(llvm::Value* method_object_addr) {
1147 return irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001148 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001149 irb_.getJObjectTy(),
1150 kTBAAConstJObject);
1151}
1152
1153llvm::Value*
1154GBCExpanderPass::Expand_LoadClassSSBFromDexCache(llvm::Value* type_idx_value) {
1155 uint32_t type_idx =
1156 llvm::cast<llvm::ConstantInt>(type_idx_value)->getZExtValue();
1157
1158 llvm::Value* storage_field_addr =
1159 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1160
TDYa127ce4cc0d2012-11-18 16:59:53 -08001161 return irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001162}
1163
1164llvm::Value*
1165GBCExpanderPass::Expand_GetSDCalleeMethodObjAddrFast(llvm::Value* callee_method_idx_value) {
1166 uint32_t callee_method_idx =
1167 llvm::cast<llvm::ConstantInt>(callee_method_idx_value)->getZExtValue();
1168
1169 return EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
1170}
1171
1172llvm::Value* GBCExpanderPass::Expand_GetVirtualCalleeMethodObjAddrFast(
1173 llvm::Value* vtable_idx_value,
1174 llvm::Value* this_addr) {
1175 int vtable_idx =
1176 llvm::cast<llvm::ConstantInt>(vtable_idx_value)->getSExtValue();
1177
1178 return EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
1179}
1180
1181llvm::Value* GBCExpanderPass::Expand_Invoke(llvm::CallInst& call_inst) {
1182 // Most of the codes refer to MethodCompiler::EmitInsn_Invoke
1183 llvm::Value* callee_method_object_addr = call_inst.getArgOperand(0);
1184 unsigned num_args = call_inst.getNumArgOperands();
1185 llvm::Type* ret_type = call_inst.getType();
1186
1187 // Determine the function type of the callee method
1188 std::vector<llvm::Type*> args_type;
1189 std::vector<llvm::Value*> args;
1190 for (unsigned i = 0; i < num_args; i++) {
1191 args.push_back(call_inst.getArgOperand(i));
1192 args_type.push_back(args[i]->getType());
1193 }
1194
1195 llvm::FunctionType* callee_method_type =
1196 llvm::FunctionType::get(ret_type, args_type, false);
1197
1198 llvm::Value* code_addr =
1199 irb_.LoadFromObjectOffset(callee_method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001200 art::mirror::AbstractMethod::GetCodeOffset().Int32Value(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001201 callee_method_type->getPointerTo(),
TDYa127ce4cc0d2012-11-18 16:59:53 -08001202 kTBAARuntimeInfo);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001203
1204 // Invoke callee
1205 llvm::Value* retval = irb_.CreateCall(code_addr, args);
1206
1207 return retval;
1208}
1209
TDYa1274ec8ccd2012-08-11 07:04:57 -07001210llvm::Value* GBCExpanderPass::Expand_DivRem(llvm::CallInst& call_inst,
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001211 bool is_div, JType op_jty) {
TDYa1274ec8ccd2012-08-11 07:04:57 -07001212 llvm::Value* dividend = call_inst.getArgOperand(0);
1213 llvm::Value* divisor = call_inst.getArgOperand(1);
TDYa1274ec8ccd2012-08-11 07:04:57 -07001214 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1215 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001216 // Most of the codes refer to MethodCompiler::EmitIntDivRemResultComputation
1217
1218 // Check the special case: MININT / -1 = MININT
1219 // That case will cause overflow, which is undefined behavior in llvm.
1220 // So we check the divisor is -1 or not, if the divisor is -1, we do
1221 // the special path to avoid undefined behavior.
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001222 llvm::Type* op_type = irb_.getJType(op_jty);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001223 llvm::Value* zero = irb_.getJZero(op_jty);
1224 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
1225
TDYa1275e869b62012-07-25 00:45:39 -07001226 llvm::Function* parent = irb_.GetInsertBlock()->getParent();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001227 llvm::BasicBlock* eq_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1228 llvm::BasicBlock* ne_neg_one = llvm::BasicBlock::Create(context_, "", parent);
1229 llvm::BasicBlock* neg_one_cont =
1230 llvm::BasicBlock::Create(context_, "", parent);
1231
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001232 llvm::Value* is_equal_neg_one = irb_.CreateICmpEQ(divisor, neg_one);
1233 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
1234
1235 // If divisor == -1
1236 irb_.SetInsertPoint(eq_neg_one);
1237 llvm::Value* eq_result;
1238 if (is_div) {
1239 // We can just change from "dividend div -1" to "neg dividend". The sub
1240 // don't care the sign/unsigned because of two's complement representation.
1241 // And the behavior is what we want:
1242 // -(2^n) (2^n)-1
1243 // MININT < k <= MAXINT -> mul k -1 = -k
1244 // MININT == k -> mul k -1 = k
1245 //
1246 // LLVM use sub to represent 'neg'
1247 eq_result = irb_.CreateSub(zero, dividend);
1248 } else {
1249 // Everything modulo -1 will be 0.
1250 eq_result = zero;
1251 }
1252 irb_.CreateBr(neg_one_cont);
1253
1254 // If divisor != -1, just do the division.
1255 irb_.SetInsertPoint(ne_neg_one);
1256 llvm::Value* ne_result;
1257 if (is_div) {
1258 ne_result = irb_.CreateSDiv(dividend, divisor);
1259 } else {
1260 ne_result = irb_.CreateSRem(dividend, divisor);
1261 }
1262 irb_.CreateBr(neg_one_cont);
1263
1264 irb_.SetInsertPoint(neg_one_cont);
1265 llvm::PHINode* result = irb_.CreatePHI(op_type, 2);
1266 result->addIncoming(eq_result, eq_neg_one);
1267 result->addIncoming(ne_result, ne_neg_one);
1268
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001269 return result;
1270}
1271
TDYa127ce4cc0d2012-11-18 16:59:53 -08001272void GBCExpanderPass::Expand_AllocaShadowFrame(llvm::Value* num_vregs_value) {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001273 // Most of the codes refer to MethodCompiler::EmitPrologueAllocShadowFrame and
1274 // MethodCompiler::EmitPushShadowFrame
TDYa1278e950c12012-11-02 09:58:19 -07001275 uint16_t num_vregs =
1276 llvm::cast<llvm::ConstantInt>(num_vregs_value)->getZExtValue();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001277
1278 llvm::StructType* shadow_frame_type =
TDYa127ce4cc0d2012-11-18 16:59:53 -08001279 irb_.getShadowFrameTy(num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001280
Sebastien Hertz77209702013-02-28 16:34:13 +01001281 // Create allocas at the start of entry block.
1282 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1283 llvm::BasicBlock* entry_block = &func_->front();
1284 irb_.SetInsertPoint(&entry_block->front());
1285
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001286 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
1287
1288 // Alloca a pointer to old shadow frame
1289 old_shadow_frame_ =
1290 irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
1291
Sebastien Hertz77209702013-02-28 16:34:13 +01001292 irb_.restoreIP(irb_ip_original);
1293
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001294 // Push the shadow frame
1295 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1296
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001297 llvm::Value* shadow_frame_upcast =
1298 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
1299
1300 llvm::Value* result = rtb_.EmitPushShadowFrame(shadow_frame_upcast,
1301 method_object_addr,
TDYa1278e950c12012-11-02 09:58:19 -07001302 num_vregs);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001303
1304 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
1305
1306 return;
1307}
1308
TDYa1278e950c12012-11-02 09:58:19 -07001309void GBCExpanderPass::Expand_SetVReg(llvm::Value* entry_idx,
1310 llvm::Value* value) {
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001311 unsigned vreg_idx = LV2UInt(entry_idx);
1312 DCHECK_LT(vreg_idx, dex_compilation_unit_->GetCodeItem()->registers_size_);
TDYa1278e950c12012-11-02 09:58:19 -07001313
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001314 llvm::Value* vreg_addr = shadow_frame_vreg_addresses_[vreg_idx];
1315 if (UNLIKELY(vreg_addr == NULL)) {
1316 DCHECK(shadow_frame_ != NULL);
TDYa1278e950c12012-11-02 09:58:19 -07001317
Sebastien Hertzf11afa02013-03-19 17:39:29 +01001318 llvm::Value* gep_index[] = {
1319 irb_.getInt32(0), // No pointer displacement
1320 irb_.getInt32(1), // VRegs
1321 entry_idx // Pointer field
1322 };
1323
1324 // A shadow frame address must dominate every use in the function so we
1325 // place it in the entry block right after the allocas.
1326 llvm::BasicBlock::iterator first_non_alloca = func_->getEntryBlock().begin();
1327 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1328 ++first_non_alloca;
1329 }
1330
1331 llvm::IRBuilderBase::InsertPoint ip = irb_.saveIP();
1332 irb_.SetInsertPoint(static_cast<llvm::Instruction*>(first_non_alloca));
1333 vreg_addr = irb_.CreateGEP(shadow_frame_, gep_index);
1334 shadow_frame_vreg_addresses_[vreg_idx] = vreg_addr;
1335 irb_.restoreIP(ip);
1336 }
TDYa1278e950c12012-11-02 09:58:19 -07001337
1338 irb_.CreateStore(value,
1339 irb_.CreateBitCast(vreg_addr, value->getType()->getPointerTo()),
1340 kTBAAShadowFrame);
1341 return;
1342}
1343
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001344void GBCExpanderPass::Expand_PopShadowFrame() {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001345 if (old_shadow_frame_ == NULL) {
1346 return;
1347 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001348 rtb_.EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
1349 return;
1350}
1351
1352void GBCExpanderPass::Expand_UpdateDexPC(llvm::Value* dex_pc_value) {
1353 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07001354 art::ShadowFrame::DexPCOffset(),
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001355 dex_pc_value,
1356 kTBAAShadowFrame);
1357 return;
1358}
1359
Logan Chien67645d82012-08-17 09:10:54 +08001360void GBCExpanderPass::InsertStackOverflowCheck(llvm::Function& func) {
jeffhao40283122013-01-15 13:15:24 -08001361 // All alloca instructions are generated in the first basic block of the
1362 // function, and there are no alloca instructions after the first non-alloca
1363 // instruction.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001364
Logan Chien67645d82012-08-17 09:10:54 +08001365 llvm::BasicBlock* first_basic_block = &func.front();
1366
1367 // Look for first non-alloca instruction
1368 llvm::BasicBlock::iterator first_non_alloca = first_basic_block->begin();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001369 while (llvm::isa<llvm::AllocaInst>(first_non_alloca)) {
1370 ++first_non_alloca;
1371 }
1372
Logan Chien67645d82012-08-17 09:10:54 +08001373 irb_.SetInsertPoint(first_non_alloca);
1374
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001375 // Insert stack overflow check codes before first_non_alloca (i.e., after all
1376 // alloca instructions)
Logan Chien67645d82012-08-17 09:10:54 +08001377 EmitStackOverflowCheck(&*first_non_alloca);
1378
TDYa127890ea892012-08-22 10:49:42 -07001379 irb_.Runtime().EmitTestSuspend();
TDYa127890ea892012-08-22 10:49:42 -07001380
Logan Chien67645d82012-08-17 09:10:54 +08001381 llvm::BasicBlock* next_basic_block = irb_.GetInsertBlock();
1382 if (next_basic_block != first_basic_block) {
1383 // Splice the rest of the instruction to the continuing basic block
1384 next_basic_block->getInstList().splice(
1385 irb_.GetInsertPoint(), first_basic_block->getInstList(),
1386 first_non_alloca, first_basic_block->end());
1387
1388 // Rewrite the basic block
1389 RewriteBasicBlock(next_basic_block);
1390
1391 // Update the phi-instructions in the successor basic block
1392 UpdatePhiInstruction(first_basic_block, irb_.GetInsertBlock());
1393 }
1394
1395 // We have changed the basic block
1396 changed_ = true;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07001397}
1398
TDYa1275e869b62012-07-25 00:45:39 -07001399// ==== High-level intrinsic expander ==========================================
1400
TDYa127a1b21852012-07-23 03:20:39 -07001401llvm::Value* GBCExpanderPass::Expand_FPCompare(llvm::Value* src1_value,
1402 llvm::Value* src2_value,
1403 bool gt_bias) {
1404 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1405 llvm::Value* cmp_lt;
1406
1407 if (gt_bias) {
1408 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1409 } else {
1410 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1411 }
1412
1413 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1414}
1415
1416llvm::Value* GBCExpanderPass::Expand_LongCompare(llvm::Value* src1_value, llvm::Value* src2_value) {
1417 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1418 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1419
1420 return EmitCompareResultSelection(cmp_eq, cmp_lt);
1421}
1422
1423llvm::Value* GBCExpanderPass::EmitCompareResultSelection(llvm::Value* cmp_eq,
1424 llvm::Value* cmp_lt) {
1425
1426 llvm::Constant* zero = irb_.getJInt(0);
1427 llvm::Constant* pos1 = irb_.getJInt(1);
1428 llvm::Constant* neg1 = irb_.getJInt(-1);
1429
1430 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1431 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1432
1433 return result_eq;
1434}
1435
Logan Chien75e4b602012-07-23 14:24:12 -07001436llvm::Value* GBCExpanderPass::Expand_IntegerShift(llvm::Value* src1_value,
1437 llvm::Value* src2_value,
1438 IntegerShiftKind kind,
1439 JType op_jty) {
1440 DCHECK(op_jty == kInt || op_jty == kLong);
1441
1442 // Mask and zero-extend RHS properly
1443 if (op_jty == kInt) {
1444 src2_value = irb_.CreateAnd(src2_value, 0x1f);
1445 } else {
1446 llvm::Value* masked_src2_value = irb_.CreateAnd(src2_value, 0x3f);
1447 src2_value = irb_.CreateZExt(masked_src2_value, irb_.getJLongTy());
1448 }
1449
1450 // Create integer shift llvm instruction
1451 switch (kind) {
1452 case kIntegerSHL:
1453 return irb_.CreateShl(src1_value, src2_value);
1454
1455 case kIntegerSHR:
1456 return irb_.CreateAShr(src1_value, src2_value);
1457
1458 case kIntegerUSHR:
1459 return irb_.CreateLShr(src1_value, src2_value);
1460
1461 default:
1462 LOG(FATAL) << "Unknown integer shift kind: " << kind;
1463 return NULL;
1464 }
1465}
1466
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001467llvm::Value* GBCExpanderPass::SignOrZeroExtendCat1Types(llvm::Value* value, JType jty) {
1468 switch (jty) {
1469 case kBoolean:
1470 case kChar:
1471 return irb_.CreateZExt(value, irb_.getJType(kInt));
1472 case kByte:
1473 case kShort:
1474 return irb_.CreateSExt(value, irb_.getJType(kInt));
1475 case kVoid:
1476 case kInt:
1477 case kLong:
1478 case kFloat:
1479 case kDouble:
1480 case kObject:
1481 return value; // Nothing to do.
1482 default:
1483 LOG(FATAL) << "Unknown java type: " << jty;
1484 return NULL;
1485 }
1486}
1487
1488llvm::Value* GBCExpanderPass::TruncateCat1Types(llvm::Value* value, JType jty) {
1489 switch (jty) {
1490 case kBoolean:
1491 case kChar:
1492 case kByte:
1493 case kShort:
1494 return irb_.CreateTrunc(value, irb_.getJType(jty));
1495 case kVoid:
1496 case kInt:
1497 case kLong:
1498 case kFloat:
1499 case kDouble:
1500 case kObject:
1501 return value; // Nothing to do.
1502 default:
1503 LOG(FATAL) << "Unknown java type: " << jty;
1504 return NULL;
1505 }
1506}
1507
TDYa1275a26d442012-07-26 18:58:38 -07001508llvm::Value* GBCExpanderPass::Expand_HLArrayGet(llvm::CallInst& call_inst,
1509 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001510 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1511 llvm::Value* array_addr = call_inst.getArgOperand(1);
1512 llvm::Value* index_value = call_inst.getArgOperand(2);
TDYa127920be7c2012-09-10 17:13:22 -07001513 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001514
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001515 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1516 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1517 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001518
1519 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1520
1521 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
1522
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001523 return SignOrZeroExtendCat1Types(array_elem_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001524}
1525
1526
1527void GBCExpanderPass::Expand_HLArrayPut(llvm::CallInst& call_inst,
1528 JType elem_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001529 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1530 llvm::Value* new_value = call_inst.getArgOperand(1);
1531 llvm::Value* array_addr = call_inst.getArgOperand(2);
1532 llvm::Value* index_value = call_inst.getArgOperand(3);
TDYa127920be7c2012-09-10 17:13:22 -07001533 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275a26d442012-07-26 18:58:38 -07001534
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001535 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
1536 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, index_value,
1537 opt_flags);
TDYa1275a26d442012-07-26 18:58:38 -07001538
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001539 new_value = TruncateCat1Types(new_value, elem_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001540
1541 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
1542
1543 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
1544 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::CheckPutArrayElement);
1545
1546 irb_.CreateCall2(runtime_func, new_value, array_addr);
1547
1548 EmitGuard_ExceptionLandingPad(dex_pc);
1549
1550 EmitMarkGCCard(new_value, array_addr);
1551 }
1552
1553 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
1554
1555 return;
1556}
1557
TDYa1275e869b62012-07-25 00:45:39 -07001558llvm::Value* GBCExpanderPass::Expand_HLIGet(llvm::CallInst& call_inst,
1559 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001560 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1561 llvm::Value* object_addr = call_inst.getArgOperand(1);
1562 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(2));
TDYa127920be7c2012-09-10 17:13:22 -07001563 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001564
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001565 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001566
1567 llvm::Value* field_value;
1568
1569 int field_offset;
1570 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001571 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001572 field_idx, dex_compilation_unit_, field_offset, is_volatile, false);
TDYa1275e869b62012-07-25 00:45:39 -07001573
1574 if (!is_fast_path) {
1575 llvm::Function* runtime_func;
1576
1577 if (field_jty == kObject) {
1578 runtime_func = irb_.GetRuntime(runtime_support::GetObjectInstance);
1579 } else if (field_jty == kLong || field_jty == kDouble) {
1580 runtime_func = irb_.GetRuntime(runtime_support::Get64Instance);
1581 } else {
1582 runtime_func = irb_.GetRuntime(runtime_support::Get32Instance);
1583 }
1584
1585 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1586
1587 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1588
1589 EmitUpdateDexPC(dex_pc);
1590
1591 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
1592 method_object_addr, object_addr);
1593
1594 EmitGuard_ExceptionLandingPad(dex_pc);
1595
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001596 if (field_jty == kFloat || field_jty == kDouble) {
1597 field_value = irb_.CreateBitCast(field_value, irb_.getJType(field_jty));
1598 }
TDYa1275e869b62012-07-25 00:45:39 -07001599 } else {
1600 DCHECK_GE(field_offset, 0);
1601
1602 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001603 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001604
1605 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
1606
1607 llvm::Value* field_addr =
1608 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1609
TDYa1275e869b62012-07-25 00:45:39 -07001610 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001611 field_value = SignOrZeroExtendCat1Types(field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001612
1613 if (is_volatile) {
1614 irb_.CreateMemoryBarrier(art::kLoadLoad);
1615 }
TDYa1275e869b62012-07-25 00:45:39 -07001616 }
1617
TDYa1275e869b62012-07-25 00:45:39 -07001618 return field_value;
1619}
1620
1621void GBCExpanderPass::Expand_HLIPut(llvm::CallInst& call_inst,
1622 JType field_jty) {
TDYa1275e869b62012-07-25 00:45:39 -07001623 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07001624 llvm::Value* new_value = call_inst.getArgOperand(1);
1625 llvm::Value* object_addr = call_inst.getArgOperand(2);
TDYa1275e869b62012-07-25 00:45:39 -07001626 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(3));
TDYa127920be7c2012-09-10 17:13:22 -07001627 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa1275e869b62012-07-25 00:45:39 -07001628
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01001629 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa1275e869b62012-07-25 00:45:39 -07001630
1631 int field_offset;
1632 bool is_volatile;
Ian Rogers1212a022013-03-04 10:48:41 -08001633 bool is_fast_path = driver_->ComputeInstanceFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001634 field_idx, dex_compilation_unit_, field_offset, is_volatile, true);
TDYa1275e869b62012-07-25 00:45:39 -07001635
1636 if (!is_fast_path) {
1637 llvm::Function* runtime_func;
1638
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001639 if (field_jty == kFloat) {
1640 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1641 } else if (field_jty == kDouble) {
1642 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1643 }
1644
TDYa1275e869b62012-07-25 00:45:39 -07001645 if (field_jty == kObject) {
1646 runtime_func = irb_.GetRuntime(runtime_support::SetObjectInstance);
1647 } else if (field_jty == kLong || field_jty == kDouble) {
1648 runtime_func = irb_.GetRuntime(runtime_support::Set64Instance);
1649 } else {
1650 runtime_func = irb_.GetRuntime(runtime_support::Set32Instance);
1651 }
1652
1653 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1654
1655 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1656
1657 EmitUpdateDexPC(dex_pc);
1658
1659 irb_.CreateCall4(runtime_func, field_idx_value,
1660 method_object_addr, object_addr, new_value);
1661
1662 EmitGuard_ExceptionLandingPad(dex_pc);
1663
1664 } else {
1665 DCHECK_GE(field_offset, 0);
1666
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001667 if (is_volatile) {
1668 irb_.CreateMemoryBarrier(art::kStoreStore);
1669 }
1670
TDYa1275e869b62012-07-25 00:45:39 -07001671 llvm::PointerType* field_type =
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001672 irb_.getJType(field_jty)->getPointerTo();
TDYa1275e869b62012-07-25 00:45:39 -07001673
1674 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
1675
1676 llvm::Value* field_addr =
1677 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
1678
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001679 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275e869b62012-07-25 00:45:39 -07001680 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
1681
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001682 if (is_volatile) {
1683 irb_.CreateMemoryBarrier(art::kLoadLoad);
1684 }
1685
TDYa1275e869b62012-07-25 00:45:39 -07001686 if (field_jty == kObject) { // If put an object, mark the GC card table.
1687 EmitMarkGCCard(new_value, object_addr);
1688 }
1689 }
1690
1691 return;
1692}
1693
TDYa127f71bf5a2012-07-29 20:09:52 -07001694llvm::Value* GBCExpanderPass::EmitLoadConstantClass(uint32_t dex_pc,
1695 uint32_t type_idx) {
Ian Rogers89756f22013-03-04 16:40:02 -08001696 if (!driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
1697 *dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001698 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1699
1700 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1701
1702 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1703
1704 llvm::Function* runtime_func =
1705 irb_.GetRuntime(runtime_support::InitializeTypeAndVerifyAccess);
1706
1707 EmitUpdateDexPC(dex_pc);
1708
1709 llvm::Value* type_object_addr =
1710 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1711
1712 EmitGuard_ExceptionLandingPad(dex_pc);
1713
1714 return type_object_addr;
1715
1716 } else {
1717 // Try to load the class (type) object from the test cache.
1718 llvm::Value* type_field_addr =
1719 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1720
TDYa127ce4cc0d2012-11-18 16:59:53 -08001721 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07001722
Ian Rogers89756f22013-03-04 16:40:02 -08001723 if (driver_->CanAssumeTypeIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(), type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07001724 return type_object_addr;
1725 }
1726
1727 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1728
1729 // Test whether class (type) object is in the dex cache or not
1730 llvm::Value* equal_null =
1731 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1732
1733 llvm::BasicBlock* block_cont =
1734 CreateBasicBlockWithDexPC(dex_pc, "cont");
1735
1736 llvm::BasicBlock* block_load_class =
1737 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1738
1739 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
1740
1741 // Failback routine to load the class object
1742 irb_.SetInsertPoint(block_load_class);
1743
1744 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeType);
1745
1746 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1747
1748 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1749
1750 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1751
1752 EmitUpdateDexPC(dex_pc);
1753
1754 llvm::Value* loaded_type_object_addr =
1755 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1756
1757 EmitGuard_ExceptionLandingPad(dex_pc);
1758
1759 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1760
1761 irb_.CreateBr(block_cont);
1762
1763 // Now the class object must be loaded
1764 irb_.SetInsertPoint(block_cont);
1765
1766 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1767
1768 phi->addIncoming(type_object_addr, block_original);
1769 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1770
1771 return phi;
1772 }
1773}
1774
TDYa1275a26d442012-07-26 18:58:38 -07001775llvm::Value* GBCExpanderPass::EmitLoadStaticStorage(uint32_t dex_pc,
1776 uint32_t type_idx) {
1777 llvm::BasicBlock* block_load_static =
1778 CreateBasicBlockWithDexPC(dex_pc, "load_static");
1779
1780 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1781
1782 // Load static storage from dex cache
1783 llvm::Value* storage_field_addr =
1784 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
1785
TDYa127ce4cc0d2012-11-18 16:59:53 -08001786 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAARuntimeInfo);
TDYa1275a26d442012-07-26 18:58:38 -07001787
1788 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1789
1790 // Test: Is the static storage of this class initialized?
1791 llvm::Value* equal_null =
1792 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
1793
1794 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
1795
1796 // Failback routine to load the class object
1797 irb_.SetInsertPoint(block_load_static);
1798
1799 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::InitializeStaticStorage);
1800
1801 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1802
1803 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1804
1805 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
1806
1807 EmitUpdateDexPC(dex_pc);
1808
1809 llvm::Value* loaded_storage_object_addr =
1810 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
1811
1812 EmitGuard_ExceptionLandingPad(dex_pc);
1813
1814 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
1815
1816 irb_.CreateBr(block_cont);
1817
1818 // Now the class object must be loaded
1819 irb_.SetInsertPoint(block_cont);
1820
1821 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1822
1823 phi->addIncoming(storage_object_addr, block_original);
1824 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
1825
1826 return phi;
1827}
1828
1829llvm::Value* GBCExpanderPass::Expand_HLSget(llvm::CallInst& call_inst,
1830 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001831 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1832 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1833
1834 int field_offset;
1835 int ssb_index;
1836 bool is_referrers_class;
1837 bool is_volatile;
1838
Ian Rogers1212a022013-03-04 10:48:41 -08001839 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001840 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001841 is_referrers_class, is_volatile, false);
1842
1843 llvm::Value* static_field_value;
1844
1845 if (!is_fast_path) {
1846 llvm::Function* runtime_func;
1847
1848 if (field_jty == kObject) {
1849 runtime_func = irb_.GetRuntime(runtime_support::GetObjectStatic);
1850 } else if (field_jty == kLong || field_jty == kDouble) {
1851 runtime_func = irb_.GetRuntime(runtime_support::Get64Static);
1852 } else {
1853 runtime_func = irb_.GetRuntime(runtime_support::Get32Static);
1854 }
1855
1856 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1857
1858 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1859
1860 EmitUpdateDexPC(dex_pc);
1861
1862 static_field_value =
1863 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
1864
1865 EmitGuard_ExceptionLandingPad(dex_pc);
1866
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001867 if (field_jty == kFloat || field_jty == kDouble) {
1868 static_field_value = irb_.CreateBitCast(static_field_value, irb_.getJType(field_jty));
1869 }
TDYa1275a26d442012-07-26 18:58:38 -07001870 } else {
1871 DCHECK_GE(field_offset, 0);
1872
1873 llvm::Value* static_storage_addr = NULL;
1874
1875 if (is_referrers_class) {
1876 // Fast path, static storage base is this method's class
1877 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1878
1879 static_storage_addr =
1880 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001881 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001882 irb_.getJObjectTy(),
1883 kTBAAConstJObject);
1884 } else {
1885 // Medium path, static storage base in a different class which
1886 // requires checks that the other class is initialized
1887 DCHECK_GE(ssb_index, 0);
1888 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1889 }
1890
1891 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1892
1893 llvm::Value* static_field_addr =
1894 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001895 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001896
TDYa1275a26d442012-07-26 18:58:38 -07001897 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001898 static_field_value = SignOrZeroExtendCat1Types(static_field_value, field_jty);
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001899
1900 if (is_volatile) {
1901 irb_.CreateMemoryBarrier(art::kLoadLoad);
1902 }
TDYa1275a26d442012-07-26 18:58:38 -07001903 }
1904
TDYa1275a26d442012-07-26 18:58:38 -07001905 return static_field_value;
1906}
1907
1908void GBCExpanderPass::Expand_HLSput(llvm::CallInst& call_inst,
1909 JType field_jty) {
TDYa1275a26d442012-07-26 18:58:38 -07001910 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
1911 uint32_t field_idx = LV2UInt(call_inst.getArgOperand(0));
1912 llvm::Value* new_value = call_inst.getArgOperand(1);
1913
1914 if (field_jty == kFloat || field_jty == kDouble) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001915 new_value = irb_.CreateBitCast(new_value, irb_.getJType(field_jty));
TDYa1275a26d442012-07-26 18:58:38 -07001916 }
1917
1918 int field_offset;
1919 int ssb_index;
1920 bool is_referrers_class;
1921 bool is_volatile;
1922
Ian Rogers1212a022013-03-04 10:48:41 -08001923 bool is_fast_path = driver_->ComputeStaticFieldInfo(
Ian Rogers89756f22013-03-04 16:40:02 -08001924 field_idx, dex_compilation_unit_, field_offset, ssb_index,
TDYa1275a26d442012-07-26 18:58:38 -07001925 is_referrers_class, is_volatile, true);
1926
1927 if (!is_fast_path) {
1928 llvm::Function* runtime_func;
1929
1930 if (field_jty == kObject) {
1931 runtime_func = irb_.GetRuntime(runtime_support::SetObjectStatic);
1932 } else if (field_jty == kLong || field_jty == kDouble) {
1933 runtime_func = irb_.GetRuntime(runtime_support::Set64Static);
1934 } else {
1935 runtime_func = irb_.GetRuntime(runtime_support::Set32Static);
1936 }
1937
Ian Rogers1b2b71f2013-03-01 11:31:30 -08001938 if (field_jty == kFloat) {
1939 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kInt));
1940 } else if (field_jty == kDouble) {
1941 new_value = irb_.CreateBitCast(new_value, irb_.getJType(kLong));
1942 }
1943
TDYa1275a26d442012-07-26 18:58:38 -07001944 llvm::Constant* field_idx_value = irb_.getInt32(field_idx);
1945
1946 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1947
1948 EmitUpdateDexPC(dex_pc);
1949
1950 irb_.CreateCall3(runtime_func, field_idx_value,
1951 method_object_addr, new_value);
1952
1953 EmitGuard_ExceptionLandingPad(dex_pc);
1954
1955 } else {
1956 DCHECK_GE(field_offset, 0);
1957
1958 llvm::Value* static_storage_addr = NULL;
1959
1960 if (is_referrers_class) {
1961 // Fast path, static storage base is this method's class
1962 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1963
1964 static_storage_addr =
1965 irb_.LoadFromObjectOffset(method_object_addr,
Ian Rogers98573f92013-01-30 17:26:32 -08001966 art::mirror::AbstractMethod::DeclaringClassOffset().Int32Value(),
TDYa1275a26d442012-07-26 18:58:38 -07001967 irb_.getJObjectTy(),
1968 kTBAAConstJObject);
1969 } else {
1970 // Medium path, static storage base in a different class which
1971 // requires checks that the other class is initialized
1972 DCHECK_GE(ssb_index, 0);
1973 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
1974 }
1975
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001976 if (is_volatile) {
1977 irb_.CreateMemoryBarrier(art::kStoreStore);
1978 }
1979
TDYa1275a26d442012-07-26 18:58:38 -07001980 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
1981
1982 llvm::Value* static_field_addr =
1983 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001984 irb_.getJType(field_jty)->getPointerTo());
TDYa1275a26d442012-07-26 18:58:38 -07001985
Ian Rogers76ae4fe2013-02-27 16:03:41 -08001986 new_value = TruncateCat1Types(new_value, field_jty);
TDYa1275a26d442012-07-26 18:58:38 -07001987 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
1988
Sebastien Hertz4b2e0b02013-03-06 16:24:00 +01001989 if (is_volatile) {
1990 irb_.CreateMemoryBarrier(art::kStoreLoad);
1991 }
1992
TDYa1275a26d442012-07-26 18:58:38 -07001993 if (field_jty == kObject) { // If put an object, mark the GC card table.
1994 EmitMarkGCCard(new_value, static_storage_addr);
1995 }
1996 }
1997
1998 return;
1999}
2000
TDYa127f71bf5a2012-07-29 20:09:52 -07002001llvm::Value* GBCExpanderPass::Expand_ConstString(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002002 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2003 uint32_t string_idx = LV2UInt(call_inst.getArgOperand(0));
2004
2005 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
2006
TDYa127ce4cc0d2012-11-18 16:59:53 -08002007 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAARuntimeInfo);
TDYa127f71bf5a2012-07-29 20:09:52 -07002008
Ian Rogers89756f22013-03-04 16:40:02 -08002009 if (!driver_->CanAssumeStringIsPresentInDexCache(*dex_compilation_unit_->GetDexFile(),
2010 string_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002011 llvm::BasicBlock* block_str_exist =
2012 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
2013
2014 llvm::BasicBlock* block_str_resolve =
2015 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
2016
2017 llvm::BasicBlock* block_cont =
2018 CreateBasicBlockWithDexPC(dex_pc, "str_cont");
2019
2020 // Test: Is the string resolved and in the dex cache?
2021 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
2022
2023 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
2024
2025 // String is resolved, go to next basic block.
2026 irb_.SetInsertPoint(block_str_exist);
2027 irb_.CreateBr(block_cont);
2028
2029 // String is not resolved yet, resolve it now.
2030 irb_.SetInsertPoint(block_str_resolve);
2031
2032 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::ResolveString);
2033
2034 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2035
2036 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
2037
2038 EmitUpdateDexPC(dex_pc);
2039
2040 llvm::Value* result = irb_.CreateCall2(runtime_func, method_object_addr,
2041 string_idx_value);
2042
2043 EmitGuard_ExceptionLandingPad(dex_pc);
2044
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002045 irb_.CreateBr(block_cont);
2046
2047
TDYa127f71bf5a2012-07-29 20:09:52 -07002048 llvm::BasicBlock* block_pre_cont = irb_.GetInsertBlock();
2049
2050 irb_.SetInsertPoint(block_cont);
2051
2052 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2053
2054 phi->addIncoming(string_addr, block_str_exist);
2055 phi->addIncoming(result, block_pre_cont);
2056
2057 string_addr = phi;
2058 }
2059
2060 return string_addr;
2061}
2062
2063llvm::Value* GBCExpanderPass::Expand_ConstClass(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002064 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2065 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2066
2067 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
2068
2069 return type_object_addr;
2070}
2071
2072void GBCExpanderPass::Expand_MonitorEnter(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002073 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2074 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002075 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002076
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002077 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002078
TDYa127ce4cc0d2012-11-18 16:59:53 -08002079 EmitUpdateDexPC(dex_pc);
2080
TDYa127f71bf5a2012-07-29 20:09:52 -07002081 irb_.Runtime().EmitLockObject(object_addr);
2082
2083 return;
2084}
2085
2086void GBCExpanderPass::Expand_MonitorExit(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002087 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2088 llvm::Value* object_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002089 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002090
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002091 EmitGuard_NullPointerException(dex_pc, object_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002092
2093 EmitUpdateDexPC(dex_pc);
2094
2095 irb_.Runtime().EmitUnlockObject(object_addr);
2096
2097 EmitGuard_ExceptionLandingPad(dex_pc);
2098
2099 return;
2100}
2101
2102void GBCExpanderPass::Expand_HLCheckCast(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002103 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2104 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2105 llvm::Value* object_addr = call_inst.getArgOperand(1);
2106
2107 llvm::BasicBlock* block_test_class =
2108 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2109
2110 llvm::BasicBlock* block_test_sub_class =
2111 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2112
2113 llvm::BasicBlock* block_cont =
2114 CreateBasicBlockWithDexPC(dex_pc, "checkcast_cont");
2115
2116 // Test: Is the reference equal to null? Act as no-op when it is null.
2117 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2118
Ian Rogers8e696052013-03-04 09:00:40 -08002119 irb_.CreateCondBr(equal_null, block_cont, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002120
2121 // Test: Is the object instantiated from the given class?
2122 irb_.SetInsertPoint(block_test_class);
2123 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002124 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002125
2126 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2127
2128 llvm::Value* object_type_field_addr =
2129 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2130
2131 llvm::Value* object_type_object_addr =
2132 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2133
2134 llvm::Value* equal_class =
2135 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2136
Ian Rogers8e696052013-03-04 09:00:40 -08002137 irb_.CreateCondBr(equal_class, block_cont, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002138
2139 // Test: Is the object instantiated from the subclass of the given class?
2140 irb_.SetInsertPoint(block_test_sub_class);
2141
2142 EmitUpdateDexPC(dex_pc);
2143
2144 irb_.CreateCall2(irb_.GetRuntime(runtime_support::CheckCast),
2145 type_object_addr, object_type_object_addr);
2146
2147 EmitGuard_ExceptionLandingPad(dex_pc);
2148
2149 irb_.CreateBr(block_cont);
2150
2151 irb_.SetInsertPoint(block_cont);
2152
2153 return;
2154}
2155
2156llvm::Value* GBCExpanderPass::Expand_InstanceOf(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002157 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2158 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2159 llvm::Value* object_addr = call_inst.getArgOperand(1);
2160
2161 llvm::BasicBlock* block_nullp =
2162 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2163
2164 llvm::BasicBlock* block_test_class =
2165 CreateBasicBlockWithDexPC(dex_pc, "test_class");
2166
2167 llvm::BasicBlock* block_class_equals =
2168 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
2169
2170 llvm::BasicBlock* block_test_sub_class =
2171 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
2172
2173 llvm::BasicBlock* block_cont =
2174 CreateBasicBlockWithDexPC(dex_pc, "instance_of_cont");
2175
2176 // Overview of the following code :
2177 // We check for null, if so, then false, otherwise check for class == . If so
2178 // then true, otherwise do callout slowpath.
2179 //
2180 // Test: Is the reference equal to null? Set 0 when it is null.
2181 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
2182
Ian Rogers8e696052013-03-04 09:00:40 -08002183 irb_.CreateCondBr(equal_null, block_nullp, block_test_class, kUnlikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002184
2185 irb_.SetInsertPoint(block_nullp);
2186 irb_.CreateBr(block_cont);
2187
2188 // Test: Is the object instantiated from the given class?
2189 irb_.SetInsertPoint(block_test_class);
2190 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, type_idx);
Ian Rogers98573f92013-01-30 17:26:32 -08002191 DCHECK_EQ(art::mirror::Object::ClassOffset().Int32Value(), 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002192
2193 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
2194
2195 llvm::Value* object_type_field_addr =
2196 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
2197
2198 llvm::Value* object_type_object_addr =
2199 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
2200
2201 llvm::Value* equal_class =
2202 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
2203
Ian Rogers8e696052013-03-04 09:00:40 -08002204 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class, kLikely);
TDYa127f71bf5a2012-07-29 20:09:52 -07002205
2206 irb_.SetInsertPoint(block_class_equals);
2207 irb_.CreateBr(block_cont);
2208
2209 // Test: Is the object instantiated from the subclass of the given class?
2210 irb_.SetInsertPoint(block_test_sub_class);
2211 llvm::Value* result =
2212 irb_.CreateCall2(irb_.GetRuntime(runtime_support::IsAssignable),
2213 type_object_addr, object_type_object_addr);
2214 irb_.CreateBr(block_cont);
2215
2216 irb_.SetInsertPoint(block_cont);
2217
2218 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJIntTy(), 3);
2219
2220 phi->addIncoming(irb_.getJInt(0), block_nullp);
2221 phi->addIncoming(irb_.getJInt(1), block_class_equals);
2222 phi->addIncoming(result, block_test_sub_class);
2223
2224 return phi;
2225}
2226
2227llvm::Value* GBCExpanderPass::Expand_NewInstance(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002228 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2229 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2230
2231 llvm::Function* runtime_func;
Ian Rogers89756f22013-03-04 16:40:02 -08002232 if (driver_->CanAccessInstantiableTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2233 *dex_compilation_unit_->GetDexFile(),
2234 type_idx)) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002235 runtime_func = irb_.GetRuntime(runtime_support::AllocObject);
2236 } else {
2237 runtime_func = irb_.GetRuntime(runtime_support::AllocObjectWithAccessCheck);
2238 }
2239
2240 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2241
2242 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2243
2244 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2245
2246 EmitUpdateDexPC(dex_pc);
2247
2248 llvm::Value* object_addr =
2249 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
2250
2251 EmitGuard_ExceptionLandingPad(dex_pc);
2252
2253 return object_addr;
2254}
2255
2256llvm::Value* GBCExpanderPass::Expand_HLInvoke(llvm::CallInst& call_inst) {
TDYa127920be7c2012-09-10 17:13:22 -07002257 art::InvokeType invoke_type = static_cast<art::InvokeType>(LV2UInt(call_inst.getArgOperand(0)));
2258 bool is_static = (invoke_type == art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002259
2260 if (!is_static) {
2261 // Test: Is *this* parameter equal to null?
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002262 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2263 llvm::Value* this_addr = call_inst.getArgOperand(3);
2264 int opt_flags = LV2UInt(call_inst.getArgOperand(2));
2265
2266 EmitGuard_NullPointerException(dex_pc, this_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002267 }
2268
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002269 llvm::Value* result = NULL;
2270 if (EmitIntrinsic(call_inst, &result)) {
2271 return result;
TDYa127f71bf5a2012-07-29 20:09:52 -07002272 }
2273
Sebastien Hertz901d5ba2013-03-06 15:19:34 +01002274 return EmitInvoke(call_inst);
TDYa127f71bf5a2012-07-29 20:09:52 -07002275}
2276
2277llvm::Value* GBCExpanderPass::Expand_OptArrayLength(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002278 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2279 // Get the array object address
2280 llvm::Value* array_addr = call_inst.getArgOperand(1);
TDYa127920be7c2012-09-10 17:13:22 -07002281 int opt_flags = LV2UInt(call_inst.getArgOperand(0));
TDYa127f71bf5a2012-07-29 20:09:52 -07002282
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002283 EmitGuard_NullPointerException(dex_pc, array_addr, opt_flags);
TDYa127f71bf5a2012-07-29 20:09:52 -07002284
2285 // Get the array length and store it to the register
2286 return EmitLoadArrayLength(array_addr);
2287}
2288
2289llvm::Value* GBCExpanderPass::Expand_NewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002290 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2291 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(0));
2292 llvm::Value* length = call_inst.getArgOperand(1);
2293
2294 return EmitAllocNewArray(dex_pc, length, type_idx, false);
2295}
2296
2297llvm::Value* GBCExpanderPass::Expand_HLFilledNewArray(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002298 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2299 uint32_t type_idx = LV2UInt(call_inst.getArgOperand(1));
2300 uint32_t length = call_inst.getNumArgOperands() - 3;
2301
2302 llvm::Value* object_addr =
2303 EmitAllocNewArray(dex_pc, irb_.getInt32(length), type_idx, true);
2304
2305 if (length > 0) {
2306 // Check for the element type
2307 uint32_t type_desc_len = 0;
2308 const char* type_desc =
Ian Rogers89756f22013-03-04 16:40:02 -08002309 dex_compilation_unit_->GetDexFile()->StringByTypeIdx(type_idx, &type_desc_len);
TDYa127f71bf5a2012-07-29 20:09:52 -07002310
2311 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
2312 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
2313 bool is_elem_int_ty = (type_desc[1] == 'I');
2314
2315 uint32_t alignment;
2316 llvm::Constant* elem_size;
2317 llvm::PointerType* field_type;
2318
2319 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
2320 // as the element, thus we are only checking 2 cases: primitive int and
2321 // non-primitive type.
2322 if (is_elem_int_ty) {
2323 alignment = sizeof(int32_t);
2324 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
2325 field_type = irb_.getJIntTy()->getPointerTo();
2326 } else {
2327 alignment = irb_.getSizeOfPtrEquivInt();
2328 elem_size = irb_.getSizeOfPtrEquivIntValue();
2329 field_type = irb_.getJObjectTy()->getPointerTo();
2330 }
2331
2332 llvm::Value* data_field_offset =
Ian Rogers98573f92013-01-30 17:26:32 -08002333 irb_.getPtrEquivInt(art::mirror::Array::DataOffset(alignment).Int32Value());
TDYa127f71bf5a2012-07-29 20:09:52 -07002334
2335 llvm::Value* data_field_addr =
2336 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
2337
2338 // TODO: Tune this code. Currently we are generating one instruction for
2339 // one element which may be very space consuming. Maybe changing to use
2340 // memcpy may help; however, since we can't guarantee that the alloca of
2341 // dalvik register are continuous, we can't perform such optimization yet.
2342 for (uint32_t i = 0; i < length; ++i) {
2343 llvm::Value* reg_value = call_inst.getArgOperand(i+3);
2344
2345 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
2346
2347 data_field_addr =
2348 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
2349 }
2350 }
2351
2352 return object_addr;
2353}
2354
2355void GBCExpanderPass::Expand_HLFillArrayData(llvm::CallInst& call_inst) {
TDYa127f71bf5a2012-07-29 20:09:52 -07002356 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2357 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
2358 LV2SInt(call_inst.getArgOperand(0));
2359 llvm::Value* array_addr = call_inst.getArgOperand(1);
2360
TDYa127920be7c2012-09-10 17:13:22 -07002361 const art::Instruction::ArrayDataPayload* payload =
2362 reinterpret_cast<const art::Instruction::ArrayDataPayload*>(
Ian Rogers89756f22013-03-04 16:40:02 -08002363 dex_compilation_unit_->GetCodeItem()->insns_ + payload_offset);
TDYa127f71bf5a2012-07-29 20:09:52 -07002364
2365 if (payload->element_count == 0) {
2366 // When the number of the elements in the payload is zero, we don't have
2367 // to copy any numbers. However, we should check whether the array object
2368 // address is equal to null or not.
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002369 EmitGuard_NullPointerException(dex_pc, array_addr, 0);
TDYa127f71bf5a2012-07-29 20:09:52 -07002370 } else {
2371 // To save the code size, we are going to call the runtime function to
2372 // copy the content from DexFile.
2373
2374 // NOTE: We will check for the NullPointerException in the runtime.
2375
2376 llvm::Function* runtime_func = irb_.GetRuntime(runtime_support::FillArrayData);
2377
2378 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2379
2380 EmitUpdateDexPC(dex_pc);
2381
2382 irb_.CreateCall4(runtime_func,
2383 method_object_addr, irb_.getInt32(dex_pc),
2384 array_addr, irb_.getInt32(payload_offset));
2385
2386 EmitGuard_ExceptionLandingPad(dex_pc);
2387 }
2388
2389 return;
2390}
2391
2392llvm::Value* GBCExpanderPass::EmitAllocNewArray(uint32_t dex_pc,
2393 llvm::Value* array_length_value,
2394 uint32_t type_idx,
2395 bool is_filled_new_array) {
2396 llvm::Function* runtime_func;
2397
2398 bool skip_access_check =
Ian Rogers89756f22013-03-04 16:40:02 -08002399 driver_->CanAccessTypeWithoutChecks(dex_compilation_unit_->GetDexMethodIndex(),
2400 *dex_compilation_unit_->GetDexFile(), type_idx);
TDYa127f71bf5a2012-07-29 20:09:52 -07002401
2402
2403 if (is_filled_new_array) {
2404 runtime_func = skip_access_check ?
2405 irb_.GetRuntime(runtime_support::CheckAndAllocArray) :
2406 irb_.GetRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck);
2407 } else {
2408 runtime_func = skip_access_check ?
2409 irb_.GetRuntime(runtime_support::AllocArray) :
2410 irb_.GetRuntime(runtime_support::AllocArrayWithAccessCheck);
2411 }
2412
2413 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
2414
2415 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2416
2417 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2418
2419 EmitUpdateDexPC(dex_pc);
2420
2421 llvm::Value* object_addr =
2422 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
2423 array_length_value, thread_object_addr);
2424
2425 EmitGuard_ExceptionLandingPad(dex_pc);
2426
2427 return object_addr;
2428}
2429
2430llvm::Value* GBCExpanderPass::
2431EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
TDYa127920be7c2012-09-10 17:13:22 -07002432 art::InvokeType invoke_type,
TDYa127f71bf5a2012-07-29 20:09:52 -07002433 llvm::Value* this_addr,
2434 uint32_t dex_pc,
2435 bool is_fast_path) {
2436
2437 llvm::Function* runtime_func = NULL;
2438
2439 switch (invoke_type) {
TDYa127920be7c2012-09-10 17:13:22 -07002440 case art::kStatic:
TDYa127f71bf5a2012-07-29 20:09:52 -07002441 runtime_func = irb_.GetRuntime(runtime_support::FindStaticMethodWithAccessCheck);
2442 break;
2443
TDYa127920be7c2012-09-10 17:13:22 -07002444 case art::kDirect:
TDYa127f71bf5a2012-07-29 20:09:52 -07002445 runtime_func = irb_.GetRuntime(runtime_support::FindDirectMethodWithAccessCheck);
2446 break;
2447
TDYa127920be7c2012-09-10 17:13:22 -07002448 case art::kVirtual:
TDYa127f71bf5a2012-07-29 20:09:52 -07002449 runtime_func = irb_.GetRuntime(runtime_support::FindVirtualMethodWithAccessCheck);
2450 break;
2451
TDYa127920be7c2012-09-10 17:13:22 -07002452 case art::kSuper:
TDYa127f71bf5a2012-07-29 20:09:52 -07002453 runtime_func = irb_.GetRuntime(runtime_support::FindSuperMethodWithAccessCheck);
2454 break;
2455
TDYa127920be7c2012-09-10 17:13:22 -07002456 case art::kInterface:
TDYa127f71bf5a2012-07-29 20:09:52 -07002457 if (is_fast_path) {
2458 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethod);
2459 } else {
2460 runtime_func = irb_.GetRuntime(runtime_support::FindInterfaceMethodWithAccessCheck);
2461 }
2462 break;
2463 }
2464
2465 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2466
2467 if (this_addr == NULL) {
TDYa127920be7c2012-09-10 17:13:22 -07002468 DCHECK_EQ(invoke_type, art::kStatic);
TDYa127f71bf5a2012-07-29 20:09:52 -07002469 this_addr = irb_.getJNull();
2470 }
2471
2472 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2473
2474 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
2475
2476 EmitUpdateDexPC(dex_pc);
2477
2478 llvm::Value* callee_method_object_addr =
2479 irb_.CreateCall4(runtime_func,
2480 callee_method_idx_value,
2481 this_addr,
2482 caller_method_object_addr,
2483 thread_object_addr);
2484
2485 EmitGuard_ExceptionLandingPad(dex_pc);
2486
2487 return callee_method_object_addr;
2488}
2489
TDYa1275e869b62012-07-25 00:45:39 -07002490void GBCExpanderPass::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2491 // Using runtime support, let the target can override by InlineAssembly.
2492 irb_.Runtime().EmitMarkGCCard(value, target_addr);
2493}
2494
2495void GBCExpanderPass::EmitUpdateDexPC(uint32_t dex_pc) {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002496 if (shadow_frame_ == NULL) {
2497 return;
2498 }
TDYa1275e869b62012-07-25 00:45:39 -07002499 irb_.StoreToObjectOffset(shadow_frame_,
TDYa127920be7c2012-09-10 17:13:22 -07002500 art::ShadowFrame::DexPCOffset(),
TDYa1275e869b62012-07-25 00:45:39 -07002501 irb_.getInt32(dex_pc),
2502 kTBAAShadowFrame);
2503}
2504
2505void GBCExpanderPass::EmitGuard_DivZeroException(uint32_t dex_pc,
2506 llvm::Value* denominator,
2507 JType op_jty) {
2508 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2509
2510 llvm::Constant* zero = irb_.getJZero(op_jty);
2511
2512 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2513
2514 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2515
2516 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2517
2518 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
2519
2520 irb_.SetInsertPoint(block_exception);
2521 EmitUpdateDexPC(dex_pc);
2522 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowDivZeroException));
2523 EmitBranchExceptionLandingPad(dex_pc);
2524
2525 irb_.SetInsertPoint(block_continue);
2526}
2527
2528void GBCExpanderPass::EmitGuard_NullPointerException(uint32_t dex_pc,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002529 llvm::Value* object,
2530 int opt_flags) {
2531 bool ignore_null_check = ((opt_flags & MIR_IGNORE_NULL_CHECK) != 0);
2532 if (ignore_null_check) {
2533 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2534 if (lpad) {
2535 // There is at least one catch: create a "fake" conditional branch to
2536 // keep the exception edge to the catch block.
2537 landing_pad_phi_mapping_[lpad].push_back(
2538 std::make_pair(current_bb_->getUniquePredecessor(),
2539 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002540
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002541 llvm::BasicBlock* block_continue =
2542 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002543
Ian Rogers8e696052013-03-04 09:00:40 -08002544 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002545
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002546 irb_.SetInsertPoint(block_continue);
2547 }
2548 } else {
2549 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
TDYa1275e869b62012-07-25 00:45:39 -07002550
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002551 llvm::BasicBlock* block_exception =
2552 CreateBasicBlockWithDexPC(dex_pc, "nullp");
TDYa1275e869b62012-07-25 00:45:39 -07002553
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002554 llvm::BasicBlock* block_continue =
2555 CreateBasicBlockWithDexPC(dex_pc, "cont");
2556
2557 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
2558
2559 irb_.SetInsertPoint(block_exception);
2560 EmitUpdateDexPC(dex_pc);
2561 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowNullPointerException),
2562 irb_.getInt32(dex_pc));
2563 EmitBranchExceptionLandingPad(dex_pc);
2564
2565 irb_.SetInsertPoint(block_continue);
2566 }
TDYa1275e869b62012-07-25 00:45:39 -07002567}
2568
2569void
2570GBCExpanderPass::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2571 llvm::Value* array,
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002572 llvm::Value* index,
2573 int opt_flags) {
2574 bool ignore_range_check = ((opt_flags & MIR_IGNORE_RANGE_CHECK) != 0);
2575 if (ignore_range_check) {
2576 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
2577 if (lpad) {
2578 // There is at least one catch: create a "fake" conditional branch to
2579 // keep the exception edge to the catch block.
2580 landing_pad_phi_mapping_[lpad].push_back(
2581 std::make_pair(current_bb_->getUniquePredecessor(),
2582 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002583
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002584 llvm::BasicBlock* block_continue =
2585 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002586
Ian Rogers8e696052013-03-04 09:00:40 -08002587 irb_.CreateCondBr(irb_.getFalse(), lpad, block_continue, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002588
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002589 irb_.SetInsertPoint(block_continue);
2590 }
2591 } else {
2592 llvm::Value* array_len = EmitLoadArrayLength(array);
TDYa1275e869b62012-07-25 00:45:39 -07002593
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002594 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
TDYa1275e869b62012-07-25 00:45:39 -07002595
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002596 llvm::BasicBlock* block_exception =
2597 CreateBasicBlockWithDexPC(dex_pc, "overflow");
TDYa1275e869b62012-07-25 00:45:39 -07002598
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002599 llvm::BasicBlock* block_continue =
2600 CreateBasicBlockWithDexPC(dex_pc, "cont");
TDYa1275e869b62012-07-25 00:45:39 -07002601
Sebastien Hertz2ffe4f12013-03-01 12:12:30 +01002602 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
2603
2604 irb_.SetInsertPoint(block_exception);
2605
2606 EmitUpdateDexPC(dex_pc);
2607 irb_.CreateCall2(irb_.GetRuntime(runtime_support::ThrowIndexOutOfBounds), index, array_len);
2608 EmitBranchExceptionLandingPad(dex_pc);
2609
2610 irb_.SetInsertPoint(block_continue);
2611 }
TDYa1275e869b62012-07-25 00:45:39 -07002612}
2613
Ian Rogers8e696052013-03-04 09:00:40 -08002614llvm::FunctionType* GBCExpanderPass::GetFunctionType(llvm::Type* ret_type, uint32_t method_idx,
TDYa1275e869b62012-07-25 00:45:39 -07002615 bool is_static) {
2616 // Get method signature
Ian Rogers8e696052013-03-04 09:00:40 -08002617 art::DexFile::MethodId const& method_id =
Ian Rogers89756f22013-03-04 16:40:02 -08002618 dex_compilation_unit_->GetDexFile()->GetMethodId(method_idx);
TDYa1275e869b62012-07-25 00:45:39 -07002619
2620 uint32_t shorty_size;
Ian Rogers89756f22013-03-04 16:40:02 -08002621 const char* shorty = dex_compilation_unit_->GetDexFile()->GetMethodShorty(method_id, &shorty_size);
TDYa1275e869b62012-07-25 00:45:39 -07002622 CHECK_GE(shorty_size, 1u);
2623
TDYa1275e869b62012-07-25 00:45:39 -07002624 // Get argument type
2625 std::vector<llvm::Type*> args_type;
2626
2627 args_type.push_back(irb_.getJObjectTy()); // method object pointer
2628
2629 if (!is_static) {
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002630 args_type.push_back(irb_.getJType('L')); // "this" object pointer
TDYa1275e869b62012-07-25 00:45:39 -07002631 }
2632
2633 for (uint32_t i = 1; i < shorty_size; ++i) {
buzbee26f10ee2012-12-21 11:16:29 -08002634 char shorty_type = art::RemapShorty(shorty[i]);
Ian Rogers76ae4fe2013-02-27 16:03:41 -08002635 args_type.push_back(irb_.getJType(shorty_type));
TDYa1275e869b62012-07-25 00:45:39 -07002636 }
2637
2638 return llvm::FunctionType::get(ret_type, args_type, false);
2639}
2640
2641
2642llvm::BasicBlock* GBCExpanderPass::
2643CreateBasicBlockWithDexPC(uint32_t dex_pc, const char* postfix) {
2644 std::string name;
2645
2646#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002647 art::StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
TDYa1275e869b62012-07-25 00:45:39 -07002648#endif
2649
2650 return llvm::BasicBlock::Create(context_, name, func_);
2651}
2652
2653llvm::BasicBlock* GBCExpanderPass::GetBasicBlock(uint32_t dex_pc) {
Ian Rogers89756f22013-03-04 16:40:02 -08002654 DCHECK(dex_pc < dex_compilation_unit_->GetCodeItem()->insns_size_in_code_units_);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07002655 CHECK(basic_blocks_[dex_pc] != NULL);
TDYa1275e869b62012-07-25 00:45:39 -07002656 return basic_blocks_[dex_pc];
2657}
2658
2659int32_t GBCExpanderPass::GetTryItemOffset(uint32_t dex_pc) {
2660 int32_t min = 0;
Ian Rogers89756f22013-03-04 16:40:02 -08002661 int32_t max = dex_compilation_unit_->GetCodeItem()->tries_size_ - 1;
TDYa1275e869b62012-07-25 00:45:39 -07002662
2663 while (min <= max) {
2664 int32_t mid = min + (max - min) / 2;
2665
Ian Rogers89756f22013-03-04 16:40:02 -08002666 const art::DexFile::TryItem* ti =
2667 art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(), mid);
TDYa1275e869b62012-07-25 00:45:39 -07002668 uint32_t start = ti->start_addr_;
2669 uint32_t end = start + ti->insn_count_;
2670
2671 if (dex_pc < start) {
2672 max = mid - 1;
2673 } else if (dex_pc >= end) {
2674 min = mid + 1;
2675 } else {
2676 return mid; // found
2677 }
2678 }
2679
2680 return -1; // not found
2681}
2682
2683llvm::BasicBlock* GBCExpanderPass::GetLandingPadBasicBlock(uint32_t dex_pc) {
2684 // Find the try item for this address in this method
2685 int32_t ti_offset = GetTryItemOffset(dex_pc);
2686
2687 if (ti_offset == -1) {
2688 return NULL; // No landing pad is available for this address.
2689 }
2690
2691 // Check for the existing landing pad basic block
2692 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2693 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2694
2695 if (block_lpad) {
2696 // We have generated landing pad for this try item already. Return the
2697 // same basic block.
2698 return block_lpad;
2699 }
2700
2701 // Get try item from code item
Ian Rogers89756f22013-03-04 16:40:02 -08002702 const art::DexFile::TryItem* ti = art::DexFile::GetTryItems(*dex_compilation_unit_->GetCodeItem(),
Ian Rogers8e696052013-03-04 09:00:40 -08002703 ti_offset);
TDYa1275e869b62012-07-25 00:45:39 -07002704
2705 std::string lpadname;
2706
2707#if !defined(NDEBUG)
TDYa127920be7c2012-09-10 17:13:22 -07002708 art::StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
TDYa1275e869b62012-07-25 00:45:39 -07002709#endif
2710
2711 // Create landing pad basic block
2712 block_lpad = llvm::BasicBlock::Create(context_, lpadname, func_);
2713
2714 // Change IRBuilder insert point
2715 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2716 irb_.SetInsertPoint(block_lpad);
2717
2718 // Find catch block with matching type
2719 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2720
2721 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
2722
2723 llvm::Value* catch_handler_index_value =
2724 irb_.CreateCall2(irb_.GetRuntime(runtime_support::FindCatchBlock),
2725 method_object_addr, ti_offset_value);
2726
2727 // Switch instruction (Go to unwind basic block by default)
2728 llvm::SwitchInst* sw =
2729 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2730
2731 // Cases with matched catch block
Ian Rogers89756f22013-03-04 16:40:02 -08002732 art::CatchHandlerIterator iter(*dex_compilation_unit_->GetCodeItem(), ti->start_addr_);
TDYa1275e869b62012-07-25 00:45:39 -07002733
2734 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2735 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2736 }
2737
2738 // Restore the orignal insert point for IRBuilder
2739 irb_.restoreIP(irb_ip_original);
2740
2741 // Cache this landing pad
2742 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2743 basic_block_landing_pads_[ti_offset] = block_lpad;
2744
2745 return block_lpad;
2746}
2747
2748llvm::BasicBlock* GBCExpanderPass::GetUnwindBasicBlock() {
2749 // Check the existing unwinding baisc block block
2750 if (basic_block_unwind_ != NULL) {
2751 return basic_block_unwind_;
2752 }
2753
2754 // Create new basic block for unwinding
2755 basic_block_unwind_ =
2756 llvm::BasicBlock::Create(context_, "exception_unwind", func_);
2757
2758 // Change IRBuilder insert point
2759 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2760 irb_.SetInsertPoint(basic_block_unwind_);
2761
2762 // Pop the shadow frame
2763 Expand_PopShadowFrame();
2764
2765 // Emit the code to return default value (zero) for the given return type.
Ian Rogers89756f22013-03-04 16:40:02 -08002766 char ret_shorty = dex_compilation_unit_->GetShorty()[0];
buzbee26f10ee2012-12-21 11:16:29 -08002767 ret_shorty = art::RemapShorty(ret_shorty);
TDYa1275e869b62012-07-25 00:45:39 -07002768 if (ret_shorty == 'V') {
2769 irb_.CreateRetVoid();
2770 } else {
2771 irb_.CreateRet(irb_.getJZero(ret_shorty));
2772 }
2773
2774 // Restore the orignal insert point for IRBuilder
2775 irb_.restoreIP(irb_ip_original);
2776
2777 return basic_block_unwind_;
2778}
2779
2780void GBCExpanderPass::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2781 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002782 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002783 irb_.GetInsertBlock()));
TDYa1275e869b62012-07-25 00:45:39 -07002784 irb_.CreateBr(lpad);
2785 } else {
2786 irb_.CreateBr(GetUnwindBasicBlock());
2787 }
2788}
2789
2790void GBCExpanderPass::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
Jeff Hao9a142652013-01-17 23:10:19 +00002791 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
2792
TDYa1275e869b62012-07-25 00:45:39 -07002793 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2794
2795 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa12755e5e6c2012-09-11 15:14:42 -07002796 landing_pad_phi_mapping_[lpad].push_back(std::make_pair(current_bb_->getUniquePredecessor(),
TDYa127aa558872012-08-16 05:11:07 -07002797 irb_.GetInsertBlock()));
Jeff Hao9a142652013-01-17 23:10:19 +00002798 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002799 } else {
Jeff Hao9a142652013-01-17 23:10:19 +00002800 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
TDYa1275e869b62012-07-25 00:45:39 -07002801 }
2802
2803 irb_.SetInsertPoint(block_cont);
2804}
2805
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002806llvm::Value*
2807GBCExpanderPass::ExpandIntrinsic(IntrinsicHelper::IntrinsicId intr_id,
2808 llvm::CallInst& call_inst) {
2809 switch (intr_id) {
2810 //==- Thread -----------------------------------------------------------==//
2811 case IntrinsicHelper::GetCurrentThread: {
TDYa127b672d1e2012-06-28 21:21:45 -07002812 return irb_.Runtime().EmitGetCurrentThread();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002813 }
Logan Chien75e4b602012-07-23 14:24:12 -07002814 case IntrinsicHelper::CheckSuspend: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08002815 Expand_TestSuspend(call_inst);
TDYa127890ea892012-08-22 10:49:42 -07002816 return NULL;
2817 }
2818 case IntrinsicHelper::TestSuspend: {
Logan Chiend54a23d2012-07-24 11:19:23 -07002819 Expand_TestSuspend(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002820 return NULL;
2821 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002822 case IntrinsicHelper::MarkGCCard: {
TDYa1279a129452012-07-19 03:10:08 -07002823 Expand_MarkGCCard(call_inst);
2824 return NULL;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002825 }
Logan Chien75e4b602012-07-23 14:24:12 -07002826
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002827 //==- Exception --------------------------------------------------------==//
2828 case IntrinsicHelper::ThrowException: {
2829 return ExpandToRuntime(runtime_support::ThrowException, call_inst);
2830 }
TDYa127f71bf5a2012-07-29 20:09:52 -07002831 case IntrinsicHelper::HLThrowException: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002832 uint32_t dex_pc = LV2UInt(call_inst.getMetadata("DexOff")->getOperand(0));
2833
2834 EmitUpdateDexPC(dex_pc);
2835
2836 irb_.CreateCall(irb_.GetRuntime(runtime_support::ThrowException),
2837 call_inst.getArgOperand(0));
2838
2839 EmitGuard_ExceptionLandingPad(dex_pc);
2840 return NULL;
2841 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002842 case IntrinsicHelper::GetException: {
TDYa127823433d2012-09-26 16:03:51 -07002843 return irb_.Runtime().EmitGetAndClearException();
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002844 }
2845 case IntrinsicHelper::IsExceptionPending: {
2846 return irb_.Runtime().EmitIsExceptionPending();
2847 }
2848 case IntrinsicHelper::FindCatchBlock: {
2849 return ExpandToRuntime(runtime_support::FindCatchBlock, call_inst);
2850 }
2851 case IntrinsicHelper::ThrowDivZeroException: {
2852 return ExpandToRuntime(runtime_support::ThrowDivZeroException, call_inst);
2853 }
2854 case IntrinsicHelper::ThrowNullPointerException: {
2855 return ExpandToRuntime(runtime_support::ThrowNullPointerException, call_inst);
2856 }
2857 case IntrinsicHelper::ThrowIndexOutOfBounds: {
2858 return ExpandToRuntime(runtime_support::ThrowIndexOutOfBounds, call_inst);
2859 }
Logan Chien75e4b602012-07-23 14:24:12 -07002860
2861 //==- Const String -----------------------------------------------------==//
2862 case IntrinsicHelper::ConstString: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002863 return Expand_ConstString(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002864 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002865 case IntrinsicHelper::LoadStringFromDexCache: {
2866 return Expand_LoadStringFromDexCache(call_inst.getArgOperand(0));
2867 }
2868 case IntrinsicHelper::ResolveString: {
2869 return ExpandToRuntime(runtime_support::ResolveString, call_inst);
2870 }
Logan Chien75e4b602012-07-23 14:24:12 -07002871
2872 //==- Const Class ------------------------------------------------------==//
2873 case IntrinsicHelper::ConstClass: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002874 return Expand_ConstClass(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002875 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002876 case IntrinsicHelper::InitializeTypeAndVerifyAccess: {
2877 return ExpandToRuntime(runtime_support::InitializeTypeAndVerifyAccess, call_inst);
2878 }
2879 case IntrinsicHelper::LoadTypeFromDexCache: {
2880 return Expand_LoadTypeFromDexCache(call_inst.getArgOperand(0));
2881 }
2882 case IntrinsicHelper::InitializeType: {
2883 return ExpandToRuntime(runtime_support::InitializeType, call_inst);
2884 }
Logan Chien75e4b602012-07-23 14:24:12 -07002885
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002886 //==- Lock -------------------------------------------------------------==//
2887 case IntrinsicHelper::LockObject: {
2888 Expand_LockObject(call_inst.getArgOperand(0));
2889 return NULL;
2890 }
2891 case IntrinsicHelper::UnlockObject: {
2892 Expand_UnlockObject(call_inst.getArgOperand(0));
2893 return NULL;
2894 }
Logan Chien75e4b602012-07-23 14:24:12 -07002895
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002896 //==- Cast -------------------------------------------------------------==//
2897 case IntrinsicHelper::CheckCast: {
2898 return ExpandToRuntime(runtime_support::CheckCast, call_inst);
2899 }
Logan Chien75e4b602012-07-23 14:24:12 -07002900 case IntrinsicHelper::HLCheckCast: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002901 Expand_HLCheckCast(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002902 return NULL;
2903 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002904 case IntrinsicHelper::IsAssignable: {
2905 return ExpandToRuntime(runtime_support::IsAssignable, call_inst);
2906 }
Logan Chien75e4b602012-07-23 14:24:12 -07002907
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002908 //==- Alloc ------------------------------------------------------------==//
2909 case IntrinsicHelper::AllocObject: {
2910 return ExpandToRuntime(runtime_support::AllocObject, call_inst);
2911 }
2912 case IntrinsicHelper::AllocObjectWithAccessCheck: {
2913 return ExpandToRuntime(runtime_support::AllocObjectWithAccessCheck, call_inst);
2914 }
Logan Chien75e4b602012-07-23 14:24:12 -07002915
2916 //==- Instance ---------------------------------------------------------==//
2917 case IntrinsicHelper::NewInstance: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002918 return Expand_NewInstance(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002919 }
2920 case IntrinsicHelper::InstanceOf: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002921 return Expand_InstanceOf(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002922 }
2923
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002924 //==- Array ------------------------------------------------------------==//
Logan Chien75e4b602012-07-23 14:24:12 -07002925 case IntrinsicHelper::NewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002926 return Expand_NewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002927 }
2928 case IntrinsicHelper::OptArrayLength: {
TDYa127f71bf5a2012-07-29 20:09:52 -07002929 return Expand_OptArrayLength(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07002930 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07002931 case IntrinsicHelper::ArrayLength: {
2932 return EmitLoadArrayLength(call_inst.getArgOperand(0));
2933 }
2934 case IntrinsicHelper::AllocArray: {
2935 return ExpandToRuntime(runtime_support::AllocArray, call_inst);
2936 }
2937 case IntrinsicHelper::AllocArrayWithAccessCheck: {
2938 return ExpandToRuntime(runtime_support::AllocArrayWithAccessCheck,
2939 call_inst);
2940 }
2941 case IntrinsicHelper::CheckAndAllocArray: {
2942 return ExpandToRuntime(runtime_support::CheckAndAllocArray, call_inst);
2943 }
2944 case IntrinsicHelper::CheckAndAllocArrayWithAccessCheck: {
2945 return ExpandToRuntime(runtime_support::CheckAndAllocArrayWithAccessCheck,
2946 call_inst);
2947 }
2948 case IntrinsicHelper::ArrayGet: {
2949 return Expand_ArrayGet(call_inst.getArgOperand(0),
2950 call_inst.getArgOperand(1),
2951 kInt);
2952 }
2953 case IntrinsicHelper::ArrayGetWide: {
2954 return Expand_ArrayGet(call_inst.getArgOperand(0),
2955 call_inst.getArgOperand(1),
2956 kLong);
2957 }
2958 case IntrinsicHelper::ArrayGetObject: {
2959 return Expand_ArrayGet(call_inst.getArgOperand(0),
2960 call_inst.getArgOperand(1),
2961 kObject);
2962 }
2963 case IntrinsicHelper::ArrayGetBoolean: {
2964 return Expand_ArrayGet(call_inst.getArgOperand(0),
2965 call_inst.getArgOperand(1),
2966 kBoolean);
2967 }
2968 case IntrinsicHelper::ArrayGetByte: {
2969 return Expand_ArrayGet(call_inst.getArgOperand(0),
2970 call_inst.getArgOperand(1),
2971 kByte);
2972 }
2973 case IntrinsicHelper::ArrayGetChar: {
2974 return Expand_ArrayGet(call_inst.getArgOperand(0),
2975 call_inst.getArgOperand(1),
2976 kChar);
2977 }
2978 case IntrinsicHelper::ArrayGetShort: {
2979 return Expand_ArrayGet(call_inst.getArgOperand(0),
2980 call_inst.getArgOperand(1),
2981 kShort);
2982 }
2983 case IntrinsicHelper::ArrayPut: {
2984 Expand_ArrayPut(call_inst.getArgOperand(0),
2985 call_inst.getArgOperand(1),
2986 call_inst.getArgOperand(2),
2987 kInt);
2988 return NULL;
2989 }
2990 case IntrinsicHelper::ArrayPutWide: {
2991 Expand_ArrayPut(call_inst.getArgOperand(0),
2992 call_inst.getArgOperand(1),
2993 call_inst.getArgOperand(2),
2994 kLong);
2995 return NULL;
2996 }
2997 case IntrinsicHelper::ArrayPutObject: {
2998 Expand_ArrayPut(call_inst.getArgOperand(0),
2999 call_inst.getArgOperand(1),
3000 call_inst.getArgOperand(2),
3001 kObject);
3002 return NULL;
3003 }
3004 case IntrinsicHelper::ArrayPutBoolean: {
3005 Expand_ArrayPut(call_inst.getArgOperand(0),
3006 call_inst.getArgOperand(1),
3007 call_inst.getArgOperand(2),
3008 kBoolean);
3009 return NULL;
3010 }
3011 case IntrinsicHelper::ArrayPutByte: {
3012 Expand_ArrayPut(call_inst.getArgOperand(0),
3013 call_inst.getArgOperand(1),
3014 call_inst.getArgOperand(2),
3015 kByte);
3016 return NULL;
3017 }
3018 case IntrinsicHelper::ArrayPutChar: {
3019 Expand_ArrayPut(call_inst.getArgOperand(0),
3020 call_inst.getArgOperand(1),
3021 call_inst.getArgOperand(2),
3022 kChar);
3023 return NULL;
3024 }
3025 case IntrinsicHelper::ArrayPutShort: {
3026 Expand_ArrayPut(call_inst.getArgOperand(0),
3027 call_inst.getArgOperand(1),
3028 call_inst.getArgOperand(2),
3029 kShort);
3030 return NULL;
3031 }
3032 case IntrinsicHelper::CheckPutArrayElement: {
3033 return ExpandToRuntime(runtime_support::CheckPutArrayElement, call_inst);
3034 }
3035 case IntrinsicHelper::FilledNewArray: {
3036 Expand_FilledNewArray(call_inst);
3037 return NULL;
3038 }
3039 case IntrinsicHelper::FillArrayData: {
3040 return ExpandToRuntime(runtime_support::FillArrayData, call_inst);
3041 }
Logan Chien75e4b602012-07-23 14:24:12 -07003042 case IntrinsicHelper::HLFillArrayData: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003043 Expand_HLFillArrayData(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003044 return NULL;
3045 }
3046 case IntrinsicHelper::HLFilledNewArray: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003047 return Expand_HLFilledNewArray(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003048 }
3049
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003050 //==- Instance Field ---------------------------------------------------==//
3051 case IntrinsicHelper::InstanceFieldGet:
3052 case IntrinsicHelper::InstanceFieldGetBoolean:
3053 case IntrinsicHelper::InstanceFieldGetByte:
3054 case IntrinsicHelper::InstanceFieldGetChar:
3055 case IntrinsicHelper::InstanceFieldGetShort: {
3056 return ExpandToRuntime(runtime_support::Get32Instance, call_inst);
3057 }
3058 case IntrinsicHelper::InstanceFieldGetWide: {
3059 return ExpandToRuntime(runtime_support::Get64Instance, call_inst);
3060 }
3061 case IntrinsicHelper::InstanceFieldGetObject: {
3062 return ExpandToRuntime(runtime_support::GetObjectInstance, call_inst);
3063 }
3064 case IntrinsicHelper::InstanceFieldGetFast: {
3065 return Expand_IGetFast(call_inst.getArgOperand(0),
3066 call_inst.getArgOperand(1),
3067 call_inst.getArgOperand(2),
3068 kInt);
3069 }
3070 case IntrinsicHelper::InstanceFieldGetWideFast: {
3071 return Expand_IGetFast(call_inst.getArgOperand(0),
3072 call_inst.getArgOperand(1),
3073 call_inst.getArgOperand(2),
3074 kLong);
3075 }
3076 case IntrinsicHelper::InstanceFieldGetObjectFast: {
3077 return Expand_IGetFast(call_inst.getArgOperand(0),
3078 call_inst.getArgOperand(1),
3079 call_inst.getArgOperand(2),
3080 kObject);
3081 }
3082 case IntrinsicHelper::InstanceFieldGetBooleanFast: {
3083 return Expand_IGetFast(call_inst.getArgOperand(0),
3084 call_inst.getArgOperand(1),
3085 call_inst.getArgOperand(2),
3086 kBoolean);
3087 }
3088 case IntrinsicHelper::InstanceFieldGetByteFast: {
3089 return Expand_IGetFast(call_inst.getArgOperand(0),
3090 call_inst.getArgOperand(1),
3091 call_inst.getArgOperand(2),
3092 kByte);
3093 }
3094 case IntrinsicHelper::InstanceFieldGetCharFast: {
3095 return Expand_IGetFast(call_inst.getArgOperand(0),
3096 call_inst.getArgOperand(1),
3097 call_inst.getArgOperand(2),
3098 kChar);
3099 }
3100 case IntrinsicHelper::InstanceFieldGetShortFast: {
3101 return Expand_IGetFast(call_inst.getArgOperand(0),
3102 call_inst.getArgOperand(1),
3103 call_inst.getArgOperand(2),
3104 kShort);
3105 }
3106 case IntrinsicHelper::InstanceFieldPut:
3107 case IntrinsicHelper::InstanceFieldPutBoolean:
3108 case IntrinsicHelper::InstanceFieldPutByte:
3109 case IntrinsicHelper::InstanceFieldPutChar:
3110 case IntrinsicHelper::InstanceFieldPutShort: {
3111 return ExpandToRuntime(runtime_support::Set32Instance, call_inst);
3112 }
3113 case IntrinsicHelper::InstanceFieldPutWide: {
3114 return ExpandToRuntime(runtime_support::Set64Instance, call_inst);
3115 }
3116 case IntrinsicHelper::InstanceFieldPutObject: {
3117 return ExpandToRuntime(runtime_support::SetObjectInstance, call_inst);
3118 }
3119 case IntrinsicHelper::InstanceFieldPutFast: {
3120 Expand_IPutFast(call_inst.getArgOperand(0),
3121 call_inst.getArgOperand(1),
3122 call_inst.getArgOperand(2),
3123 call_inst.getArgOperand(3),
3124 kInt);
3125 return NULL;
3126 }
3127 case IntrinsicHelper::InstanceFieldPutWideFast: {
3128 Expand_IPutFast(call_inst.getArgOperand(0),
3129 call_inst.getArgOperand(1),
3130 call_inst.getArgOperand(2),
3131 call_inst.getArgOperand(3),
3132 kLong);
3133 return NULL;
3134 }
3135 case IntrinsicHelper::InstanceFieldPutObjectFast: {
3136 Expand_IPutFast(call_inst.getArgOperand(0),
3137 call_inst.getArgOperand(1),
3138 call_inst.getArgOperand(2),
3139 call_inst.getArgOperand(3),
3140 kObject);
3141 return NULL;
3142 }
3143 case IntrinsicHelper::InstanceFieldPutBooleanFast: {
3144 Expand_IPutFast(call_inst.getArgOperand(0),
3145 call_inst.getArgOperand(1),
3146 call_inst.getArgOperand(2),
3147 call_inst.getArgOperand(3),
3148 kBoolean);
3149 return NULL;
3150 }
3151 case IntrinsicHelper::InstanceFieldPutByteFast: {
3152 Expand_IPutFast(call_inst.getArgOperand(0),
3153 call_inst.getArgOperand(1),
3154 call_inst.getArgOperand(2),
3155 call_inst.getArgOperand(3),
3156 kByte);
3157 return NULL;
3158 }
3159 case IntrinsicHelper::InstanceFieldPutCharFast: {
3160 Expand_IPutFast(call_inst.getArgOperand(0),
3161 call_inst.getArgOperand(1),
3162 call_inst.getArgOperand(2),
3163 call_inst.getArgOperand(3),
3164 kChar);
3165 return NULL;
3166 }
3167 case IntrinsicHelper::InstanceFieldPutShortFast: {
3168 Expand_IPutFast(call_inst.getArgOperand(0),
3169 call_inst.getArgOperand(1),
3170 call_inst.getArgOperand(2),
3171 call_inst.getArgOperand(3),
3172 kShort);
3173 return NULL;
3174 }
Logan Chien75e4b602012-07-23 14:24:12 -07003175
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003176 //==- Static Field -----------------------------------------------------==//
3177 case IntrinsicHelper::StaticFieldGet:
3178 case IntrinsicHelper::StaticFieldGetBoolean:
3179 case IntrinsicHelper::StaticFieldGetByte:
3180 case IntrinsicHelper::StaticFieldGetChar:
3181 case IntrinsicHelper::StaticFieldGetShort: {
3182 return ExpandToRuntime(runtime_support::Get32Static, call_inst);
3183 }
3184 case IntrinsicHelper::StaticFieldGetWide: {
3185 return ExpandToRuntime(runtime_support::Get64Static, call_inst);
3186 }
3187 case IntrinsicHelper::StaticFieldGetObject: {
3188 return ExpandToRuntime(runtime_support::GetObjectStatic, call_inst);
3189 }
3190 case IntrinsicHelper::StaticFieldGetFast: {
3191 return Expand_SGetFast(call_inst.getArgOperand(0),
3192 call_inst.getArgOperand(1),
3193 call_inst.getArgOperand(2),
3194 kInt);
3195 }
3196 case IntrinsicHelper::StaticFieldGetWideFast: {
3197 return Expand_SGetFast(call_inst.getArgOperand(0),
3198 call_inst.getArgOperand(1),
3199 call_inst.getArgOperand(2),
3200 kLong);
3201 }
3202 case IntrinsicHelper::StaticFieldGetObjectFast: {
3203 return Expand_SGetFast(call_inst.getArgOperand(0),
3204 call_inst.getArgOperand(1),
3205 call_inst.getArgOperand(2),
3206 kObject);
3207 }
3208 case IntrinsicHelper::StaticFieldGetBooleanFast: {
3209 return Expand_SGetFast(call_inst.getArgOperand(0),
3210 call_inst.getArgOperand(1),
3211 call_inst.getArgOperand(2),
3212 kBoolean);
3213 }
3214 case IntrinsicHelper::StaticFieldGetByteFast: {
3215 return Expand_SGetFast(call_inst.getArgOperand(0),
3216 call_inst.getArgOperand(1),
3217 call_inst.getArgOperand(2),
3218 kByte);
3219 }
3220 case IntrinsicHelper::StaticFieldGetCharFast: {
3221 return Expand_SGetFast(call_inst.getArgOperand(0),
3222 call_inst.getArgOperand(1),
3223 call_inst.getArgOperand(2),
3224 kChar);
3225 }
3226 case IntrinsicHelper::StaticFieldGetShortFast: {
3227 return Expand_SGetFast(call_inst.getArgOperand(0),
3228 call_inst.getArgOperand(1),
3229 call_inst.getArgOperand(2),
3230 kShort);
3231 }
3232 case IntrinsicHelper::StaticFieldPut:
3233 case IntrinsicHelper::StaticFieldPutBoolean:
3234 case IntrinsicHelper::StaticFieldPutByte:
3235 case IntrinsicHelper::StaticFieldPutChar:
3236 case IntrinsicHelper::StaticFieldPutShort: {
3237 return ExpandToRuntime(runtime_support::Set32Static, call_inst);
3238 }
3239 case IntrinsicHelper::StaticFieldPutWide: {
3240 return ExpandToRuntime(runtime_support::Set64Static, call_inst);
3241 }
3242 case IntrinsicHelper::StaticFieldPutObject: {
3243 return ExpandToRuntime(runtime_support::SetObjectStatic, call_inst);
3244 }
3245 case IntrinsicHelper::StaticFieldPutFast: {
3246 Expand_SPutFast(call_inst.getArgOperand(0),
3247 call_inst.getArgOperand(1),
3248 call_inst.getArgOperand(2),
3249 call_inst.getArgOperand(3),
3250 kInt);
3251 return NULL;
3252 }
3253 case IntrinsicHelper::StaticFieldPutWideFast: {
3254 Expand_SPutFast(call_inst.getArgOperand(0),
3255 call_inst.getArgOperand(1),
3256 call_inst.getArgOperand(2),
3257 call_inst.getArgOperand(3),
3258 kLong);
3259 return NULL;
3260 }
3261 case IntrinsicHelper::StaticFieldPutObjectFast: {
3262 Expand_SPutFast(call_inst.getArgOperand(0),
3263 call_inst.getArgOperand(1),
3264 call_inst.getArgOperand(2),
3265 call_inst.getArgOperand(3),
3266 kObject);
3267 return NULL;
3268 }
3269 case IntrinsicHelper::StaticFieldPutBooleanFast: {
3270 Expand_SPutFast(call_inst.getArgOperand(0),
3271 call_inst.getArgOperand(1),
3272 call_inst.getArgOperand(2),
3273 call_inst.getArgOperand(3),
3274 kBoolean);
3275 return NULL;
3276 }
3277 case IntrinsicHelper::StaticFieldPutByteFast: {
3278 Expand_SPutFast(call_inst.getArgOperand(0),
3279 call_inst.getArgOperand(1),
3280 call_inst.getArgOperand(2),
3281 call_inst.getArgOperand(3),
3282 kByte);
3283 return NULL;
3284 }
3285 case IntrinsicHelper::StaticFieldPutCharFast: {
3286 Expand_SPutFast(call_inst.getArgOperand(0),
3287 call_inst.getArgOperand(1),
3288 call_inst.getArgOperand(2),
3289 call_inst.getArgOperand(3),
3290 kChar);
3291 return NULL;
3292 }
3293 case IntrinsicHelper::StaticFieldPutShortFast: {
3294 Expand_SPutFast(call_inst.getArgOperand(0),
3295 call_inst.getArgOperand(1),
3296 call_inst.getArgOperand(2),
3297 call_inst.getArgOperand(3),
3298 kShort);
3299 return NULL;
3300 }
3301 case IntrinsicHelper::LoadDeclaringClassSSB: {
3302 return Expand_LoadDeclaringClassSSB(call_inst.getArgOperand(0));
3303 }
3304 case IntrinsicHelper::LoadClassSSBFromDexCache: {
3305 return Expand_LoadClassSSBFromDexCache(call_inst.getArgOperand(0));
3306 }
3307 case IntrinsicHelper::InitializeAndLoadClassSSB: {
3308 return ExpandToRuntime(runtime_support::InitializeStaticStorage, call_inst);
3309 }
Logan Chien75e4b602012-07-23 14:24:12 -07003310
3311 //==- High-level Array -------------------------------------------------==//
3312 case IntrinsicHelper::HLArrayGet: {
TDYa1275a26d442012-07-26 18:58:38 -07003313 return Expand_HLArrayGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003314 }
3315 case IntrinsicHelper::HLArrayGetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003316 return Expand_HLArrayGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003317 }
3318 case IntrinsicHelper::HLArrayGetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003319 return Expand_HLArrayGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003320 }
3321 case IntrinsicHelper::HLArrayGetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003322 return Expand_HLArrayGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003323 }
3324 case IntrinsicHelper::HLArrayGetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003325 return Expand_HLArrayGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003326 }
3327 case IntrinsicHelper::HLArrayGetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003328 return Expand_HLArrayGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003329 }
3330 case IntrinsicHelper::HLArrayGetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003331 return Expand_HLArrayGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003332 }
3333 case IntrinsicHelper::HLArrayGetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003334 return Expand_HLArrayGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003335 }
3336 case IntrinsicHelper::HLArrayGetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003337 return Expand_HLArrayGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003338 }
3339 case IntrinsicHelper::HLArrayPut: {
TDYa1275a26d442012-07-26 18:58:38 -07003340 Expand_HLArrayPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003341 return NULL;
3342 }
3343 case IntrinsicHelper::HLArrayPutBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003344 Expand_HLArrayPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003345 return NULL;
3346 }
3347 case IntrinsicHelper::HLArrayPutByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003348 Expand_HLArrayPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003349 return NULL;
3350 }
3351 case IntrinsicHelper::HLArrayPutChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003352 Expand_HLArrayPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003353 return NULL;
3354 }
3355 case IntrinsicHelper::HLArrayPutShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003356 Expand_HLArrayPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003357 return NULL;
3358 }
3359 case IntrinsicHelper::HLArrayPutFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003360 Expand_HLArrayPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003361 return NULL;
3362 }
3363 case IntrinsicHelper::HLArrayPutWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003364 Expand_HLArrayPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003365 return NULL;
3366 }
3367 case IntrinsicHelper::HLArrayPutDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003368 Expand_HLArrayPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003369 return NULL;
3370 }
3371 case IntrinsicHelper::HLArrayPutObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003372 Expand_HLArrayPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003373 return NULL;
3374 }
3375
3376 //==- High-level Instance ----------------------------------------------==//
3377 case IntrinsicHelper::HLIGet: {
TDYa1275e869b62012-07-25 00:45:39 -07003378 return Expand_HLIGet(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003379 }
3380 case IntrinsicHelper::HLIGetBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003381 return Expand_HLIGet(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003382 }
3383 case IntrinsicHelper::HLIGetByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003384 return Expand_HLIGet(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003385 }
3386 case IntrinsicHelper::HLIGetChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003387 return Expand_HLIGet(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003388 }
3389 case IntrinsicHelper::HLIGetShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003390 return Expand_HLIGet(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003391 }
3392 case IntrinsicHelper::HLIGetFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003393 return Expand_HLIGet(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003394 }
3395 case IntrinsicHelper::HLIGetWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003396 return Expand_HLIGet(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003397 }
3398 case IntrinsicHelper::HLIGetDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003399 return Expand_HLIGet(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003400 }
3401 case IntrinsicHelper::HLIGetObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003402 return Expand_HLIGet(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003403 }
3404 case IntrinsicHelper::HLIPut: {
TDYa1275e869b62012-07-25 00:45:39 -07003405 Expand_HLIPut(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003406 return NULL;
3407 }
3408 case IntrinsicHelper::HLIPutBoolean: {
TDYa1275e869b62012-07-25 00:45:39 -07003409 Expand_HLIPut(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003410 return NULL;
3411 }
3412 case IntrinsicHelper::HLIPutByte: {
TDYa1275e869b62012-07-25 00:45:39 -07003413 Expand_HLIPut(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003414 return NULL;
3415 }
3416 case IntrinsicHelper::HLIPutChar: {
TDYa1275e869b62012-07-25 00:45:39 -07003417 Expand_HLIPut(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003418 return NULL;
3419 }
3420 case IntrinsicHelper::HLIPutShort: {
TDYa1275e869b62012-07-25 00:45:39 -07003421 Expand_HLIPut(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003422 return NULL;
3423 }
3424 case IntrinsicHelper::HLIPutFloat: {
TDYa1275e869b62012-07-25 00:45:39 -07003425 Expand_HLIPut(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003426 return NULL;
3427 }
3428 case IntrinsicHelper::HLIPutWide: {
TDYa1275e869b62012-07-25 00:45:39 -07003429 Expand_HLIPut(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003430 return NULL;
3431 }
3432 case IntrinsicHelper::HLIPutDouble: {
TDYa1275e869b62012-07-25 00:45:39 -07003433 Expand_HLIPut(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003434 return NULL;
3435 }
3436 case IntrinsicHelper::HLIPutObject: {
TDYa1275e869b62012-07-25 00:45:39 -07003437 Expand_HLIPut(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003438 return NULL;
3439 }
3440
3441 //==- High-level Invoke ------------------------------------------------==//
TDYa127f71bf5a2012-07-29 20:09:52 -07003442 case IntrinsicHelper::HLInvokeVoid:
3443 case IntrinsicHelper::HLInvokeObj:
3444 case IntrinsicHelper::HLInvokeInt:
3445 case IntrinsicHelper::HLInvokeFloat:
3446 case IntrinsicHelper::HLInvokeLong:
Logan Chien75e4b602012-07-23 14:24:12 -07003447 case IntrinsicHelper::HLInvokeDouble: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003448 return Expand_HLInvoke(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003449 }
3450
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003451 //==- Invoke -----------------------------------------------------------==//
3452 case IntrinsicHelper::FindStaticMethodWithAccessCheck: {
3453 return ExpandToRuntime(runtime_support::FindStaticMethodWithAccessCheck, call_inst);
3454 }
3455 case IntrinsicHelper::FindDirectMethodWithAccessCheck: {
3456 return ExpandToRuntime(runtime_support::FindDirectMethodWithAccessCheck, call_inst);
3457 }
3458 case IntrinsicHelper::FindVirtualMethodWithAccessCheck: {
3459 return ExpandToRuntime(runtime_support::FindVirtualMethodWithAccessCheck, call_inst);
3460 }
3461 case IntrinsicHelper::FindSuperMethodWithAccessCheck: {
3462 return ExpandToRuntime(runtime_support::FindSuperMethodWithAccessCheck, call_inst);
3463 }
3464 case IntrinsicHelper::FindInterfaceMethodWithAccessCheck: {
3465 return ExpandToRuntime(runtime_support::FindInterfaceMethodWithAccessCheck, call_inst);
3466 }
3467 case IntrinsicHelper::GetSDCalleeMethodObjAddrFast: {
3468 return Expand_GetSDCalleeMethodObjAddrFast(call_inst.getArgOperand(0));
3469 }
3470 case IntrinsicHelper::GetVirtualCalleeMethodObjAddrFast: {
3471 return Expand_GetVirtualCalleeMethodObjAddrFast(
3472 call_inst.getArgOperand(0), call_inst.getArgOperand(1));
3473 }
3474 case IntrinsicHelper::GetInterfaceCalleeMethodObjAddrFast: {
3475 return ExpandToRuntime(runtime_support::FindInterfaceMethod, call_inst);
3476 }
3477 case IntrinsicHelper::InvokeRetVoid:
3478 case IntrinsicHelper::InvokeRetBoolean:
3479 case IntrinsicHelper::InvokeRetByte:
3480 case IntrinsicHelper::InvokeRetChar:
3481 case IntrinsicHelper::InvokeRetShort:
3482 case IntrinsicHelper::InvokeRetInt:
3483 case IntrinsicHelper::InvokeRetLong:
3484 case IntrinsicHelper::InvokeRetFloat:
3485 case IntrinsicHelper::InvokeRetDouble:
3486 case IntrinsicHelper::InvokeRetObject: {
3487 return Expand_Invoke(call_inst);
3488 }
Logan Chien75e4b602012-07-23 14:24:12 -07003489
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003490 //==- Math -------------------------------------------------------------==//
3491 case IntrinsicHelper::DivInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003492 return Expand_DivRem(call_inst, /* is_div */true, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003493 }
3494 case IntrinsicHelper::RemInt: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003495 return Expand_DivRem(call_inst, /* is_div */false, kInt);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003496 }
3497 case IntrinsicHelper::DivLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003498 return Expand_DivRem(call_inst, /* is_div */true, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003499 }
3500 case IntrinsicHelper::RemLong: {
TDYa1274ec8ccd2012-08-11 07:04:57 -07003501 return Expand_DivRem(call_inst, /* is_div */false, kLong);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003502 }
3503 case IntrinsicHelper::D2L: {
3504 return ExpandToRuntime(runtime_support::art_d2l, call_inst);
3505 }
3506 case IntrinsicHelper::D2I: {
3507 return ExpandToRuntime(runtime_support::art_d2i, call_inst);
3508 }
3509 case IntrinsicHelper::F2L: {
3510 return ExpandToRuntime(runtime_support::art_f2l, call_inst);
3511 }
3512 case IntrinsicHelper::F2I: {
3513 return ExpandToRuntime(runtime_support::art_f2i, call_inst);
3514 }
Logan Chien75e4b602012-07-23 14:24:12 -07003515
3516 //==- High-level Static ------------------------------------------------==//
3517 case IntrinsicHelper::HLSget: {
TDYa1275a26d442012-07-26 18:58:38 -07003518 return Expand_HLSget(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003519 }
3520 case IntrinsicHelper::HLSgetBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003521 return Expand_HLSget(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003522 }
3523 case IntrinsicHelper::HLSgetByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003524 return Expand_HLSget(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003525 }
3526 case IntrinsicHelper::HLSgetChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003527 return Expand_HLSget(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003528 }
3529 case IntrinsicHelper::HLSgetShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003530 return Expand_HLSget(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003531 }
3532 case IntrinsicHelper::HLSgetFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003533 return Expand_HLSget(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003534 }
3535 case IntrinsicHelper::HLSgetWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003536 return Expand_HLSget(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003537 }
3538 case IntrinsicHelper::HLSgetDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003539 return Expand_HLSget(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003540 }
3541 case IntrinsicHelper::HLSgetObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003542 return Expand_HLSget(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003543 }
3544 case IntrinsicHelper::HLSput: {
TDYa1275a26d442012-07-26 18:58:38 -07003545 Expand_HLSput(call_inst, kInt);
Logan Chien75e4b602012-07-23 14:24:12 -07003546 return NULL;
3547 }
3548 case IntrinsicHelper::HLSputBoolean: {
TDYa1275a26d442012-07-26 18:58:38 -07003549 Expand_HLSput(call_inst, kBoolean);
Logan Chien75e4b602012-07-23 14:24:12 -07003550 return NULL;
3551 }
3552 case IntrinsicHelper::HLSputByte: {
TDYa1275a26d442012-07-26 18:58:38 -07003553 Expand_HLSput(call_inst, kByte);
Logan Chien75e4b602012-07-23 14:24:12 -07003554 return NULL;
3555 }
3556 case IntrinsicHelper::HLSputChar: {
TDYa1275a26d442012-07-26 18:58:38 -07003557 Expand_HLSput(call_inst, kChar);
Logan Chien75e4b602012-07-23 14:24:12 -07003558 return NULL;
3559 }
3560 case IntrinsicHelper::HLSputShort: {
TDYa1275a26d442012-07-26 18:58:38 -07003561 Expand_HLSput(call_inst, kShort);
Logan Chien75e4b602012-07-23 14:24:12 -07003562 return NULL;
3563 }
3564 case IntrinsicHelper::HLSputFloat: {
TDYa1275a26d442012-07-26 18:58:38 -07003565 Expand_HLSput(call_inst, kFloat);
Logan Chien75e4b602012-07-23 14:24:12 -07003566 return NULL;
3567 }
3568 case IntrinsicHelper::HLSputWide: {
TDYa1275a26d442012-07-26 18:58:38 -07003569 Expand_HLSput(call_inst, kLong);
Logan Chien75e4b602012-07-23 14:24:12 -07003570 return NULL;
3571 }
3572 case IntrinsicHelper::HLSputDouble: {
TDYa1275a26d442012-07-26 18:58:38 -07003573 Expand_HLSput(call_inst, kDouble);
Logan Chien75e4b602012-07-23 14:24:12 -07003574 return NULL;
3575 }
3576 case IntrinsicHelper::HLSputObject: {
TDYa1275a26d442012-07-26 18:58:38 -07003577 Expand_HLSput(call_inst, kObject);
Logan Chien75e4b602012-07-23 14:24:12 -07003578 return NULL;
3579 }
3580
3581 //==- High-level Monitor -----------------------------------------------==//
3582 case IntrinsicHelper::MonitorEnter: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003583 Expand_MonitorEnter(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003584 return NULL;
3585 }
3586 case IntrinsicHelper::MonitorExit: {
TDYa127f71bf5a2012-07-29 20:09:52 -07003587 Expand_MonitorExit(call_inst);
Logan Chien75e4b602012-07-23 14:24:12 -07003588 return NULL;
3589 }
3590
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003591 //==- Shadow Frame -----------------------------------------------------==//
3592 case IntrinsicHelper::AllocaShadowFrame: {
TDYa127ce4cc0d2012-11-18 16:59:53 -08003593 Expand_AllocaShadowFrame(call_inst.getArgOperand(0));
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003594 return NULL;
3595 }
TDYa1278e950c12012-11-02 09:58:19 -07003596 case IntrinsicHelper::SetVReg: {
3597 Expand_SetVReg(call_inst.getArgOperand(0),
3598 call_inst.getArgOperand(1));
3599 return NULL;
3600 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003601 case IntrinsicHelper::PopShadowFrame: {
3602 Expand_PopShadowFrame();
3603 return NULL;
3604 }
3605 case IntrinsicHelper::UpdateDexPC: {
3606 Expand_UpdateDexPC(call_inst.getArgOperand(0));
3607 return NULL;
3608 }
TDYa127a1b21852012-07-23 03:20:39 -07003609
Logan Chien75e4b602012-07-23 14:24:12 -07003610 //==- Comparison -------------------------------------------------------==//
3611 case IntrinsicHelper::CmplFloat:
3612 case IntrinsicHelper::CmplDouble: {
3613 return Expand_FPCompare(call_inst.getArgOperand(0),
3614 call_inst.getArgOperand(1),
3615 false);
3616 }
3617 case IntrinsicHelper::CmpgFloat:
3618 case IntrinsicHelper::CmpgDouble: {
3619 return Expand_FPCompare(call_inst.getArgOperand(0),
3620 call_inst.getArgOperand(1),
3621 true);
3622 }
3623 case IntrinsicHelper::CmpLong: {
3624 return Expand_LongCompare(call_inst.getArgOperand(0),
3625 call_inst.getArgOperand(1));
3626 }
TDYa127a1b21852012-07-23 03:20:39 -07003627
Logan Chien75e4b602012-07-23 14:24:12 -07003628 //==- Const ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003629 case IntrinsicHelper::ConstInt:
3630 case IntrinsicHelper::ConstLong: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003631 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003632 }
TDYa127920be7c2012-09-10 17:13:22 -07003633 case IntrinsicHelper::ConstFloat: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003634 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3635 irb_.getJFloatTy());
Logan Chien75e4b602012-07-23 14:24:12 -07003636 }
TDYa127920be7c2012-09-10 17:13:22 -07003637 case IntrinsicHelper::ConstDouble: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003638 return irb_.CreateBitCast(call_inst.getArgOperand(0),
3639 irb_.getJDoubleTy());
3640 }
TDYa127920be7c2012-09-10 17:13:22 -07003641 case IntrinsicHelper::ConstObj: {
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003642 CHECK(LV2UInt(call_inst.getArgOperand(0)) == 0);
3643 return irb_.getJNull();
Logan Chien75e4b602012-07-23 14:24:12 -07003644 }
3645
3646 //==- Method Info ------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003647 case IntrinsicHelper::MethodInfo: {
Shih-wei Liaob2596522012-09-14 16:36:11 -07003648 // Nothing to be done, because MethodInfo carries optional hints that are
3649 // not needed by the portable path.
Logan Chien75e4b602012-07-23 14:24:12 -07003650 return NULL;
3651 }
3652
3653 //==- Copy -------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003654 case IntrinsicHelper::CopyInt:
3655 case IntrinsicHelper::CopyFloat:
3656 case IntrinsicHelper::CopyLong:
3657 case IntrinsicHelper::CopyDouble:
3658 case IntrinsicHelper::CopyObj: {
Logan Chiend54a23d2012-07-24 11:19:23 -07003659 return call_inst.getArgOperand(0);
Logan Chien75e4b602012-07-23 14:24:12 -07003660 }
3661
3662 //==- Shift ------------------------------------------------------------==//
TDYa127920be7c2012-09-10 17:13:22 -07003663 case IntrinsicHelper::SHLLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003664 return Expand_IntegerShift(call_inst.getArgOperand(0),
3665 call_inst.getArgOperand(1),
3666 kIntegerSHL, kLong);
3667 }
TDYa127920be7c2012-09-10 17:13:22 -07003668 case IntrinsicHelper::SHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003669 return Expand_IntegerShift(call_inst.getArgOperand(0),
3670 call_inst.getArgOperand(1),
3671 kIntegerSHR, kLong);
3672 }
TDYa127920be7c2012-09-10 17:13:22 -07003673 case IntrinsicHelper::USHRLong: {
Logan Chien75e4b602012-07-23 14:24:12 -07003674 return Expand_IntegerShift(call_inst.getArgOperand(0),
3675 call_inst.getArgOperand(1),
3676 kIntegerUSHR, kLong);
3677 }
TDYa127920be7c2012-09-10 17:13:22 -07003678 case IntrinsicHelper::SHLInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003679 return Expand_IntegerShift(call_inst.getArgOperand(0),
3680 call_inst.getArgOperand(1),
3681 kIntegerSHL, kInt);
3682 }
TDYa127920be7c2012-09-10 17:13:22 -07003683 case IntrinsicHelper::SHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003684 return Expand_IntegerShift(call_inst.getArgOperand(0),
3685 call_inst.getArgOperand(1),
3686 kIntegerSHR, kInt);
3687 }
TDYa127920be7c2012-09-10 17:13:22 -07003688 case IntrinsicHelper::USHRInt: {
Logan Chien75e4b602012-07-23 14:24:12 -07003689 return Expand_IntegerShift(call_inst.getArgOperand(0),
3690 call_inst.getArgOperand(1),
3691 kIntegerUSHR, kInt);
3692 }
3693
3694 //==- Conversion -------------------------------------------------------==//
TDYa127a1b21852012-07-23 03:20:39 -07003695 case IntrinsicHelper::IntToChar: {
3696 return irb_.CreateZExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJCharTy()),
3697 irb_.getJIntTy());
3698 }
3699 case IntrinsicHelper::IntToShort: {
3700 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJShortTy()),
3701 irb_.getJIntTy());
3702 }
3703 case IntrinsicHelper::IntToByte: {
3704 return irb_.CreateSExt(irb_.CreateTrunc(call_inst.getArgOperand(0), irb_.getJByteTy()),
3705 irb_.getJIntTy());
3706 }
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003707
TDYa12787caa7e2012-08-25 23:23:27 -07003708 //==- Exception --------------------------------------------------------==//
3709 case IntrinsicHelper::CatchTargets: {
TDYa12755e5e6c2012-09-11 15:14:42 -07003710 UpdatePhiInstruction(current_bb_, irb_.GetInsertBlock());
TDYa12787caa7e2012-08-25 23:23:27 -07003711 llvm::SwitchInst* si = llvm::dyn_cast<llvm::SwitchInst>(call_inst.getNextNode());
3712 CHECK(si != NULL);
3713 irb_.CreateBr(si->getDefaultDest());
3714 si->eraseFromParent();
3715 return call_inst.getArgOperand(0);
3716 }
3717
Sebastien Hertz0d43d542013-02-27 19:02:16 +01003718 //==- Constructor barrier-----------------------------------------------==//
3719 case IntrinsicHelper::ConstructorBarrier: {
3720 irb_.CreateMemoryBarrier(art::kStoreStore);
3721 return NULL;
3722 }
3723
Logan Chien75e4b602012-07-23 14:24:12 -07003724 //==- Unknown Cases ----------------------------------------------------==//
3725 case IntrinsicHelper::MaxIntrinsicId:
3726 case IntrinsicHelper::UnknownId:
3727 //default:
3728 // NOTE: "default" is intentionally commented so that C/C++ compiler will
3729 // give some warning on unmatched cases.
3730 // NOTE: We should not implement these cases.
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003731 break;
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003732 }
Logan Chien75e4b602012-07-23 14:24:12 -07003733 UNIMPLEMENTED(FATAL) << "Unexpected GBC intrinsic: " << static_cast<int>(intr_id);
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003734 return NULL;
3735}
3736
3737} // anonymous namespace
3738
3739namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -08003740namespace llvm {
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003741
Ian Rogers4c1c2832013-03-04 18:30:13 -08003742::llvm::FunctionPass*
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003743CreateGBCExpanderPass(const IntrinsicHelper& intrinsic_helper, IRBuilder& irb,
Brian Carlstrom265091e2013-01-30 14:08:26 -08003744 CompilerDriver* driver, const DexCompilationUnit* dex_compilation_unit) {
Ian Rogers89756f22013-03-04 16:40:02 -08003745 return new GBCExpanderPass(intrinsic_helper, irb, driver, dex_compilation_unit);
Shih-wei Liaobb33f2f2012-08-23 13:20:00 -07003746}
3747
Ian Rogers4c1c2832013-03-04 18:30:13 -08003748} // namespace llvm
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003749} // namespace art