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