1 //===-- IRForTarget.h ---------------------------------------------*- 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 #ifndef liblldb_IRForTarget_h_
11 #define liblldb_IRForTarget_h_
13 #include "lldb/lldb-public.h"
14 #include "lldb/Core/ConstString.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/StreamString.h"
18 #include "lldb/Symbol/TaggedASTType.h"
19 #include "llvm/Pass.h"
41 namespace lldb_private {
42 class ClangExpressionDeclMap;
43 class IRExecutionUnit;
47 //----------------------------------------------------------------------
48 /// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
49 /// @brief Transforms the IR for a function to run in the target
51 /// Once an expression has been parsed and converted to IR, it can run
52 /// in two contexts: interpreted by LLDB as a DWARF location expression,
53 /// or compiled by the JIT and inserted into the target process for
56 /// IRForTarget makes the second possible, by applying a series of
57 /// transformations to the IR which make it relocatable. These
58 /// transformations are discussed in more detail next to their relevant
60 //----------------------------------------------------------------------
61 class IRForTarget : public llvm::ModulePass
64 enum class LookupResult {
70 //------------------------------------------------------------------
73 /// @param[in] decl_map
74 /// The list of externally-referenced variables for the expression,
75 /// for use in looking up globals and allocating the argument
76 /// struct. See the documentation for ClangExpressionDeclMap.
78 /// @param[in] resolve_vars
79 /// True if the external variable references (including persistent
80 /// variables) should be resolved. If not, only external functions
83 /// @param[in] execution_policy
84 /// Determines whether an IR interpreter can be used to statically
85 /// evaluate the expression.
87 /// @param[in] const_result
88 /// This variable is populated with the statically-computed result
89 /// of the function, if it has no side-effects and the result can
90 /// be computed statically.
92 /// @param[in] execution_unit
93 /// The holder for raw data associated with the expression.
95 /// @param[in] error_stream
96 /// If non-NULL, a stream on which errors can be printed.
98 /// @param[in] func_name
99 /// The name of the function to prepare for execution in the target.
100 //------------------------------------------------------------------
101 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
103 lldb_private::IRExecutionUnit &execution_unit,
104 lldb_private::Stream *error_stream,
105 const char* func_name = "$__lldb_expr");
107 //------------------------------------------------------------------
109 //------------------------------------------------------------------
110 virtual ~IRForTarget();
112 //------------------------------------------------------------------
113 /// Run this IR transformer on a single module
115 /// Implementation of the llvm::ModulePass::runOnModule() function.
117 /// @param[in] llvm_module
118 /// The module to run on. This module is searched for the function
119 /// $__lldb_expr, and that function is passed to the passes one by
122 /// @param[in] interpreter_error
123 /// An error. If the expression fails to be interpreted, this error
124 /// is set to a reason why.
127 /// True on success; false otherwise
128 //------------------------------------------------------------------
130 runOnModule (llvm::Module &llvm_module);
132 //------------------------------------------------------------------
135 /// Implementation of the llvm::ModulePass::assignPassManager()
137 //------------------------------------------------------------------
139 assignPassManager (llvm::PMStack &pass_mgr_stack,
140 llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager);
142 //------------------------------------------------------------------
143 /// Returns PMT_ModulePassManager
145 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
147 //------------------------------------------------------------------
148 virtual llvm::PassManagerType
149 getPotentialPassManagerType() const;
152 //------------------------------------------------------------------
153 /// Ensures that the current function's linkage is set to external.
154 /// Otherwise the JIT may not return an address for it.
156 /// @param[in] llvm_function
157 /// The function whose linkage is to be fixed.
160 /// True on success; false otherwise.
161 //------------------------------------------------------------------
163 FixFunctionLinkage (llvm::Function &llvm_function);
165 //------------------------------------------------------------------
166 /// A module-level pass to replace all function pointers with their
167 /// integer equivalents.
168 //------------------------------------------------------------------
170 //------------------------------------------------------------------
171 /// The top-level pass implementation
173 /// @param[in] llvm_module
174 /// The module currently being processed.
176 /// @param[in] llvm_function
177 /// The function currently being processed.
180 /// True on success; false otherwise.
181 //------------------------------------------------------------------
183 HasSideEffects (llvm::Function &llvm_function);
185 //------------------------------------------------------------------
186 /// A function-level pass to check whether the function has side
188 //------------------------------------------------------------------
190 //------------------------------------------------------------------
191 /// Get the address of a function, and a location to put the complete
192 /// Value of the function if one is available.
194 /// @param[in] function
195 /// The function to find the location of.
198 /// The location of the function in the target.
201 /// The resolved name of the function (matters for intrinsics).
203 /// @param[out] value_ptr
204 /// A variable to put the function's completed Value* in, or NULL
205 /// if the Value* shouldn't be stored anywhere.
209 //------------------------------------------------------------------
211 GetFunctionAddress (llvm::Function *function,
213 lldb_private::ConstString &name,
214 llvm::Constant **&value_ptr);
216 //------------------------------------------------------------------
217 /// Build a function pointer given a type and a raw pointer.
220 /// The type of the function pointer to be built.
223 /// The value of the pointer.
227 //------------------------------------------------------------------
229 BuildFunctionPointer (llvm::Type *type,
233 RegisterFunctionMetadata (llvm::LLVMContext &context,
234 llvm::Value *function_ptr,
237 //------------------------------------------------------------------
238 /// The top-level pass implementation
240 /// @param[in] llvm_function
241 /// The function currently being processed.
244 /// True if the function has side effects (or if this cannot
245 /// be determined); false otherwise.
246 //------------------------------------------------------------------
248 ResolveFunctionPointers (llvm::Module &llvm_module);
250 //------------------------------------------------------------------
251 /// A function-level pass to take the generated global value
252 /// $__lldb_expr_result and make it into a persistent variable.
253 /// Also see ASTResultSynthesizer.
254 //------------------------------------------------------------------
256 //------------------------------------------------------------------
257 /// Find the NamedDecl corresponding to a Value. This interface is
258 /// exposed for the IR interpreter.
260 /// @param[in] module
261 /// The module containing metadata to search
263 /// @param[in] global
264 /// The global entity to search for
267 /// The corresponding variable declaration
268 //------------------------------------------------------------------
270 static clang::NamedDecl *
271 DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module);
274 DeclForGlobal (llvm::GlobalValue *global);
276 //------------------------------------------------------------------
277 /// Set the constant result variable m_const_result to the provided
278 /// constant, assuming it can be evaluated. The result variable
279 /// will be reset to NULL later if the expression has side effects.
281 /// @param[in] initializer
282 /// The constant initializer for the variable.
285 /// The name of the result variable.
288 /// The Clang type of the result variable.
289 //------------------------------------------------------------------
291 MaybeSetConstantResult (llvm::Constant *initializer,
292 const lldb_private::ConstString &name,
293 lldb_private::TypeFromParser type);
295 //------------------------------------------------------------------
296 /// If the IR represents a cast of a variable, set m_const_result
297 /// to the result of the cast. The result variable will be reset to
298 /// NULL latger if the expression has side effects.
301 /// The Clang type of the result variable.
302 //------------------------------------------------------------------
304 MaybeSetCastResult (lldb_private::TypeFromParser type);
306 //------------------------------------------------------------------
307 /// The top-level pass implementation
309 /// @param[in] llvm_function
310 /// The function currently being processed.
313 /// True on success; false otherwise
314 //------------------------------------------------------------------
316 CreateResultVariable (llvm::Function &llvm_function);
318 //------------------------------------------------------------------
319 /// A module-level pass to find Objective-C constant strings and
320 /// transform them to calls to CFStringCreateWithBytes.
321 //------------------------------------------------------------------
323 //------------------------------------------------------------------
324 /// Rewrite a single Objective-C constant string.
327 /// The constant NSString to be transformed
330 /// The constant C string inside the NSString. This will be
331 /// passed as the bytes argument to CFStringCreateWithBytes.
334 /// True on success; false otherwise
335 //------------------------------------------------------------------
337 RewriteObjCConstString (llvm::GlobalVariable *NSStr,
338 llvm::GlobalVariable *CStr);
340 //------------------------------------------------------------------
341 /// The top-level pass implementation
344 /// True on success; false otherwise
345 //------------------------------------------------------------------
347 RewriteObjCConstStrings ();
349 //------------------------------------------------------------------
350 /// A basic block-level pass to find all Objective-C method calls and
351 /// rewrite them to use sel_registerName instead of statically allocated
352 /// selectors. The reason is that the selectors are created on the
353 /// assumption that the Objective-C runtime will scan the appropriate
354 /// section and prepare them. This doesn't happen when code is copied
355 /// into the target, though, and there's no easy way to induce the
356 /// runtime to scan them. So instead we get our selectors from
357 /// sel_registerName.
358 //------------------------------------------------------------------
360 //------------------------------------------------------------------
361 /// Replace a single selector reference
363 /// @param[in] selector_load
364 /// The load of the statically-allocated selector.
367 /// True on success; false otherwise
368 //------------------------------------------------------------------
370 RewriteObjCSelector (llvm::Instruction* selector_load);
372 //------------------------------------------------------------------
373 /// The top-level pass implementation
375 /// @param[in] basic_block
376 /// The basic block currently being processed.
379 /// True on success; false otherwise
380 //------------------------------------------------------------------
382 RewriteObjCSelectors (llvm::BasicBlock &basic_block);
384 //------------------------------------------------------------------
385 /// A basic block-level pass to find all newly-declared persistent
386 /// variables and register them with the ClangExprDeclMap. This
387 /// allows them to be materialized and dematerialized like normal
388 /// external variables. Before transformation, these persistent
389 /// variables look like normal locals, so they have an allocation.
390 /// This pass excises these allocations and makes references look
391 /// like external references where they will be resolved -- like all
392 /// other external references -- by ResolveExternals().
393 //------------------------------------------------------------------
395 //------------------------------------------------------------------
396 /// Handle a single allocation of a persistent variable
398 /// @param[in] persistent_alloc
399 /// The allocation of the persistent variable.
402 /// True on success; false otherwise
403 //------------------------------------------------------------------
405 RewritePersistentAlloc (llvm::Instruction *persistent_alloc);
407 //------------------------------------------------------------------
408 /// The top-level pass implementation
410 /// @param[in] basic_block
411 /// The basic block currently being processed.
412 //------------------------------------------------------------------
414 RewritePersistentAllocs (llvm::BasicBlock &basic_block);
416 //------------------------------------------------------------------
417 /// A function-level pass to find all external variables and functions
418 /// used in the IR. Each found external variable is added to the
419 /// struct, and each external function is resolved in place, its call
420 /// replaced with a call to a function pointer whose value is the
421 /// address of the function in the target process.
422 //------------------------------------------------------------------
424 //------------------------------------------------------------------
425 /// Write an initializer to a memory array of assumed sufficient
429 /// A pointer to the data to write to.
431 /// @param[in] initializer
432 /// The initializer itself.
435 /// True on success; false otherwise
436 //------------------------------------------------------------------
438 MaterializeInitializer (uint8_t *data, llvm::Constant *initializer);
440 //------------------------------------------------------------------
441 /// Move an internal variable into the static allocation section.
443 /// @param[in] global_variable
447 /// True on success; false otherwise
448 //------------------------------------------------------------------
450 MaterializeInternalVariable (llvm::GlobalVariable *global_variable);
452 //------------------------------------------------------------------
453 /// Handle a single externally-defined variable
459 /// True on success; false otherwise
460 //------------------------------------------------------------------
462 MaybeHandleVariable (llvm::Value *value);
464 //------------------------------------------------------------------
465 /// Handle a single externally-defined symbol
467 /// @param[in] symbol
471 /// True on success; false otherwise
472 //------------------------------------------------------------------
474 HandleSymbol (llvm::Value *symbol);
476 //------------------------------------------------------------------
477 /// Handle a single externally-defined Objective-C class
479 /// @param[in] classlist_reference
480 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
481 /// where n (if present) is an index.
484 /// True on success; false otherwise
485 //------------------------------------------------------------------
487 HandleObjCClass(llvm::Value *classlist_reference);
489 //------------------------------------------------------------------
490 /// Handle all the arguments to a function call
493 /// The call instruction.
496 /// True on success; false otherwise
497 //------------------------------------------------------------------
499 MaybeHandleCallArguments (llvm::CallInst *call_inst);
501 //------------------------------------------------------------------
502 /// Resolve variable references in calls to external functions
504 /// @param[in] basic_block
505 /// The basic block currently being processed.
508 /// True on success; false otherwise
509 //------------------------------------------------------------------
511 ResolveCalls (llvm::BasicBlock &basic_block);
513 //------------------------------------------------------------------
514 /// Remove calls to __cxa_atexit, which should never be generated by
517 /// @param[in] call_inst
518 /// The call instruction.
521 /// True if the scan was successful; false if some operation
523 //------------------------------------------------------------------
525 RemoveCXAAtExit (llvm::BasicBlock &basic_block);
527 //------------------------------------------------------------------
528 /// The top-level pass implementation
530 /// @param[in] basic_block
531 /// The function currently being processed.
534 /// True on success; false otherwise
535 //------------------------------------------------------------------
537 ResolveExternals (llvm::Function &llvm_function);
539 //------------------------------------------------------------------
540 /// A basic block-level pass to excise guard variables from the code.
541 /// The result for the function is passed through Clang as a static
542 /// variable. Static variables normally have guard variables to
543 /// ensure that they are only initialized once.
544 //------------------------------------------------------------------
546 //------------------------------------------------------------------
547 /// Rewrite a load to a guard variable to return constant 0.
549 /// @param[in] guard_load
550 /// The load instruction to zero out.
551 //------------------------------------------------------------------
553 TurnGuardLoadIntoZero(llvm::Instruction* guard_load);
555 //------------------------------------------------------------------
556 /// The top-level pass implementation
558 /// @param[in] basic_block
559 /// The basic block currently being processed.
562 /// True on success; false otherwise
563 //------------------------------------------------------------------
565 RemoveGuards (llvm::BasicBlock &basic_block);
567 //------------------------------------------------------------------
568 /// A module-level pass to allocate all string literals in a separate
569 /// allocation and redirect references to them.
570 //------------------------------------------------------------------
572 //------------------------------------------------------------------
573 /// The top-level pass implementation
576 /// True on success; false otherwise
577 //------------------------------------------------------------------
581 //------------------------------------------------------------------
582 /// A basic block-level pass to find all literals that will be
583 /// allocated as statics by the JIT (in contrast to the Strings,
584 /// which already are statics) and synthesize loads for them.
585 //------------------------------------------------------------------
587 //------------------------------------------------------------------
588 /// The top-level pass implementation
590 /// @param[in] basic_block
591 /// The basic block currently being processed.
594 /// True on success; false otherwise
595 //------------------------------------------------------------------
597 ReplaceStaticLiterals (llvm::BasicBlock &basic_block);
599 //------------------------------------------------------------------
600 /// A function-level pass to make all external variable references
601 /// point at the correct offsets from the void* passed into the
602 /// function. ClangExpressionDeclMap::DoStructLayout() must be called
603 /// beforehand, so that the offsets are valid.
604 //------------------------------------------------------------------
606 //------------------------------------------------------------------
607 /// The top-level pass implementation
609 /// @param[in] llvm_function
610 /// The function currently being processed.
613 /// True on success; false otherwise
614 //------------------------------------------------------------------
616 ReplaceVariables (llvm::Function &llvm_function);
618 //------------------------------------------------------------------
619 /// A module-level pass to remove all global variables from the
620 /// module since it no longer should export or import any symbols.
621 //------------------------------------------------------------------
623 //------------------------------------------------------------------
624 /// The top-level pass implementation
626 /// @param[in] llvm_module
627 /// The module currently being processed.
630 /// True on success; false otherwise
631 //------------------------------------------------------------------
633 StripAllGVs (llvm::Module &llvm_module);
635 class StaticDataAllocator {
637 StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit);
638 lldb_private::StreamString &GetStream()
640 return m_stream_string;
642 lldb::addr_t Allocate();
647 lldb_private::IRExecutionUnit &m_execution_unit;
648 lldb_private::StreamString m_stream_string;
649 lldb::addr_t m_allocation;
653 bool m_resolve_vars; ///< True if external variable references and persistent variable references should be resolved
654 std::string m_func_name; ///< The name of the function to translate
655 lldb_private::ConstString m_result_name; ///< The name of the result variable ($0, $1, ...)
656 lldb_private::TypeFromParser m_result_type; ///< The type of the result variable.
657 llvm::Module *m_module; ///< The module being processed, or NULL if that has not been determined yet.
658 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the module being processed, or NULL if there is no module.
659 lldb_private::ClangExpressionDeclMap *m_decl_map; ///< The DeclMap containing the Decls
660 StaticDataAllocator m_data_allocator; ///< The allocator to use for constant strings
661 llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
662 llvm::Constant *m_sel_registerName; ///< The address of the function sel_registerName, cast to the appropriate function pointer type
663 llvm::IntegerType *m_intptr_ty; ///< The type of an integer large enough to hold a pointer.
664 lldb_private::Stream *m_error_stream; ///< If non-NULL, the stream on which errors should be printed
666 llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that writes to the result variable. If m_has_side_effects is true, this is NULL.
667 bool m_result_is_pointer; ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
669 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be replaced by a pointer to the final location of the static allocation.
671 //------------------------------------------------------------------
672 /// UnfoldConstant operates on a constant [Old] which has just been
673 /// replaced with a value [New]. We assume that new_value has
674 /// been properly placed early in the function, in front of the
675 /// first instruction in the entry basic block
676 /// [FirstEntryInstruction].
678 /// UnfoldConstant reads through the uses of Old and replaces Old
679 /// in those uses with New. Where those uses are constants, the
680 /// function generates new instructions to compute the result of the
681 /// new, non-constant expression and places them before
682 /// FirstEntryInstruction. These instructions replace the constant
683 /// uses, so UnfoldConstant calls itself recursively for those.
685 /// @param[in] llvm_function
686 /// The function currently being processed.
689 /// True on success; false otherwise
690 //------------------------------------------------------------------
692 class FunctionValueCache {
694 typedef std::function <llvm::Value *(llvm::Function *)> Maker;
696 FunctionValueCache (Maker const &maker);
697 ~FunctionValueCache ();
698 llvm::Value *GetValue (llvm::Function *function);
701 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
702 FunctionValueMap m_values;
705 FunctionValueCache m_entry_instruction_finder;
708 UnfoldConstant (llvm::Constant *old_constant,
709 FunctionValueCache &value_maker,
710 FunctionValueCache &entry_instruction_finder);
712 //------------------------------------------------------------------
713 /// Construct a reference to m_reloc_placeholder with a given type
714 /// and offset. This typically happens after inserting data into
715 /// m_data_allocator.
718 /// The type of the value being loaded.
720 /// @param[in] offset
721 /// The offset of the value from the base of m_data_allocator.
724 /// The Constant for the reference, usually a ConstantExpr.
725 //------------------------------------------------------------------
727 BuildRelocation(llvm::Type *type,
730 //------------------------------------------------------------------
731 /// Commit the allocation in m_data_allocator and use its final
732 /// location to replace m_reloc_placeholder.
734 /// @param[in] module
735 /// The module that m_data_allocator resides in
738 /// True on success; false otherwise
739 //------------------------------------------------------------------
741 CompleteDataAllocation ();