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"
39 namespace lldb_private {
40 class ClangExpressionDeclMap;
41 class IRExecutionUnit;
45 //----------------------------------------------------------------------
46 /// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
47 /// @brief Transforms the IR for a function to run in the target
49 /// Once an expression has been parsed and converted to IR, it can run
50 /// in two contexts: interpreted by LLDB as a DWARF location expression,
51 /// or compiled by the JIT and inserted into the target process for
54 /// IRForTarget makes the second possible, by applying a series of
55 /// transformations to the IR which make it relocatable. These
56 /// transformations are discussed in more detail next to their relevant
58 //----------------------------------------------------------------------
59 class IRForTarget : public llvm::ModulePass
62 //------------------------------------------------------------------
65 /// @param[in] decl_map
66 /// The list of externally-referenced variables for the expression,
67 /// for use in looking up globals and allocating the argument
68 /// struct. See the documentation for ClangExpressionDeclMap.
70 /// @param[in] resolve_vars
71 /// True if the external variable references (including persistent
72 /// variables) should be resolved. If not, only external functions
75 /// @param[in] execution_policy
76 /// Determines whether an IR interpreter can be used to statically
77 /// evaluate the expression.
79 /// @param[in] const_result
80 /// This variable is populated with the statically-computed result
81 /// of the function, if it has no side-effects and the result can
82 /// be computed statically.
84 /// @param[in] execution_unit
85 /// The holder for raw data associated with the expression.
87 /// @param[in] error_stream
88 /// If non-NULL, a stream on which errors can be printed.
90 /// @param[in] func_name
91 /// The name of the function to prepare for execution in the target.
92 //------------------------------------------------------------------
93 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
95 lldb_private::IRExecutionUnit &execution_unit,
96 lldb_private::Stream *error_stream,
97 const char* func_name = "$__lldb_expr");
99 //------------------------------------------------------------------
101 //------------------------------------------------------------------
102 virtual ~IRForTarget();
104 //------------------------------------------------------------------
105 /// Run this IR transformer on a single module
107 /// Implementation of the llvm::ModulePass::runOnModule() function.
109 /// @param[in] llvm_module
110 /// The module to run on. This module is searched for the function
111 /// $__lldb_expr, and that function is passed to the passes one by
114 /// @param[in] interpreter_error
115 /// An error. If the expression fails to be interpreted, this error
116 /// is set to a reason why.
119 /// True on success; false otherwise
120 //------------------------------------------------------------------
122 runOnModule (llvm::Module &llvm_module);
124 //------------------------------------------------------------------
127 /// Implementation of the llvm::ModulePass::assignPassManager()
129 //------------------------------------------------------------------
131 assignPassManager (llvm::PMStack &pass_mgr_stack,
132 llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager);
134 //------------------------------------------------------------------
135 /// Returns PMT_ModulePassManager
137 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
139 //------------------------------------------------------------------
140 virtual llvm::PassManagerType
141 getPotentialPassManagerType() const;
144 //------------------------------------------------------------------
145 /// Ensures that the current function's linkage is set to external.
146 /// Otherwise the JIT may not return an address for it.
148 /// @param[in] llvm_function
149 /// The function whose linkage is to be fixed.
152 /// True on success; false otherwise.
153 //------------------------------------------------------------------
155 FixFunctionLinkage (llvm::Function &llvm_function);
157 //------------------------------------------------------------------
158 /// A module-level pass to replace all function pointers with their
159 /// integer equivalents.
160 //------------------------------------------------------------------
162 //------------------------------------------------------------------
163 /// The top-level pass implementation
165 /// @param[in] llvm_module
166 /// The module currently being processed.
168 /// @param[in] llvm_function
169 /// The function currently being processed.
172 /// True on success; false otherwise.
173 //------------------------------------------------------------------
175 HasSideEffects (llvm::Function &llvm_function);
177 //------------------------------------------------------------------
178 /// A function-level pass to check whether the function has side
180 //------------------------------------------------------------------
182 //------------------------------------------------------------------
183 /// Get the address of a fuction, and a location to put the complete
184 /// Value of the function if one is available.
186 /// @param[in] function
187 /// The function to find the location of.
190 /// The location of the function in the target.
193 /// The resolved name of the function (matters for intrinsics).
195 /// @param[out] value_ptr
196 /// A variable to put the function's completed Value* in, or NULL
197 /// if the Value* shouldn't be stored anywhere.
201 //------------------------------------------------------------------
203 GetFunctionAddress (llvm::Function *function,
205 lldb_private::ConstString &name,
206 llvm::Constant **&value_ptr);
208 //------------------------------------------------------------------
209 /// Build a function pointer given a type and a raw pointer.
212 /// The type of the function pointer to be built.
215 /// The value of the pointer.
219 //------------------------------------------------------------------
221 BuildFunctionPointer (llvm::Type *type,
225 RegisterFunctionMetadata (llvm::LLVMContext &context,
226 llvm::Value *function_ptr,
229 //------------------------------------------------------------------
230 /// The top-level pass implementation
232 /// @param[in] llvm_function
233 /// The function currently being processed.
236 /// True if the function has side effects (or if this cannot
237 /// be determined); false otherwise.
238 //------------------------------------------------------------------
240 ResolveFunctionPointers (llvm::Module &llvm_module);
242 //------------------------------------------------------------------
243 /// A function-level pass to take the generated global value
244 /// $__lldb_expr_result and make it into a persistent variable.
245 /// Also see ASTResultSynthesizer.
246 //------------------------------------------------------------------
248 //------------------------------------------------------------------
249 /// Find the NamedDecl corresponding to a Value. This interface is
250 /// exposed for the IR interpreter.
252 /// @param[in] module
253 /// The module containing metadata to search
255 /// @param[in] global
256 /// The global entity to search for
259 /// The corresponding variable declaration
260 //------------------------------------------------------------------
262 static clang::NamedDecl *
263 DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module);
266 DeclForGlobal (llvm::GlobalValue *global);
268 //------------------------------------------------------------------
269 /// Set the constant result variable m_const_result to the provided
270 /// constant, assuming it can be evaluated. The result variable
271 /// will be reset to NULL later if the expression has side effects.
273 /// @param[in] initializer
274 /// The constant initializer for the variable.
277 /// The name of the result variable.
280 /// The Clang type of the result variable.
281 //------------------------------------------------------------------
283 MaybeSetConstantResult (llvm::Constant *initializer,
284 const lldb_private::ConstString &name,
285 lldb_private::TypeFromParser type);
287 //------------------------------------------------------------------
288 /// If the IR represents a cast of a variable, set m_const_result
289 /// to the result of the cast. The result variable will be reset to
290 /// NULL latger if the expression has side effects.
293 /// The Clang type of the result variable.
294 //------------------------------------------------------------------
296 MaybeSetCastResult (lldb_private::TypeFromParser type);
298 //------------------------------------------------------------------
299 /// The top-level pass implementation
301 /// @param[in] llvm_function
302 /// The function currently being processed.
305 /// True on success; false otherwise
306 //------------------------------------------------------------------
308 CreateResultVariable (llvm::Function &llvm_function);
310 //------------------------------------------------------------------
311 /// A module-level pass to find Objective-C constant strings and
312 /// transform them to calls to CFStringCreateWithBytes.
313 //------------------------------------------------------------------
315 //------------------------------------------------------------------
316 /// Rewrite a single Objective-C constant string.
319 /// The constant NSString to be transformed
322 /// The constant C string inside the NSString. This will be
323 /// passed as the bytes argument to CFStringCreateWithBytes.
326 /// True on success; false otherwise
327 //------------------------------------------------------------------
329 RewriteObjCConstString (llvm::GlobalVariable *NSStr,
330 llvm::GlobalVariable *CStr);
332 //------------------------------------------------------------------
333 /// The top-level pass implementation
336 /// True on success; false otherwise
337 //------------------------------------------------------------------
339 RewriteObjCConstStrings ();
341 //------------------------------------------------------------------
342 /// A basic block-level pass to find all Objective-C method calls and
343 /// rewrite them to use sel_registerName instead of statically allocated
344 /// selectors. The reason is that the selectors are created on the
345 /// assumption that the Objective-C runtime will scan the appropriate
346 /// section and prepare them. This doesn't happen when code is copied
347 /// into the target, though, and there's no easy way to induce the
348 /// runtime to scan them. So instead we get our selectors from
349 /// sel_registerName.
350 //------------------------------------------------------------------
352 //------------------------------------------------------------------
353 /// Replace a single selector reference
355 /// @param[in] selector_load
356 /// The load of the statically-allocated selector.
359 /// True on success; false otherwise
360 //------------------------------------------------------------------
362 RewriteObjCSelector (llvm::Instruction* selector_load);
364 //------------------------------------------------------------------
365 /// The top-level pass implementation
367 /// @param[in] basic_block
368 /// The basic block currently being processed.
371 /// True on success; false otherwise
372 //------------------------------------------------------------------
374 RewriteObjCSelectors (llvm::BasicBlock &basic_block);
376 //------------------------------------------------------------------
377 /// A basic block-level pass to find all newly-declared persistent
378 /// variables and register them with the ClangExprDeclMap. This
379 /// allows them to be materialized and dematerialized like normal
380 /// external variables. Before transformation, these persistent
381 /// variables look like normal locals, so they have an allocation.
382 /// This pass excises these allocations and makes references look
383 /// like external references where they will be resolved -- like all
384 /// other external references -- by ResolveExternals().
385 //------------------------------------------------------------------
387 //------------------------------------------------------------------
388 /// Handle a single allocation of a persistent variable
390 /// @param[in] persistent_alloc
391 /// The allocation of the persistent variable.
394 /// True on success; false otherwise
395 //------------------------------------------------------------------
397 RewritePersistentAlloc (llvm::Instruction *persistent_alloc);
399 //------------------------------------------------------------------
400 /// The top-level pass implementation
402 /// @param[in] basic_block
403 /// The basic block currently being processed.
404 //------------------------------------------------------------------
406 RewritePersistentAllocs (llvm::BasicBlock &basic_block);
408 //------------------------------------------------------------------
409 /// A function-level pass to find all external variables and functions
410 /// used in the IR. Each found external variable is added to the
411 /// struct, and each external function is resolved in place, its call
412 /// replaced with a call to a function pointer whose value is the
413 /// address of the function in the target process.
414 //------------------------------------------------------------------
416 //------------------------------------------------------------------
417 /// Write an initializer to a memory array of assumed sufficient
421 /// A pointer to the data to write to.
423 /// @param[in] initializer
424 /// The initializer itself.
427 /// True on success; false otherwise
428 //------------------------------------------------------------------
430 MaterializeInitializer (uint8_t *data, llvm::Constant *initializer);
432 //------------------------------------------------------------------
433 /// Move an internal variable into the static allocation section.
435 /// @param[in] global_variable
439 /// True on success; false otherwise
440 //------------------------------------------------------------------
442 MaterializeInternalVariable (llvm::GlobalVariable *global_variable);
444 //------------------------------------------------------------------
445 /// Handle a single externally-defined variable
451 /// True on success; false otherwise
452 //------------------------------------------------------------------
454 MaybeHandleVariable (llvm::Value *value);
456 //------------------------------------------------------------------
457 /// Handle a single externally-defined symbol
459 /// @param[in] symbol
463 /// True on success; false otherwise
464 //------------------------------------------------------------------
466 HandleSymbol (llvm::Value *symbol);
468 //------------------------------------------------------------------
469 /// Handle a single externally-defined Objective-C class
471 /// @param[in] classlist_reference
472 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
473 /// where n (if present) is an index.
476 /// True on success; false otherwise
477 //------------------------------------------------------------------
479 HandleObjCClass(llvm::Value *classlist_reference);
481 //------------------------------------------------------------------
482 /// Handle all the arguments to a function call
485 /// The call instruction.
488 /// True on success; false otherwise
489 //------------------------------------------------------------------
491 MaybeHandleCallArguments (llvm::CallInst *call_inst);
493 //------------------------------------------------------------------
494 /// Resolve variable references in calls to external functions
496 /// @param[in] basic_block
497 /// The basic block currently being processed.
500 /// True on success; false otherwise
501 //------------------------------------------------------------------
503 ResolveCalls (llvm::BasicBlock &basic_block);
505 //------------------------------------------------------------------
506 /// Remove calls to __cxa_atexit, which should never be generated by
509 /// @param[in] call_inst
510 /// The call instruction.
513 /// True if the scan was successful; false if some operation
515 //------------------------------------------------------------------
517 RemoveCXAAtExit (llvm::BasicBlock &basic_block);
519 //------------------------------------------------------------------
520 /// The top-level pass implementation
522 /// @param[in] basic_block
523 /// The function currently being processed.
526 /// True on success; false otherwise
527 //------------------------------------------------------------------
529 ResolveExternals (llvm::Function &llvm_function);
531 //------------------------------------------------------------------
532 /// A basic block-level pass to excise guard variables from the code.
533 /// The result for the function is passed through Clang as a static
534 /// variable. Static variables normally have guard variables to
535 /// ensure that they are only initialized once.
536 //------------------------------------------------------------------
538 //------------------------------------------------------------------
539 /// Rewrite a load to a guard variable to return constant 0.
541 /// @param[in] guard_load
542 /// The load instruction to zero out.
543 //------------------------------------------------------------------
545 TurnGuardLoadIntoZero(llvm::Instruction* guard_load);
547 //------------------------------------------------------------------
548 /// The top-level pass implementation
550 /// @param[in] basic_block
551 /// The basic block currently being processed.
554 /// True on success; false otherwise
555 //------------------------------------------------------------------
557 RemoveGuards (llvm::BasicBlock &basic_block);
559 //------------------------------------------------------------------
560 /// A module-level pass to allocate all string literals in a separate
561 /// allocation and redirect references to them.
562 //------------------------------------------------------------------
564 //------------------------------------------------------------------
565 /// The top-level pass implementation
568 /// True on success; false otherwise
569 //------------------------------------------------------------------
573 //------------------------------------------------------------------
574 /// A basick block-level pass to find all literals that will be
575 /// allocated as statics by the JIT (in contrast to the Strings,
576 /// which already are statics) and synthesize loads for them.
577 //------------------------------------------------------------------
579 //------------------------------------------------------------------
580 /// The top-level pass implementation
582 /// @param[in] basic_block
583 /// The basic block currently being processed.
586 /// True on success; false otherwise
587 //------------------------------------------------------------------
589 ReplaceStaticLiterals (llvm::BasicBlock &basic_block);
591 //------------------------------------------------------------------
592 /// A function-level pass to make all external variable references
593 /// point at the correct offsets from the void* passed into the
594 /// function. ClangExpressionDeclMap::DoStructLayout() must be called
595 /// beforehand, so that the offsets are valid.
596 //------------------------------------------------------------------
598 //------------------------------------------------------------------
599 /// The top-level pass implementation
601 /// @param[in] llvm_function
602 /// The function currently being processed.
605 /// True on success; false otherwise
606 //------------------------------------------------------------------
608 ReplaceVariables (llvm::Function &llvm_function);
610 //------------------------------------------------------------------
611 /// A module-level pass to remove all global variables from the
612 /// module since it no longer should export or import any symbols.
613 //------------------------------------------------------------------
615 //------------------------------------------------------------------
616 /// The top-level pass implementation
618 /// @param[in] llvm_module
619 /// The module currently being processed.
622 /// True on success; false otherwise
623 //------------------------------------------------------------------
625 StripAllGVs (llvm::Module &llvm_module);
627 class StaticDataAllocator {
629 StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit);
630 lldb_private::StreamString &GetStream()
632 return m_stream_string;
634 lldb::addr_t Allocate();
636 lldb_private::IRExecutionUnit &m_execution_unit;
637 lldb_private::StreamString m_stream_string;
638 lldb::addr_t m_allocation;
642 bool m_resolve_vars; ///< True if external variable references and persistent variable references should be resolved
643 std::string m_func_name; ///< The name of the function to translate
644 lldb_private::ConstString m_result_name; ///< The name of the result variable ($0, $1, ...)
645 lldb_private::TypeFromParser m_result_type; ///< The type of the result variable.
646 llvm::Module *m_module; ///< The module being processed, or NULL if that has not been determined yet.
647 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the module being processed, or NULL if there is no module.
648 lldb_private::ClangExpressionDeclMap *m_decl_map; ///< The DeclMap containing the Decls
649 StaticDataAllocator m_data_allocator; ///< The allocator to use for constant strings
650 llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
651 llvm::Constant *m_sel_registerName; ///< The address of the function sel_registerName, cast to the appropriate function pointer type
652 lldb_private::Stream *m_error_stream; ///< If non-NULL, the stream on which errors should be printed
654 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.
655 bool m_result_is_pointer; ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
657 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be replaced by a pointer to the final location of the static allocation.
659 //------------------------------------------------------------------
660 /// UnfoldConstant operates on a constant [Old] which has just been
661 /// replaced with a value [New]. We assume that new_value has
662 /// been properly placed early in the function, in front of the
663 /// first instruction in the entry basic block
664 /// [FirstEntryInstruction].
666 /// UnfoldConstant reads through the uses of Old and replaces Old
667 /// in those uses with New. Where those uses are constants, the
668 /// function generates new instructions to compute the result of the
669 /// new, non-constant expression and places them before
670 /// FirstEntryInstruction. These instructions replace the constant
671 /// uses, so UnfoldConstant calls itself recursively for those.
673 /// @param[in] llvm_function
674 /// The function currently being processed.
677 /// True on success; false otherwise
678 //------------------------------------------------------------------
680 class FunctionValueCache {
682 typedef std::function <llvm::Value *(llvm::Function *)> Maker;
684 FunctionValueCache (Maker const &maker);
685 ~FunctionValueCache ();
686 llvm::Value *GetValue (llvm::Function *function);
689 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
690 FunctionValueMap m_values;
693 FunctionValueCache m_entry_instruction_finder;
696 UnfoldConstant (llvm::Constant *old_constant,
697 FunctionValueCache &value_maker,
698 FunctionValueCache &entry_instruction_finder);
700 //------------------------------------------------------------------
701 /// Construct a reference to m_reloc_placeholder with a given type
702 /// and offset. This typically happens after inserting data into
703 /// m_data_allocator.
706 /// The type of the value being loaded.
708 /// @param[in] offset
709 /// The offset of the value from the base of m_data_allocator.
712 /// The Constant for the reference, usually a ConstantExpr.
713 //------------------------------------------------------------------
715 BuildRelocation(llvm::Type *type,
718 //------------------------------------------------------------------
719 /// Commit the allocation in m_data_allocator and use its final
720 /// location to replace m_reloc_placeholder.
722 /// @param[in] module
723 /// The module that m_data_allocator resides in
726 /// True on success; false otherwise
727 //------------------------------------------------------------------
729 CompleteDataAllocation ();