blob: 662c086373b494292beb433e909f7fce44240571 [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 Callananba992c52010-07-27 02:07:53 +0000632IRForTarget::MaybeHandleCall(Module &M,
633 CallInst *C)
634{
635 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
636
Sean Callanan02fbafa2010-07-27 21:39:39 +0000637 Function *fun = C->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000638
639 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +0000640 {
641 Value *val = C->getCalledValue();
642
643 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
644
645 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
646 {
647 fun = dyn_cast<Function>(const_expr->getOperand(0));
648
649 if (!fun)
650 return true;
651 }
652 else
653 {
654 return true;
655 }
656 }
Sean Callananba992c52010-07-27 02:07:53 +0000657
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000658 std::string str;
Sean Callananc04743d2010-09-28 21:13:03 +0000659
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000660 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +0000661 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000662 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +0000663
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000664 switch (intrinsic_id)
665 {
666 default:
667 if (log)
668 log->Printf("Unresolved intrinsic %s", Intrinsic::getName(intrinsic_id).c_str());
669 return false;
670 case Intrinsic::memcpy:
671 str = "memcpy";
672 break;
673 }
Sean Callananc04743d2010-09-28 21:13:03 +0000674
675 if (log)
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000676 log->Printf("Resolved intrinsic name %s", str.c_str());
677 }
678 else
679 {
680 str = fun->getName().str();
Sean Callananc04743d2010-09-28 21:13:03 +0000681 }
682
Sean Callananba992c52010-07-27 02:07:53 +0000683 clang::NamedDecl *fun_decl = DeclForGlobalValue(M, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000684 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000685 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000686
Sean Callananf5857a02010-07-31 01:32:05 +0000687 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000688 {
Sean Callananf5857a02010-07-31 01:32:05 +0000689 if (!m_decl_map->GetFunctionInfo(fun_decl, fun_value_ptr, fun_addr))
690 {
Sean Callanan92aa6662010-09-07 21:49:41 +0000691 fun_value_ptr = NULL;
692
Sean Callananc04743d2010-09-28 21:13:03 +0000693 if (!m_decl_map->GetFunctionAddress(str.c_str(), fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +0000694 {
695 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000696 log->Printf("Function %s had no address", str.c_str());
Sean Callanan92aa6662010-09-07 21:49:41 +0000697
698 return false;
699 }
Sean Callananf5857a02010-07-31 01:32:05 +0000700 }
701 }
702 else
703 {
Sean Callananc04743d2010-09-28 21:13:03 +0000704 if (!m_decl_map->GetFunctionAddress(str.c_str(), fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000705 {
706 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000707 log->Printf("Metadataless function %s had no address", str.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000708 }
Sean Callananba992c52010-07-27 02:07:53 +0000709 }
710
711 if (log)
Sean Callananc04743d2010-09-28 21:13:03 +0000712 log->Printf("Found %s at %llx", str.c_str(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000713
Sean Callananf5857a02010-07-31 01:32:05 +0000714 Value *fun_addr_ptr;
715
716 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000717 {
Sean Callanan02fbafa2010-07-27 21:39:39 +0000718 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
719 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +0000720 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +0000721 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
722 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000723 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
724
725 if (fun_value_ptr)
726 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000727 }
Sean Callananf5857a02010-07-31 01:32:05 +0000728
729 if (fun_value_ptr)
730 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000731
Sean Callananf5857a02010-07-31 01:32:05 +0000732 C->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000733
Sean Callananc04743d2010-09-28 21:13:03 +0000734 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(M.getContext(), str);
Sean Callanane8a59a82010-09-13 21:34:21 +0000735
736 Value *values[1];
737 values[0] = func_name;
738 MDNode *func_metadata = MDNode::get(M.getContext(), values, 1);
739
740 C->setMetadata("lldb.call.realName", func_metadata);
741
742 if (log)
743 log->Printf("Set metadata for %p [%d, %s]", C, func_name->isString(), func_name->getAsString().c_str());
744
Sean Callananba992c52010-07-27 02:07:53 +0000745 return true;
746}
747
748bool
Sean Callananf5857a02010-07-31 01:32:05 +0000749IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000750{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000751 /////////////////////////////////////////////////////////////////////////
752 // Prepare the current basic block for execution in the remote process
753 //
754
Sean Callanan02fbafa2010-07-27 21:39:39 +0000755 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000756
757 for (ii = BB.begin();
758 ii != BB.end();
759 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000760 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000761 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000762
Sean Callanane8a59a82010-09-13 21:34:21 +0000763 if (m_resolve_vars)
764 {
765 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
766 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
767 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000768
Sean Callanane8a59a82010-09-13 21:34:21 +0000769 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
770 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
771 return false;
772 }
Sean Callananba992c52010-07-27 02:07:53 +0000773
774 if (CallInst *call = dyn_cast<CallInst>(&inst))
775 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000776 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000777 }
778
779 return true;
780}
781
Sean Callanan02fbafa2010-07-27 21:39:39 +0000782static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000783{
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000784 Constant *C;
Sean Callanan45839272010-07-24 01:37:44 +0000785
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000786 if (!(C = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +0000787 return false;
788
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000789 ConstantExpr *CE;
790
791 if ((CE = dyn_cast<ConstantExpr>(V)))
792 {
793 if (CE->getOpcode() != Instruction::BitCast)
794 return false;
795
796 C = CE->getOperand(0);
797 }
798
799 GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
Sean Callanan45839272010-07-24 01:37:44 +0000800
801 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
802 return false;
803
804 return true;
805}
806
807static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
808{
809 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
810
811 Value::use_iterator ui;
812
813 for (ui = guard_load->use_begin();
814 ui != guard_load->use_end();
815 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000816 {
Greg Clayton6e713402010-07-30 20:30:44 +0000817 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000818 {
819 // do nothing for the moment
820 }
821 else
822 {
823 ui->replaceUsesOfWith(guard_load, zero);
824 }
825 }
Sean Callanan45839272010-07-24 01:37:44 +0000826
827 guard_load->eraseFromParent();
828}
829
830static void ExciseGuardStore(Instruction* guard_store)
831{
832 guard_store->eraseFromParent();
833}
834
835bool
836IRForTarget::removeGuards(Module &M, BasicBlock &BB)
837{
838 ///////////////////////////////////////////////////////
839 // Eliminate any reference to guard variables found.
840 //
841
Sean Callanan02fbafa2010-07-27 21:39:39 +0000842 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000843
Sean Callanan02fbafa2010-07-27 21:39:39 +0000844 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000845 typedef InstrList::iterator InstrIterator;
846
847 InstrList guard_loads;
848 InstrList guard_stores;
849
850 for (ii = BB.begin();
851 ii != BB.end();
852 ++ii)
853 {
854 Instruction &inst = *ii;
855
856 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
857 if (isGuardVariableRef(load->getPointerOperand()))
858 guard_loads.push_back(&inst);
859
860 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
861 if (isGuardVariableRef(store->getPointerOperand()))
862 guard_stores.push_back(&inst);
863 }
864
865 InstrIterator iter;
866
867 for (iter = guard_loads.begin();
868 iter != guard_loads.end();
869 ++iter)
870 TurnGuardLoadIntoZero(*iter, M);
871
872 for (iter = guard_stores.begin();
873 iter != guard_stores.end();
874 ++iter)
875 ExciseGuardStore(*iter);
876
877 return true;
878}
879
Sean Callananbafd6852010-07-14 23:40:29 +0000880// UnfoldConstant operates on a constant [C] which has just been replaced with a value
881// [new_value]. We assume that new_value has been properly placed early in the function,
882// most likely somewhere in front of the first instruction in the entry basic block
883// [first_entry_instruction].
884//
885// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
886// Where those uses are constants, the function generates new instructions to compute the
887// result of the new, non-constant expression and places them before first_entry_instruction.
888// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
889// for those.
890
891static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000892UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000893{
894 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
895
896 Value::use_iterator ui;
897
Sean Callanana48fe162010-08-11 03:57:18 +0000898 SmallVector<User*, 16> users;
899
900 // We do this because the use list might change, invalidating our iterator.
901 // Much better to keep a work list ourselves.
Sean Callananbafd6852010-07-14 23:40:29 +0000902 for (ui = C->use_begin();
903 ui != C->use_end();
904 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +0000905 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +0000906
Sean Callanana48fe162010-08-11 03:57:18 +0000907 for (int i = 0;
908 i < users.size();
909 ++i)
910 {
911 User *user = users[i];
912
Sean Callananbafd6852010-07-14 23:40:29 +0000913 if (Constant *constant = dyn_cast<Constant>(user))
914 {
915 // synthesize a new non-constant equivalent of the constant
916
917 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
918 {
919 switch (constant_expr->getOpcode())
920 {
921 default:
922 if (log)
923 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
924 return false;
925 case Instruction::BitCast:
926 {
927 // UnaryExpr
928 // OperandList[0] is value
929
930 Value *s = constant_expr->getOperand(0);
931
932 if (s == C)
933 s = new_value;
934
935 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
936
937 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
938 }
939 break;
940 case Instruction::GetElementPtr:
941 {
942 // GetElementPtrConstantExpr
943 // OperandList[0] is base
944 // OperandList[1]... are indices
945
946 Value *ptr = constant_expr->getOperand(0);
947
948 if (ptr == C)
949 ptr = new_value;
950
951 SmallVector<Value*, 16> indices;
952
953 unsigned operand_index;
954 unsigned num_operands = constant_expr->getNumOperands();
955
956 for (operand_index = 1;
957 operand_index < num_operands;
958 ++operand_index)
959 {
960 Value *operand = constant_expr->getOperand(operand_index);
961
962 if (operand == C)
963 operand = new_value;
964
965 indices.push_back(operand);
966 }
967
968 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
969
970 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
971 }
972 break;
973 }
974 }
975 else
976 {
977 if (log)
978 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
979 return false;
980 }
981 }
982 else
983 {
984 // simple fall-through case for non-constants
985 user->replaceUsesOfWith(C, new_value);
986 }
987 }
988
989 return true;
990}
991
Sean Callanan8bce6652010-07-13 21:41:46 +0000992bool
Sean Callananf5857a02010-07-31 01:32:05 +0000993IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +0000994{
Sean Callanane8a59a82010-09-13 21:34:21 +0000995 if (!m_resolve_vars)
996 return true;
997
Sean Callanan8bce6652010-07-13 21:41:46 +0000998 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
999
1000 m_decl_map->DoStructLayout();
1001
1002 if (log)
1003 log->Printf("Element arrangement:");
1004
1005 uint32_t num_elements;
1006 uint32_t element_index;
1007
1008 size_t size;
1009 off_t alignment;
1010
1011 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1012 return false;
1013
Sean Callananf5857a02010-07-31 01:32:05 +00001014 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001015
Sean Callananf5857a02010-07-31 01:32:05 +00001016 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001017 return false;
1018
Sean Callanan02fbafa2010-07-27 21:39:39 +00001019 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001020
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001021 if (argument->getName().equals("this"))
1022 {
1023 ++iter;
1024
1025 if (iter == F.getArgumentList().end())
1026 return false;
1027
1028 argument = iter;
1029 }
1030
Sean Callanan8bce6652010-07-13 21:41:46 +00001031 if (!argument->getName().equals("___clang_arg"))
1032 return false;
1033
1034 if (log)
1035 log->Printf("Arg: %s", PrintValue(argument).c_str());
1036
Sean Callananf5857a02010-07-31 01:32:05 +00001037 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +00001038 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001039
1040 if (!first_entry_instruction)
1041 return false;
1042
1043 LLVMContext &context(M.getContext());
1044 const IntegerType *offset_type(Type::getInt32Ty(context));
1045
1046 if (!offset_type)
1047 return false;
1048
1049 for (element_index = 0; element_index < num_elements; ++element_index)
1050 {
1051 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001052 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001053 off_t offset;
Sean Callanan45690fe2010-08-30 22:17:16 +00001054 const char *name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001055
Sean Callanan45690fe2010-08-30 22:17:16 +00001056 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001057 return false;
1058
1059 if (log)
Sean Callanan45690fe2010-08-30 22:17:16 +00001060 log->Printf(" %s [%s] (%s) placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001061 value->getName().str().c_str(),
Sean Callanan45690fe2010-08-30 22:17:16 +00001062 name,
Sean Callanan8bce6652010-07-13 21:41:46 +00001063 PrintValue(value, true).c_str(),
1064 offset);
1065
1066 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
1067 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
1068 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
1069
Sean Callananbafd6852010-07-14 23:40:29 +00001070 if (Constant *constant = dyn_cast<Constant>(value))
1071 UnfoldConstant(constant, bit_cast, first_entry_instruction);
1072 else
1073 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +00001074 }
1075
1076 if (log)
1077 log->Printf("Total structure [align %d, size %d]", alignment, size);
1078
1079 return true;
1080}
1081
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001082bool
1083IRForTarget::runOnModule(Module &M)
1084{
1085 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1086
Sean Callanan65dafa82010-08-27 01:01:44 +00001087 Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001088
1089 if (!function)
1090 {
1091 if (log)
Sean Callanan65dafa82010-08-27 01:01:44 +00001092 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001093
1094 return false;
1095 }
1096
Sean Callanan02fbafa2010-07-27 21:39:39 +00001097 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001098
Sean Callanan82b74c82010-08-12 01:56:52 +00001099 ////////////////////////////////////////////////////////////
1100 // Replace __clang_expr_result with a persistent variable
1101 //
1102
1103 if (!createResultVariable(M, *function))
1104 return false;
1105
Sean Callananf5857a02010-07-31 01:32:05 +00001106 //////////////////////////////////
1107 // Run basic-block level passes
1108 //
1109
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001110 for (bbi = function->begin();
1111 bbi != function->end();
1112 ++bbi)
1113 {
Sean Callanan8c127202010-08-23 23:09:38 +00001114 if (!removeGuards(M, *bbi))
1115 return false;
1116
Sean Callanana48fe162010-08-11 03:57:18 +00001117 if (!rewritePersistentAllocs(M, *bbi))
Sean Callananf5857a02010-07-31 01:32:05 +00001118 return false;
1119
Sean Callanana48fe162010-08-11 03:57:18 +00001120 if (!rewriteObjCSelectors(M, *bbi))
1121 return false;
1122
Sean Callananf5857a02010-07-31 01:32:05 +00001123 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +00001124 return false;
1125 }
1126
Sean Callanan771131d2010-09-30 21:18:25 +00001127 ///////////////////////////////
1128 // Run function-level passes
1129 //
1130
1131 if (!replaceVariables(M, *function))
1132 return false;
1133
Sean Callanan8bce6652010-07-13 21:41:46 +00001134 if (log)
1135 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001136 std::string s;
1137 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001138
Sean Callanan321fe9e2010-07-28 01:00:59 +00001139 M.print(oss, NULL);
1140
1141 oss.flush();
1142
1143 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001144 }
1145
1146 return true;
1147}
1148
1149void
1150IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +00001151 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001152{
1153}
1154
1155PassManagerType
1156IRForTarget::getPotentialPassManagerType() const
1157{
1158 return PMT_ModulePassManager;
1159}