xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.h (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1061da546Spatrick //===-- IRForTarget.h ---------------------------------------------*- C++
2061da546Spatrick //-*-===//
3061da546Spatrick //
4061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
6061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7061da546Spatrick //
8061da546Spatrick //===----------------------------------------------------------------------===//
9061da546Spatrick 
10dda28197Spatrick #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
11dda28197Spatrick #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
12061da546Spatrick 
13061da546Spatrick #include "lldb/Symbol/TaggedASTType.h"
14061da546Spatrick #include "lldb/Utility/ConstString.h"
15061da546Spatrick #include "lldb/Utility/Status.h"
16061da546Spatrick #include "lldb/Utility/Stream.h"
17061da546Spatrick #include "lldb/Utility/StreamString.h"
18061da546Spatrick #include "lldb/lldb-public.h"
19061da546Spatrick #include "llvm/IR/DerivedTypes.h"
20061da546Spatrick #include "llvm/Pass.h"
21061da546Spatrick 
22061da546Spatrick #include <functional>
23061da546Spatrick #include <map>
24061da546Spatrick 
25061da546Spatrick namespace llvm {
26061da546Spatrick class BasicBlock;
27061da546Spatrick class CallInst;
28061da546Spatrick class Constant;
29061da546Spatrick class ConstantInt;
30061da546Spatrick class Function;
31061da546Spatrick class GlobalValue;
32061da546Spatrick class GlobalVariable;
33061da546Spatrick class Instruction;
34061da546Spatrick class Module;
35061da546Spatrick class StoreInst;
36061da546Spatrick class DataLayout;
37061da546Spatrick class Value;
38061da546Spatrick }
39061da546Spatrick 
40dda28197Spatrick namespace clang {
41dda28197Spatrick class NamedDecl;
42dda28197Spatrick }
43dda28197Spatrick 
44061da546Spatrick namespace lldb_private {
45061da546Spatrick class ClangExpressionDeclMap;
46061da546Spatrick class IRExecutionUnit;
47061da546Spatrick class IRMemoryMap;
48061da546Spatrick }
49061da546Spatrick 
50061da546Spatrick /// \class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
51061da546Spatrick /// Transforms the IR for a function to run in the target
52061da546Spatrick ///
53061da546Spatrick /// Once an expression has been parsed and converted to IR, it can run in two
54061da546Spatrick /// contexts: interpreted by LLDB as a DWARF location expression, or compiled
55061da546Spatrick /// by the JIT and inserted into the target process for execution.
56061da546Spatrick ///
57061da546Spatrick /// IRForTarget makes the second possible, by applying a series of
58061da546Spatrick /// transformations to the IR which make it relocatable.  These
59061da546Spatrick /// transformations are discussed in more detail next to their relevant
60061da546Spatrick /// functions.
61*be691f3bSpatrick class IRForTarget {
62061da546Spatrick public:
63061da546Spatrick   enum class LookupResult { Success, Fail, Ignore };
64061da546Spatrick 
65061da546Spatrick   /// Constructor
66061da546Spatrick   ///
67061da546Spatrick   /// \param[in] decl_map
68061da546Spatrick   ///     The list of externally-referenced variables for the expression,
69061da546Spatrick   ///     for use in looking up globals and allocating the argument
70061da546Spatrick   ///     struct.  See the documentation for ClangExpressionDeclMap.
71061da546Spatrick   ///
72061da546Spatrick   /// \param[in] resolve_vars
73061da546Spatrick   ///     True if the external variable references (including persistent
74061da546Spatrick   ///     variables) should be resolved.  If not, only external functions
75061da546Spatrick   ///     are resolved.
76061da546Spatrick   ///
77061da546Spatrick   /// \param[in] execution_unit
78061da546Spatrick   ///     The holder for raw data associated with the expression.
79061da546Spatrick   ///
80061da546Spatrick   /// \param[in] error_stream
81061da546Spatrick   ///     If non-NULL, a stream on which errors can be printed.
82061da546Spatrick   ///
83061da546Spatrick   /// \param[in] func_name
84061da546Spatrick   ///     The name of the function to prepare for execution in the target.
85061da546Spatrick   IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars,
86061da546Spatrick               lldb_private::IRExecutionUnit &execution_unit,
87061da546Spatrick               lldb_private::Stream &error_stream,
88061da546Spatrick               const char *func_name = "$__lldb_expr");
89061da546Spatrick 
90061da546Spatrick   /// Run this IR transformer on a single module
91061da546Spatrick   ///
92061da546Spatrick   /// Implementation of the llvm::ModulePass::runOnModule() function.
93061da546Spatrick   ///
94061da546Spatrick   /// \param[in] llvm_module
95061da546Spatrick   ///     The module to run on.  This module is searched for the function
96061da546Spatrick   ///     $__lldb_expr, and that function is passed to the passes one by
97061da546Spatrick   ///     one.
98061da546Spatrick   ///
99061da546Spatrick   /// \return
100061da546Spatrick   ///     True on success; false otherwise
101*be691f3bSpatrick   bool runOnModule(llvm::Module &llvm_module);
102061da546Spatrick 
103061da546Spatrick private:
104061da546Spatrick   /// Ensures that the current function's linkage is set to external.
105061da546Spatrick   /// Otherwise the JIT may not return an address for it.
106061da546Spatrick   ///
107061da546Spatrick   /// \param[in] llvm_function
108061da546Spatrick   ///     The function whose linkage is to be fixed.
109061da546Spatrick   ///
110061da546Spatrick   /// \return
111061da546Spatrick   ///     True on success; false otherwise.
112061da546Spatrick   bool FixFunctionLinkage(llvm::Function &llvm_function);
113061da546Spatrick 
114061da546Spatrick   /// A module-level pass to replace all function pointers with their
115061da546Spatrick   /// integer equivalents.
116061da546Spatrick 
117061da546Spatrick   /// The top-level pass implementation
118061da546Spatrick   ///
119061da546Spatrick   /// \param[in] llvm_function
120061da546Spatrick   ///     The function currently being processed.
121061da546Spatrick   ///
122061da546Spatrick   /// \return
123061da546Spatrick   ///     True on success; false otherwise.
124061da546Spatrick   bool HasSideEffects(llvm::Function &llvm_function);
125061da546Spatrick 
126061da546Spatrick   /// A function-level pass to check whether the function has side
127061da546Spatrick   /// effects.
128061da546Spatrick 
129061da546Spatrick   /// Get the address of a function, and a location to put the complete Value
130061da546Spatrick   /// of the function if one is available.
131061da546Spatrick   ///
132061da546Spatrick   /// \param[in] function
133061da546Spatrick   ///     The function to find the location of.
134061da546Spatrick   ///
135061da546Spatrick   /// \param[out] ptr
136061da546Spatrick   ///     The location of the function in the target.
137061da546Spatrick   ///
138061da546Spatrick   /// \param[out] name
139061da546Spatrick   ///     The resolved name of the function (matters for intrinsics).
140061da546Spatrick   ///
141061da546Spatrick   /// \param[out] value_ptr
142061da546Spatrick   ///     A variable to put the function's completed Value* in, or NULL
143061da546Spatrick   ///     if the Value* shouldn't be stored anywhere.
144061da546Spatrick   ///
145061da546Spatrick   /// \return
146061da546Spatrick   ///     The pointer.
147061da546Spatrick   LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
148061da546Spatrick                                   lldb_private::ConstString &name,
149061da546Spatrick                                   llvm::Constant **&value_ptr);
150061da546Spatrick 
151061da546Spatrick   /// A function-level pass to take the generated global value
152061da546Spatrick   /// $__lldb_expr_result and make it into a persistent variable. Also see
153061da546Spatrick   /// ASTResultSynthesizer.
154061da546Spatrick 
155061da546Spatrick   /// Find the NamedDecl corresponding to a Value.  This interface is exposed
156061da546Spatrick   /// for the IR interpreter.
157061da546Spatrick   ///
158061da546Spatrick   /// \param[in] global_val
159061da546Spatrick   ///     The global entity to search for
160061da546Spatrick   ///
161061da546Spatrick   /// \param[in] module
162061da546Spatrick   ///     The module containing metadata to search
163061da546Spatrick   ///
164061da546Spatrick   /// \return
165061da546Spatrick   ///     The corresponding variable declaration
166061da546Spatrick public:
167061da546Spatrick   static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
168061da546Spatrick                                          llvm::Module *module);
169061da546Spatrick 
170061da546Spatrick private:
171061da546Spatrick   clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
172061da546Spatrick 
173061da546Spatrick   /// Set the constant result variable m_const_result to the provided
174061da546Spatrick   /// constant, assuming it can be evaluated.  The result variable will be
175061da546Spatrick   /// reset to NULL later if the expression has side effects.
176061da546Spatrick   ///
177061da546Spatrick   /// \param[in] initializer
178061da546Spatrick   ///     The constant initializer for the variable.
179061da546Spatrick   ///
180061da546Spatrick   /// \param[in] name
181061da546Spatrick   ///     The name of the result variable.
182061da546Spatrick   ///
183061da546Spatrick   /// \param[in] type
184061da546Spatrick   ///     The Clang type of the result variable.
185061da546Spatrick   void MaybeSetConstantResult(llvm::Constant *initializer,
186061da546Spatrick                               lldb_private::ConstString name,
187061da546Spatrick                               lldb_private::TypeFromParser type);
188061da546Spatrick 
189061da546Spatrick   /// If the IR represents a cast of a variable, set m_const_result to the
190061da546Spatrick   /// result of the cast.  The result variable will be reset to
191061da546Spatrick   /// NULL latger if the expression has side effects.
192061da546Spatrick   ///
193061da546Spatrick   /// \param[in] type
194061da546Spatrick   ///     The Clang type of the result variable.
195061da546Spatrick   void MaybeSetCastResult(lldb_private::TypeFromParser type);
196061da546Spatrick 
197061da546Spatrick   /// The top-level pass implementation
198061da546Spatrick   ///
199061da546Spatrick   /// \param[in] llvm_function
200061da546Spatrick   ///     The function currently being processed.
201061da546Spatrick   ///
202061da546Spatrick   /// \return
203061da546Spatrick   ///     True on success; false otherwise
204061da546Spatrick   bool CreateResultVariable(llvm::Function &llvm_function);
205061da546Spatrick 
206061da546Spatrick   /// A module-level pass to find Objective-C constant strings and
207061da546Spatrick   /// transform them to calls to CFStringCreateWithBytes.
208061da546Spatrick 
209061da546Spatrick   /// Rewrite a single Objective-C constant string.
210061da546Spatrick   ///
211061da546Spatrick   /// \param[in] NSStr
212061da546Spatrick   ///     The constant NSString to be transformed
213061da546Spatrick   ///
214061da546Spatrick   /// \param[in] CStr
215061da546Spatrick   ///     The constant C string inside the NSString.  This will be
216061da546Spatrick   ///     passed as the bytes argument to CFStringCreateWithBytes.
217061da546Spatrick   ///
218061da546Spatrick   /// \return
219061da546Spatrick   ///     True on success; false otherwise
220061da546Spatrick   bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
221061da546Spatrick                               llvm::GlobalVariable *CStr);
222061da546Spatrick 
223061da546Spatrick   /// The top-level pass implementation
224061da546Spatrick   ///
225061da546Spatrick   /// \return
226061da546Spatrick   ///     True on success; false otherwise
227061da546Spatrick   bool RewriteObjCConstStrings();
228061da546Spatrick 
229061da546Spatrick   /// A basic block-level pass to find all Objective-C method calls and
230061da546Spatrick   /// rewrite them to use sel_registerName instead of statically allocated
231061da546Spatrick   /// selectors.  The reason is that the selectors are created on the
232061da546Spatrick   /// assumption that the Objective-C runtime will scan the appropriate
233061da546Spatrick   /// section and prepare them.  This doesn't happen when code is copied into
234061da546Spatrick   /// the target, though, and there's no easy way to induce the runtime to
235061da546Spatrick   /// scan them.  So instead we get our selectors from sel_registerName.
236061da546Spatrick 
237061da546Spatrick   /// Replace a single selector reference
238061da546Spatrick   ///
239061da546Spatrick   /// \param[in] selector_load
240061da546Spatrick   ///     The load of the statically-allocated selector.
241061da546Spatrick   ///
242061da546Spatrick   /// \return
243061da546Spatrick   ///     True on success; false otherwise
244061da546Spatrick   bool RewriteObjCSelector(llvm::Instruction *selector_load);
245061da546Spatrick 
246061da546Spatrick   /// The top-level pass implementation
247061da546Spatrick   ///
248061da546Spatrick   /// \param[in] basic_block
249061da546Spatrick   ///     The basic block currently being processed.
250061da546Spatrick   ///
251061da546Spatrick   /// \return
252061da546Spatrick   ///     True on success; false otherwise
253061da546Spatrick   bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
254061da546Spatrick 
255061da546Spatrick   /// A basic block-level pass to find all Objective-C class references that
256061da546Spatrick   /// use the old-style Objective-C runtime and rewrite them to use
257061da546Spatrick   /// class_getClass instead of statically allocated class references.
258061da546Spatrick 
259061da546Spatrick   /// Replace a single old-style class reference
260061da546Spatrick   ///
261061da546Spatrick   /// \param[in] class_load
262061da546Spatrick   ///     The load of the statically-allocated selector.
263061da546Spatrick   ///
264061da546Spatrick   /// \return
265061da546Spatrick   ///     True on success; false otherwise
266061da546Spatrick   bool RewriteObjCClassReference(llvm::Instruction *class_load);
267061da546Spatrick 
268061da546Spatrick   /// The top-level pass implementation
269061da546Spatrick   ///
270061da546Spatrick   /// \param[in] basic_block
271061da546Spatrick   ///     The basic block currently being processed.
272061da546Spatrick   ///
273061da546Spatrick   /// \return
274061da546Spatrick   ///     True on success; false otherwise
275061da546Spatrick   bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block);
276061da546Spatrick 
277061da546Spatrick   /// A basic block-level pass to find all newly-declared persistent
278061da546Spatrick   /// variables and register them with the ClangExprDeclMap.  This allows them
279061da546Spatrick   /// to be materialized and dematerialized like normal external variables.
280061da546Spatrick   /// Before transformation, these persistent variables look like normal
281061da546Spatrick   /// locals, so they have an allocation. This pass excises these allocations
282061da546Spatrick   /// and makes references look like external references where they will be
283061da546Spatrick   /// resolved -- like all other external references -- by ResolveExternals().
284061da546Spatrick 
285061da546Spatrick   /// Handle a single allocation of a persistent variable
286061da546Spatrick   ///
287061da546Spatrick   /// \param[in] persistent_alloc
288061da546Spatrick   ///     The allocation of the persistent variable.
289061da546Spatrick   ///
290061da546Spatrick   /// \return
291061da546Spatrick   ///     True on success; false otherwise
292061da546Spatrick   bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
293061da546Spatrick 
294061da546Spatrick   /// The top-level pass implementation
295061da546Spatrick   ///
296061da546Spatrick   /// \param[in] basic_block
297061da546Spatrick   ///     The basic block currently being processed.
298061da546Spatrick   bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
299061da546Spatrick 
300061da546Spatrick   /// A function-level pass to find all external variables and functions
301061da546Spatrick   /// used in the IR.  Each found external variable is added to the struct,
302061da546Spatrick   /// and each external function is resolved in place, its call replaced with
303061da546Spatrick   /// a call to a function pointer whose value is the address of the function
304061da546Spatrick   /// in the target process.
305061da546Spatrick 
306061da546Spatrick   /// Handle a single externally-defined variable
307061da546Spatrick   ///
308061da546Spatrick   /// \param[in] value
309061da546Spatrick   ///     The variable.
310061da546Spatrick   ///
311061da546Spatrick   /// \return
312061da546Spatrick   ///     True on success; false otherwise
313061da546Spatrick   bool MaybeHandleVariable(llvm::Value *value);
314061da546Spatrick 
315061da546Spatrick   /// Handle a single externally-defined symbol
316061da546Spatrick   ///
317061da546Spatrick   /// \param[in] symbol
318061da546Spatrick   ///     The symbol.
319061da546Spatrick   ///
320061da546Spatrick   /// \return
321061da546Spatrick   ///     True on success; false otherwise
322061da546Spatrick   bool HandleSymbol(llvm::Value *symbol);
323061da546Spatrick 
324061da546Spatrick   /// Handle a single externally-defined Objective-C class
325061da546Spatrick   ///
326061da546Spatrick   /// \param[in] classlist_reference
327061da546Spatrick   ///     The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
328061da546Spatrick   ///     where n (if present) is an index.
329061da546Spatrick   ///
330061da546Spatrick   /// \return
331061da546Spatrick   ///     True on success; false otherwise
332061da546Spatrick   bool HandleObjCClass(llvm::Value *classlist_reference);
333061da546Spatrick 
334061da546Spatrick   /// Handle all the arguments to a function call
335061da546Spatrick   ///
336061da546Spatrick   /// \param[in] call_inst
337061da546Spatrick   ///     The call instruction.
338061da546Spatrick   ///
339061da546Spatrick   /// \return
340061da546Spatrick   ///     True on success; false otherwise
341061da546Spatrick   bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
342061da546Spatrick 
343061da546Spatrick   /// Resolve variable references in calls to external functions
344061da546Spatrick   ///
345061da546Spatrick   /// \param[in] basic_block
346061da546Spatrick   ///     The basic block currently being processed.
347061da546Spatrick   ///
348061da546Spatrick   /// \return
349061da546Spatrick   ///     True on success; false otherwise
350061da546Spatrick   bool ResolveCalls(llvm::BasicBlock &basic_block);
351061da546Spatrick 
352061da546Spatrick   /// Remove calls to __cxa_atexit, which should never be generated by
353061da546Spatrick   /// expressions.
354061da546Spatrick   ///
355061da546Spatrick   /// \param[in] basic_block
356061da546Spatrick   ///     The basic block currently being processed.
357061da546Spatrick   ///
358061da546Spatrick   /// \return
359061da546Spatrick   ///     True if the scan was successful; false if some operation
360061da546Spatrick   ///     failed
361061da546Spatrick   bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
362061da546Spatrick 
363061da546Spatrick   /// The top-level pass implementation
364061da546Spatrick   ///
365061da546Spatrick   /// \param[in] llvm_function
366061da546Spatrick   ///     The function currently being processed.
367061da546Spatrick   ///
368061da546Spatrick   /// \return
369061da546Spatrick   ///     True on success; false otherwise
370061da546Spatrick   bool ResolveExternals(llvm::Function &llvm_function);
371061da546Spatrick 
372061da546Spatrick   /// A basic block-level pass to excise guard variables from the code.
373061da546Spatrick   /// The result for the function is passed through Clang as a static
374061da546Spatrick   /// variable.  Static variables normally have guard variables to ensure that
375061da546Spatrick   /// they are only initialized once.
376061da546Spatrick 
377061da546Spatrick   /// Rewrite a load to a guard variable to return constant 0.
378061da546Spatrick   ///
379061da546Spatrick   /// \param[in] guard_load
380061da546Spatrick   ///     The load instruction to zero out.
381061da546Spatrick   void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
382061da546Spatrick 
383061da546Spatrick   /// The top-level pass implementation
384061da546Spatrick   ///
385061da546Spatrick   /// \param[in] basic_block
386061da546Spatrick   ///     The basic block currently being processed.
387061da546Spatrick   ///
388061da546Spatrick   /// \return
389061da546Spatrick   ///     True on success; false otherwise
390061da546Spatrick   bool RemoveGuards(llvm::BasicBlock &basic_block);
391061da546Spatrick 
392061da546Spatrick   /// A function-level pass to make all external variable references
393061da546Spatrick   /// point at the correct offsets from the void* passed into the function.
394061da546Spatrick   /// ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so
395061da546Spatrick   /// that the offsets are valid.
396061da546Spatrick 
397061da546Spatrick   /// The top-level pass implementation
398061da546Spatrick   ///
399061da546Spatrick   /// \param[in] llvm_function
400061da546Spatrick   ///     The function currently being processed.
401061da546Spatrick   ///
402061da546Spatrick   /// \return
403061da546Spatrick   ///     True on success; false otherwise
404061da546Spatrick   bool ReplaceVariables(llvm::Function &llvm_function);
405061da546Spatrick 
406*be691f3bSpatrick   /// True if external variable references and persistent variable references
407*be691f3bSpatrick   /// should be resolved
408*be691f3bSpatrick   bool m_resolve_vars;
409*be691f3bSpatrick   /// The name of the function to translate
410*be691f3bSpatrick   lldb_private::ConstString m_func_name;
411*be691f3bSpatrick   /// The name of the result variable ($0, $1, ...)
412*be691f3bSpatrick   lldb_private::ConstString m_result_name;
413*be691f3bSpatrick   /// The type of the result variable.
414*be691f3bSpatrick   lldb_private::TypeFromParser m_result_type;
415*be691f3bSpatrick   /// The module being processed, or NULL if that has not been determined yet.
416*be691f3bSpatrick   llvm::Module *m_module = nullptr;
417*be691f3bSpatrick   /// The target data for the module being processed, or NULL if there is no
418061da546Spatrick   /// module.
419*be691f3bSpatrick   std::unique_ptr<llvm::DataLayout> m_target_data;
420*be691f3bSpatrick   /// The DeclMap containing the Decls
421*be691f3bSpatrick   lldb_private::ClangExpressionDeclMap *m_decl_map;
422*be691f3bSpatrick   /// The address of the function CFStringCreateWithBytes, cast to the
423061da546Spatrick   /// appropriate function pointer type
424*be691f3bSpatrick   llvm::FunctionCallee m_CFStringCreateWithBytes;
425*be691f3bSpatrick   /// The address of the function sel_registerName, cast to the appropriate
426*be691f3bSpatrick   /// function pointer type.
427*be691f3bSpatrick   llvm::FunctionCallee m_sel_registerName;
428*be691f3bSpatrick   /// The address of the function objc_getClass, cast to the appropriate
429*be691f3bSpatrick   /// function pointer type.
430*be691f3bSpatrick   llvm::FunctionCallee m_objc_getClass;
431*be691f3bSpatrick   /// The type of an integer large enough to hold a pointer.
432*be691f3bSpatrick   llvm::IntegerType *m_intptr_ty = nullptr;
433*be691f3bSpatrick   /// The stream on which errors should be printed.
434*be691f3bSpatrick   lldb_private::Stream &m_error_stream;
435*be691f3bSpatrick   /// The execution unit containing the IR being created.
436*be691f3bSpatrick   lldb_private::IRExecutionUnit &m_execution_unit;
437*be691f3bSpatrick   /// If non-NULL, the store instruction that writes to the result variable.  If
438*be691f3bSpatrick   /// m_has_side_effects is true, this is NULL.
439*be691f3bSpatrick   llvm::StoreInst *m_result_store = nullptr;
440*be691f3bSpatrick   /// True if the function's result in the AST is a pointer (see comments in
441061da546Spatrick   /// ASTResultSynthesizer::SynthesizeBodyResult)
442*be691f3bSpatrick   bool m_result_is_pointer = false;
443061da546Spatrick   /// A placeholder that will be replaced by a pointer to the final location of
444061da546Spatrick   /// the static allocation.
445*be691f3bSpatrick   llvm::GlobalVariable *m_reloc_placeholder = nullptr;
446061da546Spatrick 
447061da546Spatrick   class FunctionValueCache {
448061da546Spatrick   public:
449061da546Spatrick     typedef std::function<llvm::Value *(llvm::Function *)> Maker;
450061da546Spatrick 
451061da546Spatrick     FunctionValueCache(Maker const &maker);
452061da546Spatrick     ~FunctionValueCache();
453061da546Spatrick     llvm::Value *GetValue(llvm::Function *function);
454061da546Spatrick 
455061da546Spatrick   private:
456061da546Spatrick     Maker const m_maker;
457061da546Spatrick     typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
458061da546Spatrick     FunctionValueMap m_values;
459061da546Spatrick   };
460061da546Spatrick 
461061da546Spatrick   FunctionValueCache m_entry_instruction_finder;
462061da546Spatrick 
463061da546Spatrick   /// UnfoldConstant operates on a constant [Old] which has just been replaced
464061da546Spatrick   /// with a value [New].  We assume that new_value has been properly placed
465061da546Spatrick   /// early in the function, in front of the first instruction in the entry
466061da546Spatrick   /// basic block [FirstEntryInstruction].
467061da546Spatrick   ///
468061da546Spatrick   /// UnfoldConstant reads through the uses of Old and replaces Old in those
469061da546Spatrick   /// uses with New.  Where those uses are constants, the function generates
470061da546Spatrick   /// new instructions to compute the result of the new, non-constant
471061da546Spatrick   /// expression and places them before FirstEntryInstruction.  These
472061da546Spatrick   /// instructions replace the constant uses, so UnfoldConstant calls itself
473061da546Spatrick   /// recursively for those.
474061da546Spatrick   ///
475061da546Spatrick   /// \return
476061da546Spatrick   ///     True on success; false otherwise
477061da546Spatrick   static bool UnfoldConstant(llvm::Constant *old_constant,
478061da546Spatrick                              llvm::Function *llvm_function,
479061da546Spatrick                              FunctionValueCache &value_maker,
480061da546Spatrick                              FunctionValueCache &entry_instruction_finder,
481061da546Spatrick                              lldb_private::Stream &error_stream);
482061da546Spatrick 
483061da546Spatrick   /// Commit the allocation in m_data_allocator and use its final location to
484061da546Spatrick   /// replace m_reloc_placeholder.
485061da546Spatrick   ///
486061da546Spatrick   /// \return
487061da546Spatrick   ///     True on success; false otherwise
488061da546Spatrick   bool CompleteDataAllocation();
489061da546Spatrick };
490061da546Spatrick 
491dda28197Spatrick #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_IRFORTARGET_H
492