1 //===-- IRForTarget.h ---------------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef liblldb_IRForTarget_h_
12 #define liblldb_IRForTarget_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 "lldb/lldb-public.h"
20 #include "llvm/Pass.h"
42 namespace lldb_private {
43 class ClangExpressionDeclMap;
44 class IRExecutionUnit;
48 //----------------------------------------------------------------------
49 /// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
50 /// @brief Transforms the IR for a function to run in the target
52 /// Once an expression has been parsed and converted to IR, it can run
53 /// in two contexts: interpreted by LLDB as a DWARF location expression,
54 /// or compiled by the JIT and inserted into the target process for
57 /// IRForTarget makes the second possible, by applying a series of
58 /// transformations to the IR which make it relocatable. These
59 /// transformations are discussed in more detail next to their relevant
61 //----------------------------------------------------------------------
62 class IRForTarget : public llvm::ModulePass {
64 enum class LookupResult { Success, Fail, Ignore };
66 //------------------------------------------------------------------
69 /// @param[in] decl_map
70 /// The list of externally-referenced variables for the expression,
71 /// for use in looking up globals and allocating the argument
72 /// struct. See the documentation for ClangExpressionDeclMap.
74 /// @param[in] resolve_vars
75 /// True if the external variable references (including persistent
76 /// variables) should be resolved. If not, only external functions
79 /// @param[in] execution_policy
80 /// Determines whether an IR interpreter can be used to statically
81 /// evaluate the expression.
83 /// @param[in] const_result
84 /// This variable is populated with the statically-computed result
85 /// of the function, if it has no side-effects and the result can
86 /// be computed statically.
88 /// @param[in] execution_unit
89 /// The holder for raw data associated with the expression.
91 /// @param[in] error_stream
92 /// If non-NULL, a stream on which errors can be printed.
94 /// @param[in] func_name
95 /// The name of the function to prepare for execution in the target.
96 //------------------------------------------------------------------
97 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars,
98 lldb_private::IRExecutionUnit &execution_unit,
99 lldb_private::Stream &error_stream,
100 const char *func_name = "$__lldb_expr");
102 //------------------------------------------------------------------
104 //------------------------------------------------------------------
105 ~IRForTarget() override;
107 //------------------------------------------------------------------
108 /// Run this IR transformer on a single module
110 /// Implementation of the llvm::ModulePass::runOnModule() function.
112 /// @param[in] llvm_module
113 /// The module to run on. This module is searched for the function
114 /// $__lldb_expr, and that function is passed to the passes one by
117 /// @param[in] interpreter_error
118 /// An error. If the expression fails to be interpreted, this error
119 /// is set to a reason why.
122 /// True on success; false otherwise
123 //------------------------------------------------------------------
124 bool runOnModule(llvm::Module &llvm_module) override;
126 //------------------------------------------------------------------
129 /// Implementation of the llvm::ModulePass::assignPassManager()
131 //------------------------------------------------------------------
132 void assignPassManager(llvm::PMStack &pass_mgr_stack,
133 llvm::PassManagerType pass_mgr_type =
134 llvm::PMT_ModulePassManager) override;
136 //------------------------------------------------------------------
137 /// Returns PMT_ModulePassManager
139 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
141 //------------------------------------------------------------------
142 llvm::PassManagerType getPotentialPassManagerType() const override;
145 //------------------------------------------------------------------
146 /// Ensures that the current function's linkage is set to external.
147 /// Otherwise the JIT may not return an address for it.
149 /// @param[in] llvm_function
150 /// The function whose linkage is to be fixed.
153 /// True on success; false otherwise.
154 //------------------------------------------------------------------
155 bool 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 //------------------------------------------------------------------
174 bool HasSideEffects(llvm::Function &llvm_function);
176 //------------------------------------------------------------------
177 /// A function-level pass to check whether the function has side
179 //------------------------------------------------------------------
181 //------------------------------------------------------------------
182 /// Get the address of a function, and a location to put the complete
183 /// Value of the function if one is available.
185 /// @param[in] function
186 /// The function to find the location of.
189 /// The location of the function in the target.
192 /// The resolved name of the function (matters for intrinsics).
194 /// @param[out] value_ptr
195 /// A variable to put the function's completed Value* in, or NULL
196 /// if the Value* shouldn't be stored anywhere.
200 //------------------------------------------------------------------
201 LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
202 lldb_private::ConstString &name,
203 llvm::Constant **&value_ptr);
205 //------------------------------------------------------------------
206 /// A function-level pass to take the generated global value
207 /// $__lldb_expr_result and make it into a persistent variable.
208 /// Also see ASTResultSynthesizer.
209 //------------------------------------------------------------------
211 //------------------------------------------------------------------
212 /// Find the NamedDecl corresponding to a Value. This interface is
213 /// exposed for the IR interpreter.
215 /// @param[in] module
216 /// The module containing metadata to search
218 /// @param[in] global
219 /// The global entity to search for
222 /// The corresponding variable declaration
223 //------------------------------------------------------------------
225 static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
226 llvm::Module *module);
229 clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
231 //------------------------------------------------------------------
232 /// Set the constant result variable m_const_result to the provided
233 /// constant, assuming it can be evaluated. The result variable
234 /// will be reset to NULL later if the expression has side effects.
236 /// @param[in] initializer
237 /// The constant initializer for the variable.
240 /// The name of the result variable.
243 /// The Clang type of the result variable.
244 //------------------------------------------------------------------
245 void MaybeSetConstantResult(llvm::Constant *initializer,
246 const lldb_private::ConstString &name,
247 lldb_private::TypeFromParser type);
249 //------------------------------------------------------------------
250 /// If the IR represents a cast of a variable, set m_const_result
251 /// to the result of the cast. The result variable will be reset to
252 /// NULL latger if the expression has side effects.
255 /// The Clang type of the result variable.
256 //------------------------------------------------------------------
257 void MaybeSetCastResult(lldb_private::TypeFromParser type);
259 //------------------------------------------------------------------
260 /// The top-level pass implementation
262 /// @param[in] llvm_function
263 /// The function currently being processed.
266 /// True on success; false otherwise
267 //------------------------------------------------------------------
268 bool CreateResultVariable(llvm::Function &llvm_function);
270 //------------------------------------------------------------------
271 /// A module-level pass to find Objective-C constant strings and
272 /// transform them to calls to CFStringCreateWithBytes.
273 //------------------------------------------------------------------
275 //------------------------------------------------------------------
276 /// Rewrite a single Objective-C constant string.
279 /// The constant NSString to be transformed
282 /// The constant C string inside the NSString. This will be
283 /// passed as the bytes argument to CFStringCreateWithBytes.
286 /// True on success; false otherwise
287 //------------------------------------------------------------------
288 bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
289 llvm::GlobalVariable *CStr);
291 //------------------------------------------------------------------
292 /// The top-level pass implementation
295 /// True on success; false otherwise
296 //------------------------------------------------------------------
297 bool RewriteObjCConstStrings();
299 //------------------------------------------------------------------
300 /// A basic block-level pass to find all Objective-C method calls and
301 /// rewrite them to use sel_registerName instead of statically allocated
302 /// selectors. The reason is that the selectors are created on the
303 /// assumption that the Objective-C runtime will scan the appropriate
304 /// section and prepare them. This doesn't happen when code is copied
305 /// into the target, though, and there's no easy way to induce the
306 /// runtime to scan them. So instead we get our selectors from
307 /// sel_registerName.
308 //------------------------------------------------------------------
310 //------------------------------------------------------------------
311 /// Replace a single selector reference
313 /// @param[in] selector_load
314 /// The load of the statically-allocated selector.
317 /// True on success; false otherwise
318 //------------------------------------------------------------------
319 bool RewriteObjCSelector(llvm::Instruction *selector_load);
321 //------------------------------------------------------------------
322 /// The top-level pass implementation
324 /// @param[in] basic_block
325 /// The basic block currently being processed.
328 /// True on success; false otherwise
329 //------------------------------------------------------------------
330 bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
332 //------------------------------------------------------------------
333 /// A basic block-level pass to find all Objective-C class references that
334 /// use the old-style Objective-C runtime and rewrite them to use
335 /// class_getClass instead of statically allocated class references.
336 //------------------------------------------------------------------
338 //------------------------------------------------------------------
339 /// Replace a single old-style class reference
341 /// @param[in] selector_load
342 /// The load of the statically-allocated selector.
345 /// True on success; false otherwise
346 //------------------------------------------------------------------
347 bool RewriteObjCClassReference(llvm::Instruction *class_load);
349 //------------------------------------------------------------------
350 /// The top-level pass implementation
352 /// @param[in] basic_block
353 /// The basic block currently being processed.
356 /// True on success; false otherwise
357 //------------------------------------------------------------------
358 bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block);
360 //------------------------------------------------------------------
361 /// A basic block-level pass to find all newly-declared persistent
362 /// variables and register them with the ClangExprDeclMap. This
363 /// allows them to be materialized and dematerialized like normal
364 /// external variables. Before transformation, these persistent
365 /// variables look like normal locals, so they have an allocation.
366 /// This pass excises these allocations and makes references look
367 /// like external references where they will be resolved -- like all
368 /// other external references -- by ResolveExternals().
369 //------------------------------------------------------------------
371 //------------------------------------------------------------------
372 /// Handle a single allocation of a persistent variable
374 /// @param[in] persistent_alloc
375 /// The allocation of the persistent variable.
378 /// True on success; false otherwise
379 //------------------------------------------------------------------
380 bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
382 //------------------------------------------------------------------
383 /// The top-level pass implementation
385 /// @param[in] basic_block
386 /// The basic block currently being processed.
387 //------------------------------------------------------------------
388 bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
390 //------------------------------------------------------------------
391 /// A function-level pass to find all external variables and functions
392 /// used in the IR. Each found external variable is added to the
393 /// struct, and each external function is resolved in place, its call
394 /// replaced with a call to a function pointer whose value is the
395 /// address of the function in the target process.
396 //------------------------------------------------------------------
398 //------------------------------------------------------------------
399 /// Write an initializer to a memory array of assumed sufficient
403 /// A pointer to the data to write to.
405 /// @param[in] initializer
406 /// The initializer itself.
409 /// True on success; false otherwise
410 //------------------------------------------------------------------
411 bool MaterializeInitializer(uint8_t *data, llvm::Constant *initializer);
413 //------------------------------------------------------------------
414 /// Move an internal variable into the static allocation section.
416 /// @param[in] global_variable
420 /// True on success; false otherwise
421 //------------------------------------------------------------------
422 bool MaterializeInternalVariable(llvm::GlobalVariable *global_variable);
424 //------------------------------------------------------------------
425 /// Handle a single externally-defined variable
431 /// True on success; false otherwise
432 //------------------------------------------------------------------
433 bool MaybeHandleVariable(llvm::Value *value);
435 //------------------------------------------------------------------
436 /// Handle a single externally-defined symbol
438 /// @param[in] symbol
442 /// True on success; false otherwise
443 //------------------------------------------------------------------
444 bool HandleSymbol(llvm::Value *symbol);
446 //------------------------------------------------------------------
447 /// Handle a single externally-defined Objective-C class
449 /// @param[in] classlist_reference
450 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
451 /// where n (if present) is an index.
454 /// True on success; false otherwise
455 //------------------------------------------------------------------
456 bool HandleObjCClass(llvm::Value *classlist_reference);
458 //------------------------------------------------------------------
459 /// Handle all the arguments to a function call
462 /// The call instruction.
465 /// True on success; false otherwise
466 //------------------------------------------------------------------
467 bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
469 //------------------------------------------------------------------
470 /// Resolve variable references in calls to external functions
472 /// @param[in] basic_block
473 /// The basic block currently being processed.
476 /// True on success; false otherwise
477 //------------------------------------------------------------------
478 bool ResolveCalls(llvm::BasicBlock &basic_block);
480 //------------------------------------------------------------------
481 /// Remove calls to __cxa_atexit, which should never be generated by
484 /// @param[in] call_inst
485 /// The call instruction.
488 /// True if the scan was successful; false if some operation
490 //------------------------------------------------------------------
491 bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
493 //------------------------------------------------------------------
494 /// The top-level pass implementation
496 /// @param[in] basic_block
497 /// The function currently being processed.
500 /// True on success; false otherwise
501 //------------------------------------------------------------------
502 bool ResolveExternals(llvm::Function &llvm_function);
504 //------------------------------------------------------------------
505 /// A basic block-level pass to excise guard variables from the code.
506 /// The result for the function is passed through Clang as a static
507 /// variable. Static variables normally have guard variables to
508 /// ensure that they are only initialized once.
509 //------------------------------------------------------------------
511 //------------------------------------------------------------------
512 /// Rewrite a load to a guard variable to return constant 0.
514 /// @param[in] guard_load
515 /// The load instruction to zero out.
516 //------------------------------------------------------------------
517 void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
519 //------------------------------------------------------------------
520 /// The top-level pass implementation
522 /// @param[in] basic_block
523 /// The basic block currently being processed.
526 /// True on success; false otherwise
527 //------------------------------------------------------------------
528 bool RemoveGuards(llvm::BasicBlock &basic_block);
530 //------------------------------------------------------------------
531 /// A function-level pass to make all external variable references
532 /// point at the correct offsets from the void* passed into the
533 /// function. ClangExpressionDeclMap::DoStructLayout() must be called
534 /// beforehand, so that the offsets are valid.
535 //------------------------------------------------------------------
537 //------------------------------------------------------------------
538 /// The top-level pass implementation
540 /// @param[in] llvm_function
541 /// The function currently being processed.
544 /// True on success; false otherwise
545 //------------------------------------------------------------------
546 bool ReplaceVariables(llvm::Function &llvm_function);
549 bool m_resolve_vars; ///< True if external variable references and persistent
550 ///variable references should be resolved
551 lldb_private::ConstString
552 m_func_name; ///< The name of the function to translate
553 lldb_private::ConstString
554 m_result_name; ///< The name of the result variable ($0, $1, ...)
555 lldb_private::TypeFromParser
556 m_result_type; ///< The type of the result variable.
557 llvm::Module *m_module; ///< The module being processed, or NULL if that has
558 ///not been determined yet.
559 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the
560 ///module being processed, or
561 ///NULL if there is no
563 lldb_private::ClangExpressionDeclMap
564 *m_decl_map; ///< The DeclMap containing the Decls
565 llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function
566 ///CFStringCreateWithBytes, cast to
568 /// appropriate function pointer type
569 llvm::Constant *m_sel_registerName; ///< The address of the function
570 ///sel_registerName, cast to the
572 /// function pointer type
573 llvm::Constant *m_objc_getClass; ///< The address of the function
574 ///objc_getClass, cast to the
576 /// function pointer type
578 *m_intptr_ty; ///< The type of an integer large enough to hold a pointer.
580 &m_error_stream; ///< The stream on which errors should be printed
581 lldb_private::IRExecutionUnit &
582 m_execution_unit; ///< The execution unit containing the IR being created.
584 llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that
585 ///writes to the result variable. If
586 /// m_has_side_effects is true, this is NULL.
587 bool m_result_is_pointer; ///< True if the function's result in the AST is a
588 ///pointer (see comments in
589 /// ASTResultSynthesizer::SynthesizeBodyResult)
591 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be
592 ///replaced by a pointer to the
594 /// location of the static allocation.
596 //------------------------------------------------------------------
597 /// UnfoldConstant operates on a constant [Old] which has just been
598 /// replaced with a value [New]. We assume that new_value has
599 /// been properly placed early in the function, in front of the
600 /// first instruction in the entry basic block
601 /// [FirstEntryInstruction].
603 /// UnfoldConstant reads through the uses of Old and replaces Old
604 /// in those uses with New. Where those uses are constants, the
605 /// function generates new instructions to compute the result of the
606 /// new, non-constant expression and places them before
607 /// FirstEntryInstruction. These instructions replace the constant
608 /// uses, so UnfoldConstant calls itself recursively for those.
610 /// @param[in] llvm_function
611 /// The function currently being processed.
614 /// True on success; false otherwise
615 //------------------------------------------------------------------
617 class FunctionValueCache {
619 typedef std::function<llvm::Value *(llvm::Function *)> Maker;
621 FunctionValueCache(Maker const &maker);
622 ~FunctionValueCache();
623 llvm::Value *GetValue(llvm::Function *function);
627 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
628 FunctionValueMap m_values;
631 FunctionValueCache m_entry_instruction_finder;
633 static bool UnfoldConstant(llvm::Constant *old_constant,
634 llvm::Function *llvm_function,
635 FunctionValueCache &value_maker,
636 FunctionValueCache &entry_instruction_finder,
637 lldb_private::Stream &error_stream);
639 //------------------------------------------------------------------
640 /// Construct a reference to m_reloc_placeholder with a given type
641 /// and offset. This typically happens after inserting data into
642 /// m_data_allocator.
645 /// The type of the value being loaded.
647 /// @param[in] offset
648 /// The offset of the value from the base of m_data_allocator.
651 /// The Constant for the reference, usually a ConstantExpr.
652 //------------------------------------------------------------------
653 llvm::Constant *BuildRelocation(llvm::Type *type, uint64_t offset);
655 //------------------------------------------------------------------
656 /// Commit the allocation in m_data_allocator and use its final
657 /// location to replace m_reloc_placeholder.
659 /// @param[in] module
660 /// The module that m_data_allocator resides in
663 /// True on success; false otherwise
664 //------------------------------------------------------------------
665 bool CompleteDataAllocation();
668 #endif // liblldb_IRForTarget_h_