IRForTarget.h revision 5c9a3c7cce9da84d9eff1eb48ebadab32b1abe54
1//===-- IRForTarget.h ---------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_IRForTarget_h_
11#define liblldb_IRForTarget_h_
12
13#include "lldb/lldb-public.h"
14#include "lldb/Core/ConstString.h"
15#include "lldb/Core/Stream.h"
16#include "lldb/Symbol/TaggedASTType.h"
17#include "llvm/Pass.h"
18
19namespace llvm {
20    class BasicBlock;
21    class CallInst;
22    class Constant;
23    class ConstantInt;
24    class Function;
25    class GlobalValue;
26    class GlobalVariable;
27    class Instruction;
28    class Module;
29    class StoreInst;
30    class Type;
31    class Value;
32}
33
34namespace lldb_private {
35    class ClangExpressionDeclMap;
36}
37
38//----------------------------------------------------------------------
39/// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
40/// @brief Transforms the IR for a function to run in the target
41///
42/// Once an expression has been parsed and converted to IR, it can run
43/// in two contexts: interpreted by LLDB as a DWARF location expression,
44/// or compiled by the JIT and inserted into the target process for
45/// execution.
46///
47/// IRForTarget makes the second possible, by applying a series of
48/// transformations to the IR which make it relocatable.  These
49/// transformations are discussed in more detail next to their relevant
50/// functions.
51//----------------------------------------------------------------------
52class IRForTarget : public llvm::ModulePass
53{
54public:
55    class StaticDataAllocator {
56    public:
57        StaticDataAllocator();
58        virtual ~StaticDataAllocator();
59        virtual lldb_private::StreamString &GetStream() = 0;
60        virtual lldb::addr_t Allocate() = 0;
61    };
62
63    //------------------------------------------------------------------
64    /// Constructor
65    ///
66    /// @param[in] decl_map
67    ///     The list of externally-referenced variables for the expression,
68    ///     for use in looking up globals and allocating the argument
69    ///     struct.  See the documentation for ClangExpressionDeclMap.
70    ///
71    /// @param[in] resolve_vars
72    ///     True if the external variable references (including persistent
73    ///     variables) should be resolved.  If not, only external functions
74    ///     are resolved.
75    ///
76    /// @param[in] const_result
77    ///     This variable is populated with the statically-computed result
78    ///     of the function, if it has no side-effects and the result can
79    ///     be computed statically.
80    ///
81    /// @param[in] data_allocator
82    ///     If non-NULL, the static data allocator to use for literal strings.
83    ///
84    /// @param[in] error_stream
85    ///     If non-NULL, a stream on which errors can be printed.
86    ///
87    /// @param[in] func_name
88    ///     The name of the function to prepare for execution in the target.
89    //------------------------------------------------------------------
90    IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
91                bool resolve_vars,
92                lldb::ClangExpressionVariableSP &const_result,
93                StaticDataAllocator *data_allocator,
94                lldb_private::Stream *error_stream,
95                const char* func_name = "$__lldb_expr");
96
97    //------------------------------------------------------------------
98    /// Destructor
99    //------------------------------------------------------------------
100    virtual ~IRForTarget();
101
102    //------------------------------------------------------------------
103    /// Run this IR transformer on a single module
104    ///
105    /// Implementation of the llvm::ModulePass::runOnModule() function.
106    ///
107    /// @param[in] llvm_module
108    ///     The module to run on.  This module is searched for the function
109    ///     $__lldb_expr, and that function is passed to the passes one by
110    ///     one.
111    ///
112    /// @return
113    ///     True on success; false otherwise
114    //------------------------------------------------------------------
115    virtual bool
116    runOnModule (llvm::Module &llvm_module);
117
118    //------------------------------------------------------------------
119    /// Interface stub
120    ///
121    /// Implementation of the llvm::ModulePass::assignPassManager()
122    /// function.
123    //------------------------------------------------------------------
124    virtual void
125    assignPassManager (llvm::PMStack &pass_mgr_stack,
126                       llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager);
127
128    //------------------------------------------------------------------
129    /// Returns PMT_ModulePassManager
130    ///
131    /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
132    /// function.
133    //------------------------------------------------------------------
134    virtual llvm::PassManagerType
135    getPotentialPassManagerType() const;
136
137private:
138    //------------------------------------------------------------------
139    /// Ensures that the current function's linkage is set to external.
140    /// Otherwise the JIT may not return an address for it.
141    ///
142    /// @param[in] llvm_function
143    ///     The function whose linkage is to be fixed.
144    ///
145    /// @return
146    ///     True on success; false otherwise.
147    //------------------------------------------------------------------
148    bool
149    FixFunctionLinkage (llvm::Function &llvm_function);
150
151    //------------------------------------------------------------------
152    /// A module-level pass to replace all function pointers with their
153    /// integer equivalents.
154    //------------------------------------------------------------------
155
156    //------------------------------------------------------------------
157    /// The top-level pass implementation
158    ///
159    /// @param[in] llvm_module
160    ///     The module currently being processed.
161    ///
162    /// @param[in] llvm_function
163    ///     The function currently being processed.
164    ///
165    /// @return
166    ///     True on success; false otherwise.
167    //------------------------------------------------------------------
168    bool
169    HasSideEffects (llvm::Function &llvm_function);
170
171    //------------------------------------------------------------------
172    /// A function-level pass to check whether the function has side
173    /// effects.
174    //------------------------------------------------------------------
175
176    //------------------------------------------------------------------
177    /// Get the address of a fuction, and a location to put the complete
178    /// Value of the function if one is available.
179    ///
180    /// @param[in] function
181    ///     The function to find the location of.
182    ///
183    /// @param[out] ptr
184    ///     The location of the function in the target.
185    ///
186    /// @param[out] name
187    ///     The resolved name of the function (matters for intrinsics).
188    ///
189    /// @param[out] value_ptr
190    ///     A variable to put the function's completed Value* in, or NULL
191    ///     if the Value* shouldn't be stored anywhere.
192    ///
193    /// @return
194    ///     The pointer.
195    //------------------------------------------------------------------
196    bool
197    GetFunctionAddress (llvm::Function *function,
198                        uint64_t &ptr,
199                        lldb_private::ConstString &name,
200                        llvm::Constant **&value_ptr);
201
202    //------------------------------------------------------------------
203    /// Build a function pointer given a type and a raw pointer.
204    ///
205    /// @param[in] type
206    ///     The type of the function pointer to be built.
207    ///
208    /// @param[in] ptr
209    ///     The value of the pointer.
210    ///
211    /// @return
212    ///     The pointer.
213    //------------------------------------------------------------------
214    llvm::Constant *
215    BuildFunctionPointer (llvm::Type *type,
216                          uint64_t ptr);
217
218    //------------------------------------------------------------------
219    /// The top-level pass implementation
220    ///
221    /// @param[in] llvm_function
222    ///     The function currently being processed.
223    ///
224    /// @return
225    ///     True if the function has side effects (or if this cannot
226    ///     be determined); false otherwise.
227    //------------------------------------------------------------------
228    bool
229    ResolveFunctionPointers (llvm::Module &llvm_module,
230                             llvm::Function &llvm_function);
231
232    //------------------------------------------------------------------
233    /// A function-level pass to take the generated global value
234    /// $__lldb_expr_result and make it into a persistent variable.
235    /// Also see ASTResultSynthesizer.
236    //------------------------------------------------------------------
237
238    //------------------------------------------------------------------
239    /// Set the constant result variable m_const_result to the provided
240    /// constant, assuming it can be evaluated.  The result variable
241    /// will be reset to NULL later if the expression has side effects.
242    ///
243    /// @param[in] global
244    ///     The global entity to search for
245    ///
246    /// @return
247    ///     The corresponding variable declaration
248    //------------------------------------------------------------------
249    clang::NamedDecl *
250    DeclForGlobal (llvm::GlobalValue *global);
251
252    //------------------------------------------------------------------
253    /// Set the constant result variable m_const_result to the provided
254    /// constant, assuming it can be evaluated.  The result variable
255    /// will be reset to NULL later if the expression has side effects.
256    ///
257    /// @param[in] initializer
258    ///     The constant initializer for the variable.
259    ///
260    /// @param[in] name
261    ///     The name of the result variable.
262    ///
263    /// @param[in] type
264    ///     The Clang type of the result variable.
265    //------------------------------------------------------------------
266    void
267    MaybeSetConstantResult (llvm::Constant *initializer,
268                            const lldb_private::ConstString &name,
269                            lldb_private::TypeFromParser type);
270
271    //------------------------------------------------------------------
272    /// If the IR represents a cast of a variable, set m_const_result
273    /// to the result of the cast.  The result variable will be reset to
274    /// NULL latger if the expression has side effects.
275    ///
276    /// @param[in] type
277    ///     The Clang type of the result variable.
278    //------------------------------------------------------------------
279    void
280    MaybeSetCastResult (lldb_private::TypeFromParser type);
281
282    //------------------------------------------------------------------
283    /// The top-level pass implementation
284    ///
285    /// @param[in] llvm_function
286    ///     The function currently being processed.
287    ///
288    /// @return
289    ///     True on success; false otherwise
290    //------------------------------------------------------------------
291    bool
292    CreateResultVariable (llvm::Function &llvm_function);
293
294    //------------------------------------------------------------------
295    /// A function-level pass to find Objective-C constant strings and
296    /// transform them to calls to CFStringCreateWithBytes.
297    //------------------------------------------------------------------
298
299    //------------------------------------------------------------------
300    /// Rewrite a single Objective-C constant string.
301    ///
302    /// @param[in] NSStr
303    ///     The constant NSString to be transformed
304    ///
305    /// @param[in] CStr
306    ///     The constant C string inside the NSString.  This will be
307    ///     passed as the bytes argument to CFStringCreateWithBytes.
308    ///
309    /// @param[in] FirstEntryInstruction
310    ///     An instruction early in the execution of the function.
311    ///     When this function synthesizes a call to
312    ///     CFStringCreateWithBytes, it places the call before this
313    ///     instruction.  The instruction should come before all
314    ///     uses of the NSString.
315    ///
316    /// @return
317    ///     True on success; false otherwise
318    //------------------------------------------------------------------
319    bool
320    RewriteObjCConstString (llvm::GlobalVariable *NSStr,
321                            llvm::GlobalVariable *CStr,
322                            llvm::Instruction *FirstEntryInstruction);
323
324    //------------------------------------------------------------------
325    /// The top-level pass implementation
326    ///
327    /// @param[in] llvm_function
328    ///     The function currently being processed.
329    ///
330    /// @return
331    ///     True on success; false otherwise
332    //------------------------------------------------------------------
333    bool
334    RewriteObjCConstStrings (llvm::Function &llvm_function);
335
336    //------------------------------------------------------------------
337    /// A basic block-level pass to find all Objective-C method calls and
338    /// rewrite them to use sel_registerName instead of statically allocated
339    /// selectors.  The reason is that the selectors are created on the
340    /// assumption that the Objective-C runtime will scan the appropriate
341    /// section and prepare them.  This doesn't happen when code is copied
342    /// into the target, though, and there's no easy way to induce the
343    /// runtime to scan them.  So instead we get our selectors from
344    /// sel_registerName.
345    //------------------------------------------------------------------
346
347    //------------------------------------------------------------------
348    /// Replace a single selector reference
349    ///
350    /// @param[in] selector_load
351    ///     The load of the statically-allocated selector.
352    ///
353    /// @return
354    ///     True on success; false otherwise
355    //------------------------------------------------------------------
356    bool
357    RewriteObjCSelector (llvm::Instruction* selector_load);
358
359    //------------------------------------------------------------------
360    /// The top-level pass implementation
361    ///
362    /// @param[in] basic_block
363    ///     The basic block currently being processed.
364    ///
365    /// @return
366    ///     True on success; false otherwise
367    //------------------------------------------------------------------
368    bool
369    RewriteObjCSelectors (llvm::BasicBlock &basic_block);
370
371    //------------------------------------------------------------------
372    /// A basic block-level pass to find all newly-declared persistent
373    /// variables and register them with the ClangExprDeclMap.  This
374    /// allows them to be materialized and dematerialized like normal
375    /// external variables.  Before transformation, these persistent
376    /// variables look like normal locals, so they have an allocation.
377    /// This pass excises these allocations and makes references look
378    /// like external references where they will be resolved -- like all
379    /// other external references -- by ResolveExternals().
380    //------------------------------------------------------------------
381
382    //------------------------------------------------------------------
383    /// Handle a single allocation of a persistent variable
384    ///
385    /// @param[in] persistent_alloc
386    ///     The allocation of the persistent variable.
387    ///
388    /// @return
389    ///     True on success; false otherwise
390    //------------------------------------------------------------------
391    bool
392    RewritePersistentAlloc (llvm::Instruction *persistent_alloc);
393
394    //------------------------------------------------------------------
395    /// The top-level pass implementation
396    ///
397    /// @param[in] basic_block
398    ///     The basic block currently being processed.
399    //------------------------------------------------------------------
400    bool
401    RewritePersistentAllocs (llvm::BasicBlock &basic_block);
402
403    //------------------------------------------------------------------
404    /// A function-level pass to find all external variables and functions
405    /// used in the IR.  Each found external variable is added to the
406    /// struct, and each external function is resolved in place, its call
407    /// replaced with a call to a function pointer whose value is the
408    /// address of the function in the target process.
409    //------------------------------------------------------------------
410
411    //------------------------------------------------------------------
412    /// Handle a single externally-defined variable
413    ///
414    /// @param[in] value
415    ///     The variable.
416    ///
417    /// @return
418    ///     True on success; false otherwise
419    //------------------------------------------------------------------
420    bool
421    MaybeHandleVariable (llvm::Value *value);
422
423    //------------------------------------------------------------------
424    /// Handle a single externally-defined symbol
425    ///
426    /// @param[in] symbol
427    ///     The symbol.
428    ///
429    /// @return
430    ///     True on success; false otherwise
431    //------------------------------------------------------------------
432    bool
433    HandleSymbol (llvm::Value *symbol);
434
435    //------------------------------------------------------------------
436    /// Handle all the arguments to a function call
437    ///
438    /// @param[in] C
439    ///     The call instruction.
440    ///
441    /// @return
442    ///     True on success; false otherwise
443    //------------------------------------------------------------------
444    bool
445    MaybeHandleCallArguments (llvm::CallInst *call_inst);
446
447    //------------------------------------------------------------------
448    /// Resolve variable references in calls to external functions
449    ///
450    /// @param[in] basic_block
451    ///     The basic block currently being processed.
452    ///
453    /// @return
454    ///     True on success; false otherwise
455    //------------------------------------------------------------------
456    bool
457    ResolveCalls (llvm::BasicBlock &basic_block);
458
459    //------------------------------------------------------------------
460    /// The top-level pass implementation
461    ///
462    /// @param[in] basic_block
463    ///     The function currently being processed.
464    ///
465    /// @return
466    ///     True on success; false otherwise
467    //------------------------------------------------------------------
468    bool
469    ResolveExternals (llvm::Function &llvm_function);
470
471    //------------------------------------------------------------------
472    /// A basic block-level pass to excise guard variables from the code.
473    /// The result for the function is passed through Clang as a static
474    /// variable.  Static variables normally have guard variables to
475    /// ensure that they are only initialized once.
476    //------------------------------------------------------------------
477
478    //------------------------------------------------------------------
479    /// Rewrite a load to a guard variable to return constant 0.
480    ///
481    /// @param[in] guard_load
482    ///     The load instruction to zero out.
483    //------------------------------------------------------------------
484    void
485    TurnGuardLoadIntoZero(llvm::Instruction* guard_load);
486
487    //------------------------------------------------------------------
488    /// The top-level pass implementation
489    ///
490    /// @param[in] basic_block
491    ///     The basic block currently being processed.
492    ///
493    /// @return
494    ///     True on success; false otherwise
495    //------------------------------------------------------------------
496    bool
497    RemoveGuards (llvm::BasicBlock &basic_block);
498
499    //------------------------------------------------------------------
500    /// A module-level pass to allocate all string literals in a separate
501    /// allocation and redirect references to them.
502    //------------------------------------------------------------------
503
504    //------------------------------------------------------------------
505    /// The top-level pass implementation
506    ///
507    /// @return
508    ///     True on success; false otherwise
509    //------------------------------------------------------------------
510    bool
511    ReplaceStrings ();
512
513    //------------------------------------------------------------------
514    /// A basick block-level pass to find all literals that will be
515    /// allocated as statics by the JIT (in contrast to the Strings,
516    /// which already are statics) and synthesize loads for them.
517    //------------------------------------------------------------------
518
519    //------------------------------------------------------------------
520    /// The top-level pass implementation
521    ///
522    /// @param[in] basic_block
523    ///     The basic block currently being processed.
524    ///
525    /// @return
526    ///     True on success; false otherwise
527    //------------------------------------------------------------------
528    bool
529    ReplaceStaticLiterals (llvm::BasicBlock &basic_block);
530
531    //------------------------------------------------------------------
532    /// A function-level pass to make all external variable references
533    /// point at the correct offsets from the void* passed into the
534    /// function.  ClangExpressionDeclMap::DoStructLayout() must be called
535    /// beforehand, so that the offsets are valid.
536    //------------------------------------------------------------------
537
538    //------------------------------------------------------------------
539    /// The top-level pass implementation
540    ///
541    /// @param[in] llvm_function
542    ///     The function currently being processed.
543    ///
544    /// @return
545    ///     True on success; false otherwise
546    //------------------------------------------------------------------
547    bool
548    ReplaceVariables (llvm::Function &llvm_function);
549
550    /// Flags
551    bool                                    m_resolve_vars;             ///< True if external variable references and persistent variable references should be resolved
552    std::string                             m_func_name;                ///< The name of the function to translate
553    lldb_private::ConstString               m_result_name;              ///< The name of the result variable ($0, $1, ...)
554    llvm::Module                           *m_module;                   ///< The module being processed, or NULL if that has not been determined yet.
555    lldb_private::ClangExpressionDeclMap   *m_decl_map;                 ///< The DeclMap containing the Decls
556    StaticDataAllocator                    *m_data_allocator;           ///< If non-NULL, the allocator to use for constant strings
557    llvm::Constant                         *m_CFStringCreateWithBytes;  ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
558    llvm::Constant                         *m_sel_registerName;         ///< The address of the function sel_registerName, cast to the appropriate function pointer type
559    lldb::ClangExpressionVariableSP        &m_const_result;             ///< This value should be set to the return value of the expression if it is constant and the expression has no side effects
560    lldb_private::Stream                   *m_error_stream;             ///< If non-NULL, the stream on which errors should be printed
561
562    bool                                    m_has_side_effects;         ///< True if the function's result cannot be simply determined statically
563    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.
564    bool                                    m_result_is_pointer;        ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
565
566    llvm::GlobalVariable                   *m_reloc_placeholder;        ///< A placeholder that will be replaced by a pointer to the final location of the static allocation.
567
568    //------------------------------------------------------------------
569    /// UnfoldConstant operates on a constant [Old] which has just been
570    /// replaced with a value [New].  We assume that new_value has
571    /// been properly placed early in the function, in front of the
572    /// first instruction in the entry basic block
573    /// [FirstEntryInstruction].
574    ///
575    /// UnfoldConstant reads through the uses of Old and replaces Old
576    /// in those uses with New.  Where those uses are constants, the
577    /// function generates new instructions to compute the result of the
578    /// new, non-constant expression and places them before
579    /// FirstEntryInstruction.  These instructions replace the constant
580    /// uses, so UnfoldConstant calls itself recursively for those.
581    ///
582    /// @param[in] llvm_function
583    ///     The function currently being processed.
584    ///
585    /// @return
586    ///     True on success; false otherwise
587    //------------------------------------------------------------------
588    static bool
589    UnfoldConstant (llvm::Constant *old_constant,
590                    llvm::Value *new_constant,
591                    llvm::Instruction *first_entry_inst);
592
593    //------------------------------------------------------------------
594    /// Construct a reference to m_reloc_placeholder with a given type
595    /// and offset.  This typically happens after inserting data into
596    /// m_data_allocator.
597    ///
598    /// @param[in] type
599    ///     The type of the value being loaded.
600    ///
601    /// @param[in] offset
602    ///     The offset of the value from the base of m_data_allocator.
603    ///
604    /// @return
605    ///     The Constant for the reference, usually a ConstantExpr.
606    //------------------------------------------------------------------
607    llvm::Constant *
608    BuildRelocation(llvm::Type *type,
609                    uint64_t offset);
610
611    //------------------------------------------------------------------
612    /// Commit the allocation in m_data_allocator and use its final
613    /// location to replace m_reloc_placeholder.
614    ///
615    /// @param[in] module
616    ///     The module that m_data_allocator resides in
617    ///
618    /// @return
619    ///     True on success; false otherwise
620    //------------------------------------------------------------------
621    bool
622    CompleteDataAllocation ();
623
624};
625
626#endif
627