1 //===-- IRForTarget.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Expression/IRForTarget.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/InstrTypes.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Transforms/IPO.h"
21 #include "llvm/IR/ValueSymbolTable.h"
23 #include "clang/AST/ASTContext.h"
25 #include "lldb/Core/dwarf.h"
26 #include "lldb/Core/ConstString.h"
27 #include "lldb/Core/DataBufferHeap.h"
28 #include "lldb/Core/Log.h"
29 #include "lldb/Core/Scalar.h"
30 #include "lldb/Core/StreamString.h"
31 #include "lldb/Expression/ClangExpressionDeclMap.h"
32 #include "lldb/Expression/IRExecutionUnit.h"
33 #include "lldb/Expression/IRInterpreter.h"
34 #include "lldb/Host/Endian.h"
35 #include "lldb/Symbol/ClangASTContext.h"
36 #include "lldb/Symbol/ClangASTType.h"
44 IRForTarget::StaticDataAllocator::StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit) :
45 m_execution_unit(execution_unit),
46 m_stream_string(lldb_private::Stream::eBinary, execution_unit.GetAddressByteSize(), execution_unit.GetByteOrder()),
47 m_allocation(LLDB_INVALID_ADDRESS)
51 IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) :
57 IRForTarget::FunctionValueCache::~FunctionValueCache()
61 llvm::Value *IRForTarget::FunctionValueCache::GetValue(llvm::Function *function)
63 if (!m_values.count(function))
65 llvm::Value *ret = m_maker(function);
66 m_values[function] = ret;
69 return m_values[function];
72 lldb::addr_t IRForTarget::StaticDataAllocator::Allocate()
74 lldb_private::Error err;
76 if (m_allocation != LLDB_INVALID_ADDRESS)
78 m_execution_unit.FreeNow(m_allocation);
79 m_allocation = LLDB_INVALID_ADDRESS;
82 m_allocation = m_execution_unit.WriteNow((const uint8_t*)m_stream_string.GetData(), m_stream_string.GetSize(), err);
87 static llvm::Value *FindEntryInstruction (llvm::Function *function)
89 if (function->empty())
92 return function->getEntryBlock().getFirstNonPHIOrDbg();
95 IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
97 lldb_private::IRExecutionUnit &execution_unit,
98 lldb_private::Stream *error_stream,
99 const char *func_name) :
101 m_resolve_vars(resolve_vars),
102 m_func_name(func_name),
104 m_decl_map(decl_map),
105 m_data_allocator(execution_unit),
106 m_CFStringCreateWithBytes(NULL),
107 m_sel_registerName(NULL),
109 m_error_stream(error_stream),
110 m_result_store(NULL),
111 m_result_is_pointer(false),
112 m_reloc_placeholder(NULL),
113 m_entry_instruction_finder (FindEntryInstruction)
117 /* Handy utility functions used at several places in the code */
120 PrintValue(const Value *value, bool truncate = false)
125 raw_string_ostream rso(s);
129 s.resize(s.length() - 1);
135 PrintType(const llvm::Type *type, bool truncate = false)
138 raw_string_ostream rso(s);
142 s.resize(s.length() - 1);
146 IRForTarget::~IRForTarget()
151 IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
153 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
155 std::string name = llvm_function.getName().str();
160 IRForTarget::LookupResult
161 IRForTarget::GetFunctionAddress (llvm::Function *fun,
163 lldb_private::ConstString &name,
164 Constant **&value_ptr)
166 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
168 fun_addr = LLDB_INVALID_ADDRESS;
172 if (fun->isIntrinsic())
174 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
176 switch (intrinsic_id)
180 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
183 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
185 return LookupResult::Fail;
186 case Intrinsic::memcpy:
188 static lldb_private::ConstString g_memcpy_str ("memcpy");
192 case Intrinsic::memset:
194 static lldb_private::ConstString g_memset_str ("memset");
198 case Intrinsic::dbg_declare:
199 case Intrinsic::dbg_value:
200 return LookupResult::Ignore;
204 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
208 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
211 // Find the address of the function.
213 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
217 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
219 lldb_private::ConstString altnernate_name;
220 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
223 // Check for an alternate mangling for "std::basic_string<char>"
224 // that is part of the itanium C++ name mangling scheme
225 const char *name_cstr = name.GetCString();
226 if (name_cstr && strncmp(name_cstr, "_ZNKSbIcE", strlen("_ZNKSbIcE")) == 0)
228 std::string alternate_mangling("_ZNKSs");
229 alternate_mangling.append (name_cstr + strlen("_ZNKSbIcE"));
230 altnernate_name.SetCString(alternate_mangling.c_str());
231 found_it = m_decl_map->GetFunctionAddress (altnernate_name, fun_addr);
237 lldb_private::Mangled mangled_name(name);
238 lldb_private::Mangled alt_mangled_name(altnernate_name);
241 if (alt_mangled_name)
242 log->Printf("Function \"%s\" (alternate name \"%s\") has no address",
243 mangled_name.GetName().GetCString(),
244 alt_mangled_name.GetName().GetCString());
246 log->Printf("Function \"%s\" had no address",
247 mangled_name.GetName().GetCString());
252 if (alt_mangled_name)
253 m_error_stream->Printf("error: call to a function '%s' (alternate name '%s') that is not present in the target\n",
254 mangled_name.GetName().GetCString(),
255 alt_mangled_name.GetName().GetCString());
256 else if (mangled_name.GetMangledName())
257 m_error_stream->Printf("error: call to a function '%s' ('%s') that is not present in the target\n",
258 mangled_name.GetName().GetCString(),
259 mangled_name.GetMangledName().GetCString());
261 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
262 mangled_name.GetName().GetCString());
264 return LookupResult::Fail;
270 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
273 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
276 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
278 return LookupResult::Fail;
283 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), fun_addr);
285 return LookupResult::Success;
289 IRForTarget::BuildFunctionPointer (llvm::Type *type,
292 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
293 Constant *fun_addr_int = ConstantInt::get(m_intptr_ty, ptr, false);
294 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
298 IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
299 llvm::Value *function_ptr,
302 for (llvm::User *user : function_ptr->users())
304 if (Instruction *user_inst = dyn_cast<Instruction>(user))
306 MDString* md_name = MDString::get(context, StringRef(name));
308 MDNode *metadata = MDNode::get(context, md_name);
310 user_inst->setMetadata("lldb.call.realName", metadata);
314 RegisterFunctionMetadata (context, user, name);
320 IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module)
322 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
324 for (llvm::Module::iterator fi = llvm_module.begin();
325 fi != llvm_module.end();
330 bool is_decl = fun->isDeclaration();
333 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
338 if (fun->use_empty())
341 uint64_t addr = LLDB_INVALID_ADDRESS;
342 lldb_private::ConstString name;
343 Constant **value_ptr = NULL;
345 LookupResult result = GetFunctionAddress(fun,
352 case LookupResult::Fail:
353 return false; // GetFunctionAddress reports its own errors
355 case LookupResult::Ignore:
356 break; // Nothing to do
358 case LookupResult::Success:
360 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
362 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
367 // If we are replacing a function with the nobuiltin attribute, it may
368 // be called with the builtin attribute on call sites. Remove any such
369 // attributes since it's illegal to have a builtin call to something
370 // other than a nobuiltin function.
371 if (fun->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
372 llvm::Attribute builtin = llvm::Attribute::get(fun->getContext(), llvm::Attribute::Builtin);
374 for (auto u : fun->users()) {
375 if (auto call = dyn_cast<CallInst>(u)) {
376 call->removeAttribute(AttributeSet::FunctionIndex, builtin);
381 fun->replaceAllUsesWith(value);
392 IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
394 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
399 unsigned num_nodes = named_metadata->getNumOperands();
403 node_index < num_nodes;
406 MDNode *metadata_node = named_metadata->getOperand(node_index);
411 if (metadata_node->getNumOperands() != 2)
414 if (metadata_node->getOperand(0) != global_val)
417 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
422 uintptr_t ptr = constant_int->getZExtValue();
424 return reinterpret_cast<clang::NamedDecl *>(ptr);
431 IRForTarget::DeclForGlobal (GlobalValue *global_val)
433 return DeclForGlobal(global_val, m_module);
437 IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
439 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
444 // Find the result variable. If it doesn't exist, we can give up right here.
446 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
448 std::string result_name_str;
449 const char *result_name = NULL;
451 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
455 result_name_str = vi->first().str();
456 const char *value_name = result_name_str.c_str();
458 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
459 strncmp(value_name, "_ZGV", 4))
461 result_name = value_name;
462 m_result_is_pointer = true;
466 if (strstr(value_name, "$__lldb_expr_result") &&
467 strncmp(value_name, "_ZGV", 4))
469 result_name = value_name;
470 m_result_is_pointer = false;
478 log->PutCString("Couldn't find result variable");
484 log->Printf("Result name: \"%s\"", result_name);
486 Value *result_value = m_module->getNamedValue(result_name);
491 log->PutCString("Result variable had no data");
494 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
500 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
502 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
507 log->PutCString("Result variable isn't a GlobalVariable");
510 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
515 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
519 log->PutCString("Result variable doesn't have a corresponding Decl");
522 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
529 std::string decl_desc_str;
530 raw_string_ostream decl_desc_stream(decl_desc_str);
531 result_decl->print(decl_desc_stream);
532 decl_desc_stream.flush();
534 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
537 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
541 log->PutCString("Result variable Decl isn't a VarDecl");
544 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
549 // Get the next available result name from m_decl_map and create the persistent
552 // If the result is an Lvalue, it is emitted as a pointer; see
553 // ASTResultSynthesizer::SynthesizeBodyResult.
554 if (m_result_is_pointer)
556 clang::QualType pointer_qual_type = result_var->getType();
557 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
559 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
560 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
562 if (pointer_pointertype)
564 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
566 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
567 &result_decl->getASTContext());
569 else if (pointer_objcobjpointertype)
571 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
573 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
574 &result_decl->getASTContext());
579 log->PutCString("Expected result to have pointer type, but it did not");
582 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
589 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
590 &result_decl->getASTContext());
593 if (m_result_type.GetBitSize() == 0)
595 lldb_private::StreamString type_desc_stream;
596 m_result_type.DumpTypeDescription(&type_desc_stream);
599 log->Printf("Result type has size 0");
602 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
603 type_desc_stream.GetData());
609 lldb_private::StreamString type_desc_stream;
610 m_result_type.DumpTypeDescription(&type_desc_stream);
612 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
615 m_result_name = lldb_private::ConstString("$RESULT_NAME");
618 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
619 m_result_name.GetCString(),
620 m_result_type.GetByteSize());
622 // Construct a new result global and set up its metadata
624 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
625 result_global->getType()->getElementType(),
626 false, /* not constant */
627 GlobalValue::ExternalLinkage,
628 NULL, /* no initializer */
629 m_result_name.GetCString ());
631 // It's too late in compilation to create a new VarDecl for this, but we don't
632 // need to. We point the metadata at the old VarDecl. This creates an odd
633 // anomaly: a variable with a Value whose name is something like $0 and a
634 // Decl whose name is $__lldb_expr_result. This condition is handled in
635 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
638 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
639 reinterpret_cast<uint64_t>(result_decl),
642 llvm::Value* values[2];
643 values[0] = new_result_global;
644 values[1] = new_constant_int;
646 ArrayRef<Value*> value_ref(values, 2);
648 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
649 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
650 named_metadata->addOperand(persistent_global_md);
653 log->Printf("Replacing \"%s\" with \"%s\"",
654 PrintValue(result_global).c_str(),
655 PrintValue(new_result_global).c_str());
657 if (result_global->use_empty())
659 // We need to synthesize a store for this variable, because otherwise
660 // there's nothing to put into its equivalent persistent variable.
662 BasicBlock &entry_block(llvm_function.getEntryBlock());
663 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
665 if (!first_entry_instruction)
668 if (!result_global->hasInitializer())
671 log->Printf("Couldn't find initializer for unused variable");
674 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
679 Constant *initializer = result_global->getInitializer();
681 StoreInst *synthesized_store = new StoreInst(initializer,
683 first_entry_instruction);
686 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
690 result_global->replaceAllUsesWith(new_result_global);
693 if (!m_decl_map->AddPersistentVariable(result_decl,
697 m_result_is_pointer))
700 result_global->eraseFromParent();
706 IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
707 llvm::GlobalVariable *cstr)
709 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
711 Type *ns_str_ty = ns_str->getType();
713 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
714 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
715 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
717 if (!m_CFStringCreateWithBytes)
719 lldb::addr_t CFStringCreateWithBytes_addr;
721 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
723 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
726 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
729 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
735 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
737 // Build the function type:
739 // CFStringRef CFStringCreateWithBytes (
740 // CFAllocatorRef alloc,
741 // const UInt8 *bytes,
743 // CFStringEncoding encoding,
744 // Boolean isExternalRepresentation
747 // We make the following substitutions:
749 // CFStringRef -> i8*
750 // CFAllocatorRef -> i8*
752 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
753 // CFStringEncoding -> i32
756 Type *arg_type_array[5];
758 arg_type_array[0] = i8_ptr_ty;
759 arg_type_array[1] = i8_ptr_ty;
760 arg_type_array[2] = m_intptr_ty;
761 arg_type_array[3] = i32_ty;
762 arg_type_array[4] = i8_ty;
764 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
766 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
768 // Build the constant containing the pointer to the function
769 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
770 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
771 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
774 ConstantDataSequential *string_array = NULL;
777 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
779 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
780 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
781 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
782 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
783 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
785 Value *argument_array[5];
787 argument_array[0] = alloc_arg;
788 argument_array[1] = bytes_arg;
789 argument_array[2] = numBytes_arg;
790 argument_array[3] = encoding_arg;
791 argument_array[4] = isExternal_arg;
793 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
795 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
796 return CallInst::Create(m_CFStringCreateWithBytes,
798 "CFStringCreateWithBytes",
799 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
802 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
805 log->PutCString("Couldn't replace the NSString with the result of the call");
808 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
813 ns_str->eraseFromParent();
819 IRForTarget::RewriteObjCConstStrings()
821 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
823 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
825 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
829 std::string value_name = vi->first().str();
830 const char *value_name_cstr = value_name.c_str();
832 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
834 Value *nsstring_value = vi->second;
836 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
838 if (!nsstring_global)
841 log->PutCString("NSString variable is not a GlobalVariable");
844 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
849 if (!nsstring_global->hasInitializer())
852 log->PutCString("NSString variable does not have an initializer");
855 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
860 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
862 if (!nsstring_struct)
865 log->PutCString("NSString variable's initializer is not a ConstantStruct");
868 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
873 // We expect the following structure:
882 if (nsstring_struct->getNumOperands() != 4)
885 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
888 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
893 Constant *nsstring_member = nsstring_struct->getOperand(2);
895 if (!nsstring_member)
898 log->PutCString("NSString initializer's str element was empty");
901 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
906 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
911 log->PutCString("NSString initializer's str element is not a ConstantExpr");
914 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
919 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
922 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
925 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
930 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
932 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
937 log->PutCString("NSString initializer's str element is not a GlobalVariable");
940 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
945 if (!cstr_global->hasInitializer())
948 log->PutCString("NSString initializer's str element does not have an initializer");
951 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
960 log->PutCString("NSString initializer's str element is not a ConstantArray");
963 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
968 if (!cstr_array->isCString())
971 log->PutCString("NSString initializer's str element is not a C string array");
974 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
980 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
985 log->Printf("Found NSString constant %s, which contains \"%s\"", value_name_cstr, cstr_array->getAsString().str().c_str());
987 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
993 if (!RewriteObjCConstString(nsstring_global, cstr_global))
996 log->PutCString("Error rewriting the constant string");
998 // We don't print an error message here because RewriteObjCConstString has done so for us.
1005 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1009 std::string value_name = vi->first().str();
1010 const char *value_name_cstr = value_name.c_str();
1012 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
1014 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
1019 log->PutCString("__CFConstantStringClassReference is not a global variable");
1022 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
1027 gv->eraseFromParent();
1036 static bool IsObjCSelectorRef (Value *value)
1038 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
1040 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
1046 // This function does not report errors; its callers are responsible.
1048 IRForTarget::RewriteObjCSelector (Instruction* selector_load)
1050 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1052 LoadInst *load = dyn_cast<LoadInst>(selector_load);
1057 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1059 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
1060 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1062 // where %obj is the object pointer and %tmp is the selector.
1064 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
1065 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
1067 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
1069 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
1071 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1074 Constant *osr_initializer = _objc_selector_references_->getInitializer();
1076 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
1078 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1081 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1083 if (!osr_initializer_base)
1086 // Find the string's initializer (a ConstantArray) and get the string from it
1088 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
1090 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1093 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1095 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
1097 if (!omvn_initializer_array->isString())
1100 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
1103 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
1105 // Construct a call to sel_registerName
1107 if (!m_sel_registerName)
1109 lldb::addr_t sel_registerName_addr;
1111 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
1112 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
1116 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
1118 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
1120 // The below code would be "more correct," but in actuality what's required is uint8_t*
1121 //Type *sel_type = StructType::get(m_module->getContext());
1122 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
1123 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
1125 Type *type_array[1];
1127 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
1129 ArrayRef<Type *> srN_arg_types(type_array, 1);
1131 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
1133 // Build the constant containing the pointer to the function
1134 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
1135 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
1136 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1139 Value *argument_array[1];
1141 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
1143 argument_array[0] = omvn_pointer;
1145 ArrayRef<Value *> srN_arguments(argument_array, 1);
1147 CallInst *srN_call = CallInst::Create(m_sel_registerName,
1152 // Replace the load with the call in all users
1154 selector_load->replaceAllUsesWith(srN_call);
1156 selector_load->eraseFromParent();
1162 IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
1164 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1166 BasicBlock::iterator ii;
1168 typedef SmallVector <Instruction*, 2> InstrList;
1169 typedef InstrList::iterator InstrIterator;
1171 InstrList selector_loads;
1173 for (ii = basic_block.begin();
1174 ii != basic_block.end();
1177 Instruction &inst = *ii;
1179 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1180 if (IsObjCSelectorRef(load->getPointerOperand()))
1181 selector_loads.push_back(&inst);
1186 for (iter = selector_loads.begin();
1187 iter != selector_loads.end();
1190 if (!RewriteObjCSelector(*iter))
1193 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
1196 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
1205 // This function does not report errors; its callers are responsible.
1207 IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
1209 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1211 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1213 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1215 if (!alloc_md || !alloc_md->getNumOperands())
1218 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
1223 // We attempt to register this as a new persistent variable with the DeclMap.
1225 uintptr_t ptr = constant_int->getZExtValue();
1227 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
1229 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1230 &decl->getASTContext());
1232 StringRef decl_name (decl->getName());
1233 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
1234 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
1237 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
1239 false, /* not constant */
1240 GlobalValue::ExternalLinkage,
1241 NULL, /* no initializer */
1242 alloc->getName().str().c_str());
1244 // What we're going to do here is make believe this was a regular old external
1245 // variable. That means we need to make the metadata valid.
1247 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
1249 llvm::Value* values[2];
1250 values[0] = persistent_global;
1251 values[1] = constant_int;
1253 ArrayRef<llvm::Value*> value_ref(values, 2);
1255 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
1256 named_metadata->addOperand(persistent_global_md);
1258 // Now, since the variable is a pointer variable, we will drop in a load of that
1259 // pointer variable.
1261 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
1264 log->Printf("Replacing \"%s\" with \"%s\"",
1265 PrintValue(alloc).c_str(),
1266 PrintValue(persistent_load).c_str());
1268 alloc->replaceAllUsesWith(persistent_load);
1269 alloc->eraseFromParent();
1275 IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
1277 if (!m_resolve_vars)
1280 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1282 BasicBlock::iterator ii;
1284 typedef SmallVector <Instruction*, 2> InstrList;
1285 typedef InstrList::iterator InstrIterator;
1287 InstrList pvar_allocs;
1289 for (ii = basic_block.begin();
1290 ii != basic_block.end();
1293 Instruction &inst = *ii;
1295 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
1297 llvm::StringRef alloc_name = alloc->getName();
1299 if (alloc_name.startswith("$") &&
1300 !alloc_name.startswith("$__lldb"))
1302 if (alloc_name.find_first_of("0123456789") == 1)
1305 log->Printf("Rejecting a numeric persistent variable.");
1308 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
1313 pvar_allocs.push_back(alloc);
1320 for (iter = pvar_allocs.begin();
1321 iter != pvar_allocs.end();
1324 if (!RewritePersistentAlloc(*iter))
1327 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
1330 log->PutCString("Couldn't rewrite the creation of a persistent variable");
1340 IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1345 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1347 if (log && log->GetVerbose())
1348 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
1350 Type *initializer_type = initializer->getType();
1352 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1354 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1357 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
1359 if (array_initializer->isString())
1361 std::string array_initializer_string = array_initializer->getAsString();
1362 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1366 ArrayType *array_initializer_type = array_initializer->getType();
1367 Type *array_element_type = array_initializer_type->getElementType();
1369 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
1371 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
1373 Value *operand_value = array_initializer->getOperand(i);
1374 Constant *operand_constant = dyn_cast<Constant>(operand_value);
1376 if (!operand_constant)
1379 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
1385 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1387 StructType *struct_initializer_type = struct_initializer->getType();
1388 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1390 for (unsigned i = 0;
1391 i < struct_initializer->getNumOperands();
1394 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1399 else if (isa<ConstantAggregateZero>(initializer))
1401 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1408 IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1410 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1413 if (global_variable == m_reloc_placeholder)
1416 uint64_t offset = m_data_allocator.GetStream().GetSize();
1418 llvm::Type *variable_type = global_variable->getType();
1420 Constant *initializer = global_variable->getInitializer();
1422 llvm::Type *initializer_type = initializer->getType();
1424 size_t size = m_target_data->getTypeAllocSize(initializer_type);
1425 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
1427 const size_t mask = (align - 1);
1428 uint64_t aligned_offset = (offset + mask) & ~mask;
1429 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
1430 offset = aligned_offset;
1432 lldb_private::DataBufferHeap data(size, '\0');
1435 if (!MaterializeInitializer(data.GetBytes(), initializer))
1438 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
1440 Constant *new_pointer = BuildRelocation(variable_type, offset);
1442 global_variable->replaceAllUsesWith(new_pointer);
1444 global_variable->eraseFromParent();
1449 // This function does not report errors; its callers are responsible.
1451 IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
1453 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1456 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
1458 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
1460 switch (constant_expr->getOpcode())
1464 case Instruction::GetElementPtr:
1465 case Instruction::BitCast:
1466 Value *s = constant_expr->getOperand(0);
1467 if (!MaybeHandleVariable(s))
1471 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
1473 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1474 return MaterializeInternalVariable(global_variable);
1476 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
1480 if (IsObjCSelectorRef(llvm_value_ptr))
1483 if (!global_variable->hasExternalLinkage())
1487 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
1492 std::string name (named_decl->getName().str());
1494 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1495 if (value_decl == NULL)
1498 lldb_private::ClangASTType clang_type(&value_decl->getASTContext(), value_decl->getType());
1500 const Type *value_type = NULL;
1504 // The $__lldb_expr_result name indicates the the return value has allocated as
1505 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1506 // accesses to this static variable need to be redirected to the result of dereferencing
1507 // a pointer that is passed in as one of the arguments.
1509 // Consequently, when reporting the size of the type, we report a pointer type pointing
1510 // to the type of $__lldb_expr_result, not the type itself.
1512 // We also do this for any user-declared persistent variables.
1513 clang_type = clang_type.GetPointerType();
1514 value_type = PointerType::get(global_variable->getType(), 0);
1518 value_type = global_variable->getType();
1521 const uint64_t value_size = clang_type.GetByteSize();
1522 lldb::offset_t value_alignment = (clang_type.GetTypeBitAlign() + 7ull) / 8ull;
1526 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
1528 clang_type.GetQualType().getAsString().c_str(),
1529 PrintType(value_type).c_str(),
1535 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
1536 lldb_private::ConstString (name.c_str()),
1541 if (!global_variable->hasExternalLinkage())
1543 else if (HandleSymbol (global_variable))
1549 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
1552 log->Printf("Function pointers aren't handled right now");
1560 // This function does not report errors; its callers are responsible.
1562 IRForTarget::HandleSymbol (Value *symbol)
1564 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1566 lldb_private::ConstString name(symbol->getName().str().c_str());
1568 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
1570 if (symbol_addr == LLDB_INVALID_ADDRESS)
1573 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
1579 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
1581 Type *symbol_type = symbol->getType();
1583 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
1585 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1588 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
1590 symbol->replaceAllUsesWith(symbol_addr_ptr);
1596 IRForTarget::MaybeHandleCallArguments (CallInst *Old)
1598 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1601 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
1603 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
1606 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
1609 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
1618 IRForTarget::HandleObjCClass(Value *classlist_reference)
1620 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1622 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
1624 if (!global_variable)
1627 Constant *initializer = global_variable->getInitializer();
1632 if (!initializer->hasName())
1635 StringRef name(initializer->getName());
1636 lldb_private::ConstString name_cstr(name.str().c_str());
1637 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
1640 log->Printf("Found reference to Objective-C class %s (0x%llx)", name_cstr.AsCString(), (unsigned long long)class_ptr);
1642 if (class_ptr == LLDB_INVALID_ADDRESS)
1645 if (global_variable->use_empty())
1648 SmallVector<LoadInst *, 2> load_instructions;
1650 for (llvm::User *u : global_variable->users())
1652 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
1653 load_instructions.push_back(load_instruction);
1656 if (load_instructions.empty())
1659 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
1661 for (LoadInst *load_instruction : load_instructions)
1663 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1665 load_instruction->replaceAllUsesWith(class_bitcast);
1667 load_instruction->eraseFromParent();
1674 IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1676 BasicBlock::iterator ii;
1678 std::vector<CallInst *> calls_to_remove;
1680 for (ii = basic_block.begin();
1681 ii != basic_block.end();
1684 Instruction &inst = *ii;
1686 CallInst *call = dyn_cast<CallInst>(&inst);
1688 // MaybeHandleCallArguments handles error reporting; we are silent here
1692 bool remove = false;
1694 llvm::Function *func = call->getCalledFunction();
1696 if (func && func->getName() == "__cxa_atexit")
1699 llvm::Value *val = call->getCalledValue();
1701 if (val && val->getName() == "__cxa_atexit")
1705 calls_to_remove.push_back(call);
1708 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1712 (*ci)->eraseFromParent();
1719 IRForTarget::ResolveCalls(BasicBlock &basic_block)
1721 /////////////////////////////////////////////////////////////////////////
1722 // Prepare the current basic block for execution in the remote process
1725 BasicBlock::iterator ii;
1727 for (ii = basic_block.begin();
1728 ii != basic_block.end();
1731 Instruction &inst = *ii;
1733 CallInst *call = dyn_cast<CallInst>(&inst);
1735 // MaybeHandleCallArguments handles error reporting; we are silent here
1736 if (call && !MaybeHandleCallArguments(call))
1744 IRForTarget::ResolveExternals (Function &llvm_function)
1746 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1748 for (GlobalVariable &global_var : m_module->globals())
1750 std::string global_name = global_var.getName().str();
1753 log->Printf("Examining %s, DeclForGlobalValue returns %p",
1754 global_name.c_str(),
1755 static_cast<void*>(DeclForGlobal(&global_var)));
1757 if (global_name.find("OBJC_IVAR") == 0)
1759 if (!HandleSymbol(&global_var))
1762 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", global_name.c_str());
1767 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1769 if (!HandleObjCClass(&global_var))
1772 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
1777 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1779 if (!HandleObjCClass(&global_var))
1782 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
1787 else if (DeclForGlobal(&global_var))
1789 if (!MaybeHandleVariable (&global_var))
1792 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", global_name.c_str());
1803 IRForTarget::ReplaceStrings ()
1805 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1807 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
1811 for (GlobalVariable &gv : m_module->globals())
1813 if (!gv.hasInitializer())
1816 Constant *gc = gv.getInitializer();
1820 if (gc->isNullValue())
1822 Type *gc_type = gc->getType();
1824 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
1829 Type *gc_element_type = gc_array_type->getElementType();
1831 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
1833 if (gc_integer_type->getBitWidth() != 8)
1840 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
1845 if (!gc_array->isCString())
1849 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
1851 str = gc_array->getAsString();
1854 offsets[&gv] = m_data_allocator.GetStream().GetSize();
1856 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
1859 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
1861 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1865 GlobalVariable *gv = oi->first;
1866 size_t offset = oi->second;
1868 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
1871 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
1873 for (llvm::User *u : gv->users())
1876 log->Printf("Found use %s", PrintValue(u).c_str());
1878 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(u);
1879 StoreInst *store_inst = dyn_cast<StoreInst>(u);
1883 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1886 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
1891 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1892 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
1894 const_expr->replaceAllUsesWith(new_gep);
1896 else if (store_inst)
1898 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
1900 store_inst->setOperand(0, bit_cast);
1905 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
1911 gv->eraseFromParent();
1918 IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1920 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1922 typedef SmallVector <Value*, 2> ConstantList;
1923 typedef SmallVector <llvm::Instruction*, 2> UserList;
1924 typedef ConstantList::iterator ConstantIterator;
1925 typedef UserList::iterator UserIterator;
1927 ConstantList static_constants;
1928 UserList static_users;
1930 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1934 llvm::Instruction &inst = *ii;
1936 for (Value *operand_val : inst.operand_values())
1938 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1940 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
1942 static_constants.push_back(operand_val);
1943 static_users.push_back(ii);
1948 ConstantIterator constant_iter;
1949 UserIterator user_iter;
1951 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1952 constant_iter != static_constants.end();
1953 ++constant_iter, ++user_iter)
1955 Value *operand_val = *constant_iter;
1956 llvm::Instruction *inst = *user_iter;
1958 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1960 if (operand_constant_fp)
1962 Type *operand_type = operand_constant_fp->getType();
1964 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1965 APInt operand_apint = operand_apfloat.bitcastToAPInt();
1967 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1968 size_t operand_data_size = operand_apint.getBitWidth() / 8;
1973 raw_string_ostream ss(s);
1974 for (size_t index = 0;
1975 index < operand_data_size;
1978 ss << (uint32_t)operand_raw_data[index];
1983 log->Printf("Found ConstantFP with size %" PRIu64 " and raw data %s", (uint64_t)operand_data_size, s.c_str());
1986 lldb_private::DataBufferHeap data(operand_data_size, 0);
1988 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
1990 uint8_t *data_bytes = data.GetBytes();
1992 for (size_t index = 0;
1993 index < operand_data_size;
1996 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
2001 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2004 uint64_t offset = m_data_allocator.GetStream().GetSize();
2006 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
2008 const size_t mask = (align - 1);
2009 uint64_t aligned_offset = (offset + mask) & ~mask;
2010 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
2012 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
2014 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
2016 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
2018 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
2020 operand_constant_fp->replaceAllUsesWith(fp_load);
2027 static bool isGuardVariableRef(Value *V)
2029 Constant *Old = NULL;
2031 if (!(Old = dyn_cast<Constant>(V)))
2034 ConstantExpr *CE = NULL;
2036 if ((CE = dyn_cast<ConstantExpr>(V)))
2038 if (CE->getOpcode() != Instruction::BitCast)
2041 Old = CE->getOperand(0);
2044 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
2046 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
2053 IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
2055 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
2057 for (llvm::User *u : guard_load->users())
2059 if (isa<Constant>(u))
2061 // do nothing for the moment
2065 u->replaceUsesOfWith(guard_load, zero);
2069 guard_load->eraseFromParent();
2072 static void ExciseGuardStore(Instruction* guard_store)
2074 guard_store->eraseFromParent();
2078 IRForTarget::RemoveGuards(BasicBlock &basic_block)
2080 ///////////////////////////////////////////////////////
2081 // Eliminate any reference to guard variables found.
2084 BasicBlock::iterator ii;
2086 typedef SmallVector <Instruction*, 2> InstrList;
2087 typedef InstrList::iterator InstrIterator;
2089 InstrList guard_loads;
2090 InstrList guard_stores;
2092 for (ii = basic_block.begin();
2093 ii != basic_block.end();
2096 Instruction &inst = *ii;
2098 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2099 if (isGuardVariableRef(load->getPointerOperand()))
2100 guard_loads.push_back(&inst);
2102 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
2103 if (isGuardVariableRef(store->getPointerOperand()))
2104 guard_stores.push_back(&inst);
2109 for (iter = guard_loads.begin();
2110 iter != guard_loads.end();
2112 TurnGuardLoadIntoZero(*iter);
2114 for (iter = guard_stores.begin();
2115 iter != guard_stores.end();
2117 ExciseGuardStore(*iter);
2122 // This function does not report errors; its callers are responsible.
2124 IRForTarget::UnfoldConstant(Constant *old_constant,
2125 FunctionValueCache &value_maker,
2126 FunctionValueCache &entry_instruction_finder)
2128 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2130 SmallVector<User*, 16> users;
2132 // We do this because the use list might change, invalidating our iterator.
2133 // Much better to keep a work list ourselves.
2134 for (llvm::User *u : old_constant->users())
2141 User *user = users[i];
2143 if (Constant *constant = dyn_cast<Constant>(user))
2145 // synthesize a new non-constant equivalent of the constant
2147 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2149 switch (constant_expr->getOpcode())
2153 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
2155 case Instruction::BitCast:
2157 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2159 // OperandList[0] is value
2161 if (constant_expr->getOperand(0) != old_constant)
2162 return constant_expr;
2164 return new BitCastInst(value_maker.GetValue(function),
2165 constant_expr->getType(),
2167 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2170 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
2174 case Instruction::GetElementPtr:
2176 // GetElementPtrConstantExpr
2177 // OperandList[0] is base
2178 // OperandList[1]... are indices
2180 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2181 Value *ptr = constant_expr->getOperand(0);
2183 if (ptr == old_constant)
2184 ptr = value_maker.GetValue(function);
2186 std::vector<Value*> index_vector;
2188 unsigned operand_index;
2189 unsigned num_operands = constant_expr->getNumOperands();
2191 for (operand_index = 1;
2192 operand_index < num_operands;
2195 Value *operand = constant_expr->getOperand(operand_index);
2197 if (operand == old_constant)
2198 operand = value_maker.GetValue(function);
2200 index_vector.push_back(operand);
2203 ArrayRef <Value*> indices(index_vector);
2205 return GetElementPtrInst::Create(ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2208 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
2217 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
2223 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
2225 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
2230 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
2236 if (!isa<GlobalValue>(old_constant))
2238 old_constant->destroyConstant();
2245 IRForTarget::ReplaceVariables (Function &llvm_function)
2247 if (!m_resolve_vars)
2250 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2252 m_decl_map->DoStructLayout();
2255 log->Printf("Element arrangement:");
2257 uint32_t num_elements;
2258 uint32_t element_index;
2261 lldb::offset_t alignment;
2263 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2266 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
2268 if (iter == llvm_function.getArgumentList().end())
2271 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
2276 Argument *argument = iter;
2278 if (argument->getName().equals("this"))
2282 if (iter == llvm_function.getArgumentList().end())
2285 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
2292 else if (argument->getName().equals("self"))
2296 if (iter == llvm_function.getArgumentList().end())
2299 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
2304 if (!iter->getName().equals("_cmd"))
2307 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
2314 if (iter == llvm_function.getArgumentList().end())
2317 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
2325 if (!argument->getName().equals("$__lldb_arg"))
2328 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
2334 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
2336 BasicBlock &entry_block(llvm_function.getEntryBlock());
2337 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
2339 if (!FirstEntryInstruction)
2342 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
2347 LLVMContext &context(m_module->getContext());
2348 IntegerType *offset_type(Type::getInt32Ty(context));
2353 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
2358 for (element_index = 0; element_index < num_elements; ++element_index)
2360 const clang::NamedDecl *decl = NULL;
2361 Value *value = NULL;
2362 lldb::offset_t offset;
2363 lldb_private::ConstString name;
2365 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
2368 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
2374 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
2376 decl->getNameAsString().c_str(),
2382 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
2384 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
2385 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2386 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2387 // entry in order to produce the static variable that the AST thinks it is accessing.
2389 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
2391 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
2392 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument,
2397 if (name == m_result_name && !m_result_is_pointer)
2399 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
2400 value->getType()->getPointerTo(),
2404 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
2410 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
2416 if (Constant *constant = dyn_cast<Constant>(value))
2418 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
2420 else if (Instruction *instruction = dyn_cast<Instruction>(value))
2422 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
2427 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
2431 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2432 var->eraseFromParent();
2437 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
2443 IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
2445 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
2447 llvm::Constant *offset_array[1];
2449 offset_array[0] = offset_int;
2451 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
2453 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
2454 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
2456 return reloc_getbitcast;
2460 IRForTarget::CompleteDataAllocation ()
2462 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2464 if (!m_data_allocator.GetStream().GetSize())
2467 lldb::addr_t allocation = m_data_allocator.Allocate();
2472 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2474 log->Printf("Failed to allocate static data");
2477 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
2480 Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
2481 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
2483 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
2485 m_reloc_placeholder->eraseFromParent();
2491 IRForTarget::StripAllGVs (Module &llvm_module)
2493 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2494 std::vector<GlobalVariable *> global_vars;
2495 std::set<GlobalVariable *>erased_vars;
2503 for (GlobalVariable &global_var : llvm_module.globals())
2505 global_var.removeDeadConstantUsers();
2507 if (global_var.use_empty())
2510 log->Printf("Did remove %s",
2511 PrintValue(&global_var).c_str());
2512 global_var.eraseFromParent();
2519 for (GlobalVariable &global_var : llvm_module.globals())
2521 GlobalValue::user_iterator ui = global_var.user_begin();
2524 log->Printf("Couldn't remove %s because of %s",
2525 PrintValue(&global_var).c_str(),
2526 PrintValue(*ui).c_str());
2533 IRForTarget::runOnModule (Module &llvm_module)
2535 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2537 m_module = &llvm_module;
2538 m_target_data.reset(new DataLayout(m_module));
2539 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
2544 raw_string_ostream oss(s);
2546 m_module->print(oss, NULL);
2550 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2553 Function* main_function = m_module->getFunction(StringRef(m_func_name.c_str()));
2558 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
2561 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
2566 if (!FixFunctionLinkage (*main_function))
2569 log->Printf("Couldn't fix the linkage for the function");
2574 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
2576 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
2578 false /* IsConstant */,
2579 GlobalVariable::InternalLinkage,
2580 Constant::getNullValue(int8_ty),
2581 "reloc_placeholder",
2582 NULL /* InsertBefore */,
2583 GlobalVariable::NotThreadLocal /* ThreadLocal */,
2584 0 /* AddressSpace */);
2586 ////////////////////////////////////////////////////////////
2587 // Replace $__lldb_expr_result with a persistent variable
2590 if (!CreateResultVariable(*main_function))
2593 log->Printf("CreateResultVariable() failed");
2595 // CreateResultVariable() reports its own errors, so we don't do so here
2600 if (log && log->GetVerbose())
2603 raw_string_ostream oss(s);
2605 m_module->print(oss, NULL);
2609 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2612 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2616 llvm::Function *function = fi;
2618 if (function->begin() == function->end())
2621 Function::iterator bbi;
2623 for (bbi = function->begin();
2624 bbi != function->end();
2627 if (!RemoveGuards(*bbi))
2630 log->Printf("RemoveGuards() failed");
2632 // RemoveGuards() reports its own errors, so we don't do so here
2637 if (!RewritePersistentAllocs(*bbi))
2640 log->Printf("RewritePersistentAllocs() failed");
2642 // RewritePersistentAllocs() reports its own errors, so we don't do so here
2647 if (!RemoveCXAAtExit(*bbi))
2650 log->Printf("RemoveCXAAtExit() failed");
2652 // RemoveCXAAtExit() reports its own errors, so we don't do so here
2659 ///////////////////////////////////////////////////////////////////////////////
2660 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2663 if (!RewriteObjCConstStrings())
2666 log->Printf("RewriteObjCConstStrings() failed");
2668 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
2673 ///////////////////////////////
2674 // Resolve function pointers
2677 if (!ResolveFunctionPointers(llvm_module))
2680 log->Printf("ResolveFunctionPointers() failed");
2682 // ResolveFunctionPointers() reports its own errors, so we don't do so here
2687 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2691 llvm::Function *function = fi;
2693 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2697 if (!RewriteObjCSelectors(*bbi))
2700 log->Printf("RewriteObjCSelectors() failed");
2702 // RewriteObjCSelectors() reports its own errors, so we don't do so here
2709 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2713 llvm::Function *function = fi;
2715 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2719 if (!ResolveCalls(*bbi))
2722 log->Printf("ResolveCalls() failed");
2724 // ResolveCalls() reports its own errors, so we don't do so here
2729 if (!ReplaceStaticLiterals(*bbi))
2732 log->Printf("ReplaceStaticLiterals() failed");
2739 ////////////////////////////////////////////////////////////////////////
2740 // Run function-level passes that only make sense on the main function
2743 if (!ResolveExternals(*main_function))
2746 log->Printf("ResolveExternals() failed");
2748 // ResolveExternals() reports its own errors, so we don't do so here
2753 if (!ReplaceVariables(*main_function))
2756 log->Printf("ReplaceVariables() failed");
2758 // ReplaceVariables() reports its own errors, so we don't do so here
2763 if (!ReplaceStrings())
2766 log->Printf("ReplaceStrings() failed");
2771 if (!CompleteDataAllocation())
2774 log->Printf("CompleteDataAllocation() failed");
2779 if (!StripAllGVs(llvm_module))
2782 log->Printf("StripAllGVs() failed");
2785 if (log && log->GetVerbose())
2788 raw_string_ostream oss(s);
2790 m_module->print(oss, NULL);
2794 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
2801 IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
2806 IRForTarget::getPotentialPassManagerType() const
2808 return PMT_ModulePassManager;