1 //===-- IRForTarget.h ---------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_IRForTarget_h_
11 #define liblldb_IRForTarget_h_
13 #include "lldb/Core/ClangForward.h"
14 #include "lldb/Symbol/TaggedASTType.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/Status.h"
17 #include "lldb/Utility/Stream.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/lldb-public.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/Pass.h"
41 namespace lldb_private {
42 class ClangExpressionDeclMap;
43 class IRExecutionUnit;
47 /// \class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
48 /// Transforms the IR for a function to run in the target
50 /// Once an expression has been parsed and converted to IR, it can run in two
51 /// contexts: interpreted by LLDB as a DWARF location expression, or compiled
52 /// by the JIT and inserted into the target process for execution.
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 class IRForTarget : public llvm::ModulePass {
60 enum class LookupResult { Success, Fail, Ignore };
64 /// \param[in] decl_map
65 /// The list of externally-referenced variables for the expression,
66 /// for use in looking up globals and allocating the argument
67 /// struct. See the documentation for ClangExpressionDeclMap.
69 /// \param[in] resolve_vars
70 /// True if the external variable references (including persistent
71 /// variables) should be resolved. If not, only external functions
74 /// \param[in] execution_policy
75 /// Determines whether an IR interpreter can be used to statically
76 /// evaluate the expression.
78 /// \param[in] const_result
79 /// This variable is populated with the statically-computed result
80 /// of the function, if it has no side-effects and the result can
81 /// be computed statically.
83 /// \param[in] execution_unit
84 /// The holder for raw data associated with the expression.
86 /// \param[in] error_stream
87 /// If non-NULL, a stream on which errors can be printed.
89 /// \param[in] func_name
90 /// The name of the function to prepare for execution in the target.
91 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars,
92 lldb_private::IRExecutionUnit &execution_unit,
93 lldb_private::Stream &error_stream,
94 const char *func_name = "$__lldb_expr");
97 ~IRForTarget() override;
99 /// Run this IR transformer on a single module
101 /// Implementation of the llvm::ModulePass::runOnModule() function.
103 /// \param[in] llvm_module
104 /// The module to run on. This module is searched for the function
105 /// $__lldb_expr, and that function is passed to the passes one by
108 /// \param[in] interpreter_error
109 /// An error. If the expression fails to be interpreted, this error
110 /// is set to a reason why.
113 /// True on success; false otherwise
114 bool runOnModule(llvm::Module &llvm_module) override;
118 /// Implementation of the llvm::ModulePass::assignPassManager() function.
119 void assignPassManager(llvm::PMStack &pass_mgr_stack,
120 llvm::PassManagerType pass_mgr_type =
121 llvm::PMT_ModulePassManager) override;
123 /// Returns PMT_ModulePassManager
125 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
127 llvm::PassManagerType getPotentialPassManagerType() const override;
130 /// Ensures that the current function's linkage is set to external.
131 /// Otherwise the JIT may not return an address for it.
133 /// \param[in] llvm_function
134 /// The function whose linkage is to be fixed.
137 /// True on success; false otherwise.
138 bool FixFunctionLinkage(llvm::Function &llvm_function);
140 /// A module-level pass to replace all function pointers with their
141 /// integer equivalents.
143 /// The top-level pass implementation
145 /// \param[in] llvm_module
146 /// The module currently being processed.
148 /// \param[in] llvm_function
149 /// The function currently being processed.
152 /// True on success; false otherwise.
153 bool HasSideEffects(llvm::Function &llvm_function);
155 /// A function-level pass to check whether the function has side
158 /// Get the address of a function, and a location to put the complete Value
159 /// of the function if one is available.
161 /// \param[in] function
162 /// The function to find the location of.
165 /// The location of the function in the target.
168 /// The resolved name of the function (matters for intrinsics).
170 /// \param[out] value_ptr
171 /// A variable to put the function's completed Value* in, or NULL
172 /// if the Value* shouldn't be stored anywhere.
176 LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
177 lldb_private::ConstString &name,
178 llvm::Constant **&value_ptr);
180 /// A function-level pass to take the generated global value
181 /// $__lldb_expr_result and make it into a persistent variable. Also see
182 /// ASTResultSynthesizer.
184 /// Find the NamedDecl corresponding to a Value. This interface is exposed
185 /// for the IR interpreter.
187 /// \param[in] module
188 /// The module containing metadata to search
190 /// \param[in] global
191 /// The global entity to search for
194 /// The corresponding variable declaration
196 static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
197 llvm::Module *module);
200 clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
202 /// Set the constant result variable m_const_result to the provided
203 /// constant, assuming it can be evaluated. The result variable will be
204 /// reset to NULL later if the expression has side effects.
206 /// \param[in] initializer
207 /// The constant initializer for the variable.
210 /// The name of the result variable.
213 /// The Clang type of the result variable.
214 void MaybeSetConstantResult(llvm::Constant *initializer,
215 lldb_private::ConstString name,
216 lldb_private::TypeFromParser type);
218 /// If the IR represents a cast of a variable, set m_const_result to the
219 /// result of the cast. The result variable will be reset to
220 /// NULL latger if the expression has side effects.
223 /// The Clang type of the result variable.
224 void MaybeSetCastResult(lldb_private::TypeFromParser type);
226 /// The top-level pass implementation
228 /// \param[in] llvm_function
229 /// The function currently being processed.
232 /// True on success; false otherwise
233 bool CreateResultVariable(llvm::Function &llvm_function);
235 /// A module-level pass to find Objective-C constant strings and
236 /// transform them to calls to CFStringCreateWithBytes.
238 /// Rewrite a single Objective-C constant string.
241 /// The constant NSString to be transformed
244 /// The constant C string inside the NSString. This will be
245 /// passed as the bytes argument to CFStringCreateWithBytes.
248 /// True on success; false otherwise
249 bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
250 llvm::GlobalVariable *CStr);
252 /// The top-level pass implementation
255 /// True on success; false otherwise
256 bool RewriteObjCConstStrings();
258 /// A basic block-level pass to find all Objective-C method calls and
259 /// rewrite them to use sel_registerName instead of statically allocated
260 /// selectors. The reason is that the selectors are created on the
261 /// assumption that the Objective-C runtime will scan the appropriate
262 /// section and prepare them. This doesn't happen when code is copied into
263 /// the target, though, and there's no easy way to induce the runtime to
264 /// scan them. So instead we get our selectors from sel_registerName.
266 /// Replace a single selector reference
268 /// \param[in] selector_load
269 /// The load of the statically-allocated selector.
272 /// True on success; false otherwise
273 bool RewriteObjCSelector(llvm::Instruction *selector_load);
275 /// The top-level pass implementation
277 /// \param[in] basic_block
278 /// The basic block currently being processed.
281 /// True on success; false otherwise
282 bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
284 /// A basic block-level pass to find all Objective-C class references that
285 /// use the old-style Objective-C runtime and rewrite them to use
286 /// class_getClass instead of statically allocated class references.
288 /// Replace a single old-style class reference
290 /// \param[in] selector_load
291 /// The load of the statically-allocated selector.
294 /// True on success; false otherwise
295 bool RewriteObjCClassReference(llvm::Instruction *class_load);
297 /// The top-level pass implementation
299 /// \param[in] basic_block
300 /// The basic block currently being processed.
303 /// True on success; false otherwise
304 bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block);
306 /// A basic block-level pass to find all newly-declared persistent
307 /// variables and register them with the ClangExprDeclMap. This allows them
308 /// to be materialized and dematerialized like normal external variables.
309 /// Before transformation, these persistent variables look like normal
310 /// locals, so they have an allocation. This pass excises these allocations
311 /// and makes references look like external references where they will be
312 /// resolved -- like all other external references -- by ResolveExternals().
314 /// Handle a single allocation of a persistent variable
316 /// \param[in] persistent_alloc
317 /// The allocation of the persistent variable.
320 /// True on success; false otherwise
321 bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
323 /// The top-level pass implementation
325 /// \param[in] basic_block
326 /// The basic block currently being processed.
327 bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
329 /// A function-level pass to find all external variables and functions
330 /// used in the IR. Each found external variable is added to the struct,
331 /// and each external function is resolved in place, its call replaced with
332 /// a call to a function pointer whose value is the address of the function
333 /// in the target process.
335 /// Handle a single externally-defined variable
341 /// True on success; false otherwise
342 bool MaybeHandleVariable(llvm::Value *value);
344 /// Handle a single externally-defined symbol
346 /// \param[in] symbol
350 /// True on success; false otherwise
351 bool HandleSymbol(llvm::Value *symbol);
353 /// Handle a single externally-defined Objective-C class
355 /// \param[in] classlist_reference
356 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
357 /// where n (if present) is an index.
360 /// True on success; false otherwise
361 bool HandleObjCClass(llvm::Value *classlist_reference);
363 /// Handle all the arguments to a function call
366 /// The call instruction.
369 /// True on success; false otherwise
370 bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
372 /// Resolve variable references in calls to external functions
374 /// \param[in] basic_block
375 /// The basic block currently being processed.
378 /// True on success; false otherwise
379 bool ResolveCalls(llvm::BasicBlock &basic_block);
381 /// Remove calls to __cxa_atexit, which should never be generated by
384 /// \param[in] call_inst
385 /// The call instruction.
388 /// True if the scan was successful; false if some operation
390 bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
392 /// The top-level pass implementation
394 /// \param[in] basic_block
395 /// The function currently being processed.
398 /// True on success; false otherwise
399 bool ResolveExternals(llvm::Function &llvm_function);
401 /// A basic block-level pass to excise guard variables from the code.
402 /// The result for the function is passed through Clang as a static
403 /// variable. Static variables normally have guard variables to ensure that
404 /// they are only initialized once.
406 /// Rewrite a load to a guard variable to return constant 0.
408 /// \param[in] guard_load
409 /// The load instruction to zero out.
410 void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
412 /// The top-level pass implementation
414 /// \param[in] basic_block
415 /// The basic block currently being processed.
418 /// True on success; false otherwise
419 bool RemoveGuards(llvm::BasicBlock &basic_block);
421 /// A function-level pass to make all external variable references
422 /// point at the correct offsets from the void* passed into the function.
423 /// ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so
424 /// that the offsets are valid.
426 /// The top-level pass implementation
428 /// \param[in] llvm_function
429 /// The function currently being processed.
432 /// True on success; false otherwise
433 bool ReplaceVariables(llvm::Function &llvm_function);
436 bool m_resolve_vars; ///< True if external variable references and persistent
437 ///variable references should be resolved
438 lldb_private::ConstString
439 m_func_name; ///< The name of the function to translate
440 lldb_private::ConstString
441 m_result_name; ///< The name of the result variable ($0, $1, ...)
442 lldb_private::TypeFromParser
443 m_result_type; ///< The type of the result variable.
444 llvm::Module *m_module; ///< The module being processed, or NULL if that has
445 ///not been determined yet.
446 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the
447 ///module being processed, or
448 ///NULL if there is no
450 lldb_private::ClangExpressionDeclMap
451 *m_decl_map; ///< The DeclMap containing the Decls
453 m_CFStringCreateWithBytes; ///< The address of the function
454 /// CFStringCreateWithBytes, cast to the
455 /// appropriate function pointer type
456 llvm::FunctionCallee m_sel_registerName; ///< The address of the function
457 /// sel_registerName, cast to the
458 /// appropriate function pointer type
459 llvm::FunctionCallee m_objc_getClass; ///< The address of the function
460 /// objc_getClass, cast to the
461 /// appropriate function pointer type
463 *m_intptr_ty; ///< The type of an integer large enough to hold a pointer.
465 &m_error_stream; ///< The stream on which errors should be printed
466 lldb_private::IRExecutionUnit &
467 m_execution_unit; ///< The execution unit containing the IR being created.
469 llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that
470 ///writes to the result variable. If
471 /// m_has_side_effects is true, this is
473 bool m_result_is_pointer; ///< True if the function's result in the AST is a
474 ///pointer (see comments in
475 /// ASTResultSynthesizer::SynthesizeBodyResult)
477 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be
478 ///replaced by a pointer to the
480 /// location of the static allocation.
482 /// UnfoldConstant operates on a constant [Old] which has just been replaced
483 /// with a value [New]. We assume that new_value has been properly placed
484 /// early in the function, in front of the first instruction in the entry
485 /// basic block [FirstEntryInstruction].
487 /// UnfoldConstant reads through the uses of Old and replaces Old in those
488 /// uses with New. Where those uses are constants, the function generates
489 /// new instructions to compute the result of the new, non-constant
490 /// expression and places them before FirstEntryInstruction. These
491 /// instructions replace the constant uses, so UnfoldConstant calls itself
492 /// recursively for those.
494 /// \param[in] llvm_function
495 /// The function currently being processed.
498 /// True on success; false otherwise
500 class FunctionValueCache {
502 typedef std::function<llvm::Value *(llvm::Function *)> Maker;
504 FunctionValueCache(Maker const &maker);
505 ~FunctionValueCache();
506 llvm::Value *GetValue(llvm::Function *function);
510 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
511 FunctionValueMap m_values;
514 FunctionValueCache m_entry_instruction_finder;
516 static bool UnfoldConstant(llvm::Constant *old_constant,
517 llvm::Function *llvm_function,
518 FunctionValueCache &value_maker,
519 FunctionValueCache &entry_instruction_finder,
520 lldb_private::Stream &error_stream);
522 /// Commit the allocation in m_data_allocator and use its final location to
523 /// replace m_reloc_placeholder.
525 /// \param[in] module
526 /// The module that m_data_allocator resides in
529 /// True on success; false otherwise
530 bool CompleteDataAllocation();
533 #endif // liblldb_IRForTarget_h_