blob: 6cf8628d293e96358e07cf3abd72a67a87a47ed8 [file] [log] [blame]
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001//===-- IRForTarget.cpp -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Expression/IRForTarget.h"
11
12#include "llvm/Support/raw_ostream.h"
13#include "llvm/InstrTypes.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000014#include "llvm/Instructions.h"
Sean Callanan3cb1fd82010-09-28 23:55:00 +000015#include "llvm/Intrinsics.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000016#include "llvm/Module.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000017#include "llvm/Target/TargetData.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000018#include "llvm/ValueSymbolTable.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000019
20#include "clang/AST/ASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000021
22#include "lldb/Core/dwarf.h"
23#include "lldb/Core/Log.h"
24#include "lldb/Core/Scalar.h"
25#include "lldb/Core/StreamString.h"
26#include "lldb/Expression/ClangExpressionDeclMap.h"
27
28#include <map>
29
30using namespace llvm;
31
Sean Callanan3351dac2010-08-18 18:50:51 +000032static char ID;
33
34IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
Sean Callanane8a59a82010-09-13 21:34:21 +000035 bool resolve_vars,
Sean Callanan65dafa82010-08-27 01:01:44 +000036 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000037 ModulePass(ID),
Sean Callanan8bce6652010-07-13 21:41:46 +000038 m_decl_map(decl_map),
Sean Callanan65dafa82010-08-27 01:01:44 +000039 m_sel_registerName(NULL),
Sean Callanane8a59a82010-09-13 21:34:21 +000040 m_func_name(func_name),
41 m_resolve_vars(resolve_vars)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000042{
43}
44
Sean Callanan771131d2010-09-30 21:18:25 +000045/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000046
47static std::string
Sean Callanan771131d2010-09-30 21:18:25 +000048PrintValue(const Value *V, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000049{
50 std::string s;
51 raw_string_ostream rso(s);
52 V->print(rso);
53 rso.flush();
54 if (truncate)
55 s.resize(s.length() - 1);
56 return s;
57}
58
Sean Callanan771131d2010-09-30 21:18:25 +000059static std::string
60PrintType(const Type *T, bool truncate = false)
61{
62 std::string s;
63 raw_string_ostream rso(s);
64 T->print(rso);
65 rso.flush();
66 if (truncate)
67 s.resize(s.length() - 1);
68 return s;
69}
70
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000071IRForTarget::~IRForTarget()
72{
73}
74
Sean Callanan82b74c82010-08-12 01:56:52 +000075bool
76IRForTarget::createResultVariable(llvm::Module &M,
77 llvm::Function &F)
78{
79 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
80
Sean Callanane8a59a82010-09-13 21:34:21 +000081 if (!m_resolve_vars)
82 return true;
83
84 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000085
86 ValueSymbolTable& value_symbol_table = M.getValueSymbolTable();
87
88 const char *result_name = NULL;
89
90 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
91 vi != ve;
92 ++vi)
93 {
Sean Callananc04743d2010-09-28 21:13:03 +000094 if (strstr(vi->first(), "___clang_expr_result") &&
95 !strstr(vi->first(), "GV"))
96 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000097 result_name = vi->first();
Sean Callananc04743d2010-09-28 21:13:03 +000098 break;
99 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000100 }
101
102 if (!result_name)
103 {
104 if (log)
105 log->PutCString("Couldn't find result variable");
106
107 return true;
108 }
109
Sean Callananc04743d2010-09-28 21:13:03 +0000110 if (log)
111 log->Printf("Result name: %s", result_name);
112
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000113 Value *result_value = M.getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000114
115 if (!result_value)
116 {
117 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000118 log->PutCString("Result variable had no data");
Sean Callanane8a59a82010-09-13 21:34:21 +0000119
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000120 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000121 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000122
Sean Callanan82b74c82010-08-12 01:56:52 +0000123 if (log)
124 log->Printf("Found result in the IR: %s", PrintValue(result_value, false).c_str());
125
126 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
127
128 if (!result_global)
129 {
130 if (log)
131 log->PutCString("Result variable isn't a GlobalVariable");
132 return false;
133 }
134
135 // Find the metadata and follow it to the VarDecl
136
137 NamedMDNode *named_metadata = M.getNamedMetadata("clang.global.decl.ptrs");
138
139 if (!named_metadata)
140 {
141 if (log)
142 log->PutCString("No global metadata");
143
144 return false;
145 }
146
147 unsigned num_nodes = named_metadata->getNumOperands();
148 unsigned node_index;
149
150 MDNode *metadata_node = NULL;
151
152 for (node_index = 0;
153 node_index < num_nodes;
154 ++node_index)
155 {
156 metadata_node = named_metadata->getOperand(node_index);
157
158 if (metadata_node->getNumOperands() != 2)
159 continue;
160
161 if (metadata_node->getOperand(0) == result_global)
162 break;
163 }
164
165 if (!metadata_node)
166 {
167 if (log)
168 log->PutCString("Couldn't find result metadata");
169 return false;
170 }
171
172 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
173
174 uint64_t result_decl_intptr = constant_int->getZExtValue();
175
176 clang::VarDecl *result_decl = reinterpret_cast<clang::VarDecl *>(result_decl_intptr);
177
178 // Get the next available result name from m_decl_map and create the persistent
179 // variable for it
180
181 lldb_private::TypeFromParser result_decl_type (result_decl->getType().getAsOpaquePtr(),
182 &result_decl->getASTContext());
183 std::string new_result_name;
184
185 m_decl_map->GetPersistentResultName(new_result_name);
Sean Callanan8c127202010-08-23 23:09:38 +0000186 m_decl_map->AddPersistentVariable(result_decl, new_result_name.c_str(), result_decl_type);
Sean Callanan82b74c82010-08-12 01:56:52 +0000187
188 if (log)
189 log->Printf("Creating a new result global: %s", new_result_name.c_str());
190
191 // Construct a new result global and set up its metadata
192
193 GlobalVariable *new_result_global = new GlobalVariable(M,
194 result_global->getType()->getElementType(),
195 false, /* not constant */
196 GlobalValue::ExternalLinkage,
197 NULL, /* no initializer */
198 new_result_name.c_str());
199
200 // It's too late in compilation to create a new VarDecl for this, but we don't
201 // need to. We point the metadata at the old VarDecl. This creates an odd
202 // anomaly: a variable with a Value whose name is something like $0 and a
203 // Decl whose name is ___clang_expr_result. This condition is handled in
204 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
205 // fixed up.
206
207 ConstantInt *new_constant_int = ConstantInt::get(constant_int->getType(),
208 result_decl_intptr,
209 false);
210
211 llvm::Value* values[2];
212 values[0] = new_result_global;
213 values[1] = new_constant_int;
214
215 MDNode *persistent_global_md = MDNode::get(M.getContext(), values, 2);
216 named_metadata->addOperand(persistent_global_md);
217
218 if (log)
Sean Callanan2e2db532010-09-07 22:43:19 +0000219 log->Printf("Replacing %s with %s",
220 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000221 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000222
223 if (result_global->hasNUses(0))
224 {
225 // We need to synthesize a store for this variable, because otherwise
226 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000227
Sean Callanan2e2db532010-09-07 22:43:19 +0000228 BasicBlock &entry_block(F.getEntryBlock());
229 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
230
231 if (!first_entry_instruction)
232 return false;
233
234 if (!result_global->hasInitializer())
235 {
236 if (log)
237 log->Printf("Couldn't find initializer for unused variable");
238 return false;
239 }
240
241 Constant *initializer = result_global->getInitializer();
242
243 StoreInst *synthesized_store = new StoreInst::StoreInst(initializer,
244 new_result_global,
245 first_entry_instruction);
246
247 if (log)
248 log->Printf("Synthesized result store %s\n", PrintValue(synthesized_store).c_str());
249 }
250 else
251 {
252 result_global->replaceAllUsesWith(new_result_global);
253 }
254
Sean Callanan82b74c82010-08-12 01:56:52 +0000255 result_global->eraseFromParent();
256
257 return true;
258}
259
Sean Callananf5857a02010-07-31 01:32:05 +0000260static bool isObjCSelectorRef(Value *V)
261{
262 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
263
264 if (!GV || !GV->hasName() || !GV->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
265 return false;
266
267 return true;
268}
269
270bool
271IRForTarget::RewriteObjCSelector(Instruction* selector_load,
272 Module &M)
273{
274 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
275
276 LoadInst *load = dyn_cast<LoadInst>(selector_load);
277
278 if (!load)
279 return false;
280
281 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
282 //
283 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
284 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
285 //
286 // where %obj is the object pointer and %tmp is the selector.
287 //
288 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_METH_VAR_NAME_".
289 // @"\01L_OBJC_METH_VAR_NAME_" contains the string.
290
291 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
292
293 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
294
295 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
296 return false;
297
298 Constant *osr_initializer = _objc_selector_references_->getInitializer();
299
300 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
301
302 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
303 return false;
304
305 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
306
307 if (!osr_initializer_base)
308 return false;
309
310 // Find the string's initializer (a ConstantArray) and get the string from it
311
312 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
313
314 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
315 return false;
316
317 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
318
319 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
320
321 if (!omvn_initializer_array->isString())
322 return false;
323
324 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
325
326 if (log)
327 log->Printf("Found Objective-C selector reference %s", omvn_initializer_string.c_str());
328
329 // Construct a call to sel_registerName
330
331 if (!m_sel_registerName)
332 {
333 uint64_t srN_addr;
334
335 if (!m_decl_map->GetFunctionAddress("sel_registerName", srN_addr))
336 return false;
337
338 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
339
340 // The below code would be "more correct," but in actuality what's required is uint8_t*
341 //Type *sel_type = StructType::get(M.getContext());
342 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
343 const Type *sel_ptr_type = Type::getInt8PtrTy(M.getContext());
344
345 std::vector <const Type *> srN_arg_types;
346 srN_arg_types.push_back(Type::getInt8PtrTy(M.getContext()));
347 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
348
349 // Build the constant containing the pointer to the function
350 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
351 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
352 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
353 Constant *srN_addr_int = ConstantInt::get(intptr_ty, srN_addr, false);
354 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
355 }
356
357 SmallVector <Value*, 1> srN_arguments;
358
359 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(M.getContext()));
360
361 srN_arguments.push_back(omvn_pointer);
362
363 CallInst *srN_call = CallInst::Create(m_sel_registerName,
364 srN_arguments.begin(),
365 srN_arguments.end(),
366 "srN",
367 selector_load);
368
369 // Replace the load with the call in all users
370
371 selector_load->replaceAllUsesWith(srN_call);
372
373 selector_load->eraseFromParent();
374
375 return true;
376}
377
378bool
379IRForTarget::rewriteObjCSelectors(Module &M,
380 BasicBlock &BB)
381{
382 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
383
384 BasicBlock::iterator ii;
385
386 typedef SmallVector <Instruction*, 2> InstrList;
387 typedef InstrList::iterator InstrIterator;
388
389 InstrList selector_loads;
390
391 for (ii = BB.begin();
392 ii != BB.end();
393 ++ii)
394 {
395 Instruction &inst = *ii;
396
397 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
398 if (isObjCSelectorRef(load->getPointerOperand()))
399 selector_loads.push_back(&inst);
400 }
401
402 InstrIterator iter;
403
404 for (iter = selector_loads.begin();
405 iter != selector_loads.end();
406 ++iter)
407 {
408 if (!RewriteObjCSelector(*iter, M))
409 {
410 if(log)
411 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
412 return false;
413 }
414 }
415
416 return true;
417}
418
Sean Callanana48fe162010-08-11 03:57:18 +0000419bool
420IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc,
421 llvm::Module &M)
422{
423 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
424
425 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
426
427 if (!alloc_md || !alloc_md->getNumOperands())
428 return false;
429
430 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
431
432 if (!constant_int)
433 return false;
434
435 // We attempt to register this as a new persistent variable with the DeclMap.
436
437 uintptr_t ptr = constant_int->getZExtValue();
438
Sean Callanan82b74c82010-08-12 01:56:52 +0000439 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +0000440
Sean Callanan82b74c82010-08-12 01:56:52 +0000441 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
442 &decl->getASTContext());
443
Sean Callanan8c127202010-08-23 23:09:38 +0000444 if (!m_decl_map->AddPersistentVariable(decl, decl->getName().str().c_str(), result_decl_type))
Sean Callanana48fe162010-08-11 03:57:18 +0000445 return false;
446
447 GlobalVariable *persistent_global = new GlobalVariable(M,
448 alloc->getType()->getElementType(),
449 false, /* not constant */
450 GlobalValue::ExternalLinkage,
451 NULL, /* no initializer */
452 alloc->getName().str().c_str());
453
454 // What we're going to do here is make believe this was a regular old external
455 // variable. That means we need to make the metadata valid.
456
457 NamedMDNode *named_metadata = M.getNamedMetadata("clang.global.decl.ptrs");
458
459 llvm::Value* values[2];
460 values[0] = persistent_global;
461 values[1] = constant_int;
462
463 MDNode *persistent_global_md = MDNode::get(M.getContext(), values, 2);
464 named_metadata->addOperand(persistent_global_md);
465
466 alloc->replaceAllUsesWith(persistent_global);
467 alloc->eraseFromParent();
468
469 return true;
470}
471
472bool
473IRForTarget::rewritePersistentAllocs(llvm::Module &M,
474 llvm::BasicBlock &BB)
475{
Sean Callanane8a59a82010-09-13 21:34:21 +0000476 if (!m_resolve_vars)
477 return true;
478
Sean Callanana48fe162010-08-11 03:57:18 +0000479 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
480
481 BasicBlock::iterator ii;
482
483 typedef SmallVector <Instruction*, 2> InstrList;
484 typedef InstrList::iterator InstrIterator;
485
486 InstrList pvar_allocs;
487
488 for (ii = BB.begin();
489 ii != BB.end();
490 ++ii)
491 {
492 Instruction &inst = *ii;
493
494 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
495 if (alloc->getName().startswith("$"))
496 pvar_allocs.push_back(alloc);
497 }
498
499 InstrIterator iter;
500
501 for (iter = pvar_allocs.begin();
502 iter != pvar_allocs.end();
503 ++iter)
504 {
505 if (!RewritePersistentAlloc(*iter, M))
506 {
507 if(log)
508 log->PutCString("Couldn't rewrite the creation of a persistent variable");
509 return false;
510 }
511 }
512
513 return true;
514}
515
Sean Callanan8bce6652010-07-13 21:41:46 +0000516static clang::NamedDecl *
Sean Callanan02fbafa2010-07-27 21:39:39 +0000517DeclForGlobalValue(Module &module,
518 GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000519{
520 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
521
522 if (!named_metadata)
523 return NULL;
524
525 unsigned num_nodes = named_metadata->getNumOperands();
526 unsigned node_index;
527
528 for (node_index = 0;
529 node_index < num_nodes;
530 ++node_index)
531 {
532 MDNode *metadata_node = named_metadata->getOperand(node_index);
533
534 if (!metadata_node)
535 return NULL;
536
537 if (metadata_node->getNumOperands() != 2)
Sean Callanana48fe162010-08-11 03:57:18 +0000538 continue;
Sean Callanan8bce6652010-07-13 21:41:46 +0000539
540 if (metadata_node->getOperand(0) != global_value)
541 continue;
542
543 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
544
545 if (!constant_int)
546 return NULL;
547
548 uintptr_t ptr = constant_int->getZExtValue();
549
550 return reinterpret_cast<clang::NamedDecl *>(ptr);
551 }
552
553 return NULL;
554}
555
556bool
557IRForTarget::MaybeHandleVariable(Module &M,
Sean Callanan02fbafa2010-07-27 21:39:39 +0000558 Value *V,
Sean Callanan8bce6652010-07-13 21:41:46 +0000559 bool Store)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000560{
Sean Callananf5857a02010-07-31 01:32:05 +0000561 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
562
Sean Callananbc2928a2010-08-03 00:23:29 +0000563 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(V))
564 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000565 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +0000566 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000567 default:
568 break;
569 case Instruction::GetElementPtr:
570 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +0000571 Value *s = constant_expr->getOperand(0);
572 MaybeHandleVariable(M, s, Store);
573 }
574 }
Sean Callanan8bce6652010-07-13 21:41:46 +0000575 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(V))
Sean Callananf5857a02010-07-31 01:32:05 +0000576 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000577 clang::NamedDecl *named_decl = DeclForGlobalValue(M, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000578
Sean Callananf5857a02010-07-31 01:32:05 +0000579 if (!named_decl)
580 {
581 if (isObjCSelectorRef(V))
582 return true;
583
584 if (log)
585 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
586 return false;
587 }
588
Sean Callanan810f22d2010-07-16 00:09:46 +0000589 std::string name = named_decl->getName().str();
590
Sean Callanan771131d2010-09-30 21:18:25 +0000591 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000592 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000593
594 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000595 {
Sean Callanan771131d2010-09-30 21:18:25 +0000596 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000597 ast_context = &value_decl->getASTContext();
598 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000599 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000600 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000601 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000602 }
Sean Callanan771131d2010-09-30 21:18:25 +0000603
604 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
Sean Callananf328c9f2010-07-20 23:31:16 +0000605
Sean Callanan02fbafa2010-07-27 21:39:39 +0000606 const Type *value_type = global_variable->getType();
Sean Callanan771131d2010-09-30 21:18:25 +0000607
608 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
609 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
610
611 if (log)
612 log->Printf("Type of %s is [clang %s, lldb %s] [size %d, align %d]",
613 name.c_str(),
614 qual_type.getAsString().c_str(),
615 PrintType(value_type).c_str(),
616 value_size,
617 value_alignment);
618
Sean Callanan8bce6652010-07-13 21:41:46 +0000619
Sean Callanan8c127202010-08-23 23:09:38 +0000620 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Sean Callanan45690fe2010-08-30 22:17:16 +0000621 name.c_str(),
Sean Callanan8c127202010-08-23 23:09:38 +0000622 V,
Sean Callananba992c52010-07-27 02:07:53 +0000623 value_size,
624 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000625 return false;
626 }
627
628 return true;
629}
630
631bool
Sean Callanandc27aba2010-10-05 22:26:43 +0000632IRForTarget::MaybeHandleCallArguments(Module &M,
633 CallInst *C)
634{
635 // lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
636
637 for (unsigned op_index = 0, num_ops = C->getNumArgOperands();
638 op_index < num_ops;
639 ++op_index)
640 if (!MaybeHandleVariable(M, C->getArgOperand(op_index), true)) // conservatively believe that this is a store
641 return false;
642
643 return true;
644}
645
646bool
Sean Callananba992c52010-07-27 02:07:53 +0000647IRForTarget::MaybeHandleCall(Module &M,
648 CallInst *C)
649{
650 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
651
Sean Callanan02fbafa2010-07-27 21:39:39 +0000652 Function *fun = C->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000653
654 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +0000655 {
656 Value *val = C->getCalledValue();
657
658 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
659
660 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
661 {
662 fun = dyn_cast<Function>(const_expr->getOperand(0));
663
664 if (!fun)
665 return true;
666 }
667 else
668 {
669 return true;
670 }
671 }
Sean Callananba992c52010-07-27 02:07:53 +0000672
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000673 std::string str;
Sean Callananc04743d2010-09-28 21:13:03 +0000674
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000675 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +0000676 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000677 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +0000678
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000679 switch (intrinsic_id)
680 {
681 default:
682 if (log)
683 log->Printf("Unresolved intrinsic %s", Intrinsic::getName(intrinsic_id).c_str());
684 return false;
685 case Intrinsic::memcpy:
686 str = "memcpy";
687 break;
688 }
Sean Callananc04743d2010-09-28 21:13:03 +0000689
690 if (log)
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000691 log->Printf("Resolved intrinsic name %s", str.c_str());
692 }
693 else
694 {
695 str = fun->getName().str();
Sean Callananc04743d2010-09-28 21:13:03 +0000696 }
697
Sean Callananba992c52010-07-27 02:07:53 +0000698 clang::NamedDecl *fun_decl = DeclForGlobalValue(M, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000699 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000700 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000701
Sean Callananf5857a02010-07-31 01:32:05 +0000702 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000703 {
Sean Callananf5857a02010-07-31 01:32:05 +0000704 if (!m_decl_map->GetFunctionInfo(fun_decl, fun_value_ptr, fun_addr))
705 {
Sean Callanan92aa6662010-09-07 21:49:41 +0000706 fun_value_ptr = NULL;
707
Sean Callananc04743d2010-09-28 21:13:03 +0000708 if (!m_decl_map->GetFunctionAddress(str.c_str(), fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +0000709 {
710 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000711 log->Printf("Function %s had no address", str.c_str());
Sean Callanan92aa6662010-09-07 21:49:41 +0000712
713 return false;
714 }
Sean Callananf5857a02010-07-31 01:32:05 +0000715 }
716 }
717 else
718 {
Sean Callananc04743d2010-09-28 21:13:03 +0000719 if (!m_decl_map->GetFunctionAddress(str.c_str(), fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000720 {
721 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000722 log->Printf("Metadataless function %s had no address", str.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000723 }
Sean Callananba992c52010-07-27 02:07:53 +0000724 }
725
726 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000727 log->Printf("Found %s at %llx", str.c_str(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000728
Sean Callananf5857a02010-07-31 01:32:05 +0000729 Value *fun_addr_ptr;
730
731 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000732 {
Sean Callanan02fbafa2010-07-27 21:39:39 +0000733 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
734 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +0000735 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +0000736 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
737 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000738 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
739
740 if (fun_value_ptr)
741 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000742 }
Sean Callananf5857a02010-07-31 01:32:05 +0000743
744 if (fun_value_ptr)
745 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000746
Sean Callananf5857a02010-07-31 01:32:05 +0000747 C->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000748
Sean Callananc04743d2010-09-28 21:13:03 +0000749 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(M.getContext(), str);
Sean Callanane8a59a82010-09-13 21:34:21 +0000750
751 Value *values[1];
752 values[0] = func_name;
753 MDNode *func_metadata = MDNode::get(M.getContext(), values, 1);
754
755 C->setMetadata("lldb.call.realName", func_metadata);
756
757 if (log)
758 log->Printf("Set metadata for %p [%d, %s]", C, func_name->isString(), func_name->getAsString().c_str());
759
Sean Callananba992c52010-07-27 02:07:53 +0000760 return true;
761}
762
763bool
Sean Callananf5857a02010-07-31 01:32:05 +0000764IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000765{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000766 /////////////////////////////////////////////////////////////////////////
767 // Prepare the current basic block for execution in the remote process
768 //
769
Sean Callanan02fbafa2010-07-27 21:39:39 +0000770 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000771
772 for (ii = BB.begin();
773 ii != BB.end();
774 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000775 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000776 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000777
Sean Callanane8a59a82010-09-13 21:34:21 +0000778 if (m_resolve_vars)
779 {
780 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
781 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
782 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000783
Sean Callanane8a59a82010-09-13 21:34:21 +0000784 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
785 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
786 return false;
787 }
Sean Callananba992c52010-07-27 02:07:53 +0000788
789 if (CallInst *call = dyn_cast<CallInst>(&inst))
Sean Callanandc27aba2010-10-05 22:26:43 +0000790 {
791 if (!MaybeHandleCallArguments(M, call))
792 return false;
793
Sean Callananba992c52010-07-27 02:07:53 +0000794 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000795 return false;
Sean Callanandc27aba2010-10-05 22:26:43 +0000796 }
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000797 }
798
799 return true;
800}
801
Sean Callanan02fbafa2010-07-27 21:39:39 +0000802static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000803{
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000804 Constant *C;
Sean Callanan45839272010-07-24 01:37:44 +0000805
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000806 if (!(C = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +0000807 return false;
808
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000809 ConstantExpr *CE;
810
811 if ((CE = dyn_cast<ConstantExpr>(V)))
812 {
813 if (CE->getOpcode() != Instruction::BitCast)
814 return false;
815
816 C = CE->getOperand(0);
817 }
818
819 GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
Sean Callanan45839272010-07-24 01:37:44 +0000820
821 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
822 return false;
823
824 return true;
825}
826
827static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
828{
829 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
830
831 Value::use_iterator ui;
832
833 for (ui = guard_load->use_begin();
834 ui != guard_load->use_end();
835 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000836 {
Greg Clayton6e713402010-07-30 20:30:44 +0000837 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000838 {
839 // do nothing for the moment
840 }
841 else
842 {
843 ui->replaceUsesOfWith(guard_load, zero);
844 }
845 }
Sean Callanan45839272010-07-24 01:37:44 +0000846
847 guard_load->eraseFromParent();
848}
849
850static void ExciseGuardStore(Instruction* guard_store)
851{
852 guard_store->eraseFromParent();
853}
854
855bool
856IRForTarget::removeGuards(Module &M, BasicBlock &BB)
857{
858 ///////////////////////////////////////////////////////
859 // Eliminate any reference to guard variables found.
860 //
861
Sean Callanan02fbafa2010-07-27 21:39:39 +0000862 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000863
Sean Callanan02fbafa2010-07-27 21:39:39 +0000864 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000865 typedef InstrList::iterator InstrIterator;
866
867 InstrList guard_loads;
868 InstrList guard_stores;
869
870 for (ii = BB.begin();
871 ii != BB.end();
872 ++ii)
873 {
874 Instruction &inst = *ii;
875
876 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
877 if (isGuardVariableRef(load->getPointerOperand()))
878 guard_loads.push_back(&inst);
879
880 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
881 if (isGuardVariableRef(store->getPointerOperand()))
882 guard_stores.push_back(&inst);
883 }
884
885 InstrIterator iter;
886
887 for (iter = guard_loads.begin();
888 iter != guard_loads.end();
889 ++iter)
890 TurnGuardLoadIntoZero(*iter, M);
891
892 for (iter = guard_stores.begin();
893 iter != guard_stores.end();
894 ++iter)
895 ExciseGuardStore(*iter);
896
897 return true;
898}
899
Sean Callananbafd6852010-07-14 23:40:29 +0000900// UnfoldConstant operates on a constant [C] which has just been replaced with a value
901// [new_value]. We assume that new_value has been properly placed early in the function,
902// most likely somewhere in front of the first instruction in the entry basic block
903// [first_entry_instruction].
904//
905// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
906// Where those uses are constants, the function generates new instructions to compute the
907// result of the new, non-constant expression and places them before first_entry_instruction.
908// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
909// for those.
910
911static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000912UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000913{
914 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
915
916 Value::use_iterator ui;
917
Sean Callanana48fe162010-08-11 03:57:18 +0000918 SmallVector<User*, 16> users;
919
920 // We do this because the use list might change, invalidating our iterator.
921 // Much better to keep a work list ourselves.
Sean Callananbafd6852010-07-14 23:40:29 +0000922 for (ui = C->use_begin();
923 ui != C->use_end();
924 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +0000925 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +0000926
Sean Callanana48fe162010-08-11 03:57:18 +0000927 for (int i = 0;
928 i < users.size();
929 ++i)
930 {
931 User *user = users[i];
932
Sean Callananbafd6852010-07-14 23:40:29 +0000933 if (Constant *constant = dyn_cast<Constant>(user))
934 {
935 // synthesize a new non-constant equivalent of the constant
936
937 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
938 {
939 switch (constant_expr->getOpcode())
940 {
941 default:
942 if (log)
943 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
944 return false;
945 case Instruction::BitCast:
946 {
947 // UnaryExpr
948 // OperandList[0] is value
949
950 Value *s = constant_expr->getOperand(0);
951
952 if (s == C)
953 s = new_value;
954
955 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
956
957 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
958 }
959 break;
960 case Instruction::GetElementPtr:
961 {
962 // GetElementPtrConstantExpr
963 // OperandList[0] is base
964 // OperandList[1]... are indices
965
966 Value *ptr = constant_expr->getOperand(0);
967
968 if (ptr == C)
969 ptr = new_value;
970
971 SmallVector<Value*, 16> indices;
972
973 unsigned operand_index;
974 unsigned num_operands = constant_expr->getNumOperands();
975
976 for (operand_index = 1;
977 operand_index < num_operands;
978 ++operand_index)
979 {
980 Value *operand = constant_expr->getOperand(operand_index);
981
982 if (operand == C)
983 operand = new_value;
984
985 indices.push_back(operand);
986 }
987
988 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
989
990 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
991 }
992 break;
993 }
994 }
995 else
996 {
997 if (log)
998 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
999 return false;
1000 }
1001 }
1002 else
1003 {
1004 // simple fall-through case for non-constants
1005 user->replaceUsesOfWith(C, new_value);
1006 }
1007 }
1008
1009 return true;
1010}
1011
Sean Callanan8bce6652010-07-13 21:41:46 +00001012bool
Sean Callananf5857a02010-07-31 01:32:05 +00001013IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +00001014{
Sean Callanane8a59a82010-09-13 21:34:21 +00001015 if (!m_resolve_vars)
1016 return true;
1017
Sean Callanan8bce6652010-07-13 21:41:46 +00001018 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1019
1020 m_decl_map->DoStructLayout();
1021
1022 if (log)
1023 log->Printf("Element arrangement:");
1024
1025 uint32_t num_elements;
1026 uint32_t element_index;
1027
1028 size_t size;
1029 off_t alignment;
1030
1031 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1032 return false;
1033
Sean Callananf5857a02010-07-31 01:32:05 +00001034 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001035
Sean Callananf5857a02010-07-31 01:32:05 +00001036 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001037 return false;
1038
Sean Callanan02fbafa2010-07-27 21:39:39 +00001039 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001040
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001041 if (argument->getName().equals("this"))
1042 {
1043 ++iter;
1044
1045 if (iter == F.getArgumentList().end())
1046 return false;
1047
1048 argument = iter;
1049 }
1050
Sean Callanan8bce6652010-07-13 21:41:46 +00001051 if (!argument->getName().equals("___clang_arg"))
1052 return false;
1053
1054 if (log)
1055 log->Printf("Arg: %s", PrintValue(argument).c_str());
1056
Sean Callananf5857a02010-07-31 01:32:05 +00001057 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +00001058 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001059
1060 if (!first_entry_instruction)
1061 return false;
1062
1063 LLVMContext &context(M.getContext());
1064 const IntegerType *offset_type(Type::getInt32Ty(context));
1065
1066 if (!offset_type)
1067 return false;
1068
1069 for (element_index = 0; element_index < num_elements; ++element_index)
1070 {
1071 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001072 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001073 off_t offset;
Sean Callanan45690fe2010-08-30 22:17:16 +00001074 const char *name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001075
Sean Callanan45690fe2010-08-30 22:17:16 +00001076 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001077 return false;
1078
1079 if (log)
Sean Callanan45690fe2010-08-30 22:17:16 +00001080 log->Printf(" %s [%s] (%s) placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001081 value->getName().str().c_str(),
Sean Callanan45690fe2010-08-30 22:17:16 +00001082 name,
Sean Callanan8bce6652010-07-13 21:41:46 +00001083 PrintValue(value, true).c_str(),
1084 offset);
1085
1086 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
1087 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
1088 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
1089
Sean Callananbafd6852010-07-14 23:40:29 +00001090 if (Constant *constant = dyn_cast<Constant>(value))
1091 UnfoldConstant(constant, bit_cast, first_entry_instruction);
1092 else
1093 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +00001094 }
1095
1096 if (log)
1097 log->Printf("Total structure [align %d, size %d]", alignment, size);
1098
1099 return true;
1100}
1101
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001102bool
1103IRForTarget::runOnModule(Module &M)
1104{
1105 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1106
Sean Callanan65dafa82010-08-27 01:01:44 +00001107 Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001108
1109 if (!function)
1110 {
1111 if (log)
Sean Callanan65dafa82010-08-27 01:01:44 +00001112 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001113
1114 return false;
1115 }
1116
Sean Callanan02fbafa2010-07-27 21:39:39 +00001117 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001118
Sean Callanan82b74c82010-08-12 01:56:52 +00001119 ////////////////////////////////////////////////////////////
1120 // Replace __clang_expr_result with a persistent variable
1121 //
1122
1123 if (!createResultVariable(M, *function))
1124 return false;
1125
Sean Callananf5857a02010-07-31 01:32:05 +00001126 //////////////////////////////////
1127 // Run basic-block level passes
1128 //
1129
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001130 for (bbi = function->begin();
1131 bbi != function->end();
1132 ++bbi)
1133 {
Sean Callanan8c127202010-08-23 23:09:38 +00001134 if (!removeGuards(M, *bbi))
1135 return false;
1136
Sean Callanana48fe162010-08-11 03:57:18 +00001137 if (!rewritePersistentAllocs(M, *bbi))
Sean Callananf5857a02010-07-31 01:32:05 +00001138 return false;
1139
Sean Callanana48fe162010-08-11 03:57:18 +00001140 if (!rewriteObjCSelectors(M, *bbi))
1141 return false;
1142
Sean Callananf5857a02010-07-31 01:32:05 +00001143 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +00001144 return false;
1145 }
1146
Sean Callanan771131d2010-09-30 21:18:25 +00001147 ///////////////////////////////
1148 // Run function-level passes
1149 //
1150
1151 if (!replaceVariables(M, *function))
1152 return false;
1153
Sean Callanan8bce6652010-07-13 21:41:46 +00001154 if (log)
1155 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001156 std::string s;
1157 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001158
Sean Callanan321fe9e2010-07-28 01:00:59 +00001159 M.print(oss, NULL);
1160
1161 oss.flush();
1162
1163 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001164 }
1165
1166 return true;
1167}
1168
1169void
1170IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +00001171 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001172{
1173}
1174
1175PassManagerType
1176IRForTarget::getPotentialPassManagerType() const
1177{
1178 return PMT_ModulePassManager;
1179}