IRForTarget.h revision f285ebcfa3ce0fd200b6a566eb1196927e618fc6
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 "llvm/Pass.h" 14 15namespace llvm { 16 class BasicBlock; 17 class CallInst; 18 class Constant; 19 class Function; 20 class Instruction; 21 class Module; 22 class TargetData; 23 class Value; 24} 25 26namespace lldb_private { 27 class ClangExpressionDeclMap; 28} 29 30//---------------------------------------------------------------------- 31/// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h" 32/// @brief Transforms the IR for a function to run in the target 33/// 34/// Once an expression has been parsed and converted to IR, it can run 35/// in two contexts: interpreted by LLDB as a DWARF location expression, 36/// or compiled by the JIT and inserted into the target process for 37/// execution. 38/// 39/// IRForTarget makes the second possible, by applying a series of 40/// transformations to the IR which make it relocatable. These 41/// transformations are discussed in more detail next to their relevant 42/// functions. 43//---------------------------------------------------------------------- 44class IRForTarget : public llvm::ModulePass 45{ 46public: 47 //------------------------------------------------------------------ 48 /// Constructor 49 /// 50 /// @param[in] pid 51 /// A unique identifier for this pass. I'm not sure what this does; 52 /// it just gets passed down to ModulePass's constructor. 53 /// 54 /// @param[in] decl_map 55 /// The list of externally-referenced variables for the expression, 56 /// for use in looking up globals and allocating the argument 57 /// struct. See the documentation for ClangExpressionDeclMap. 58 /// 59 /// @param[in] target_data 60 /// The data layout information for the target. This information is 61 /// used to determine the sizes of types that have been lowered into 62 /// IR types. 63 //------------------------------------------------------------------ 64 IRForTarget(const void *pid, 65 lldb_private::ClangExpressionDeclMap *decl_map, 66 const llvm::TargetData *target_data); 67 68 //------------------------------------------------------------------ 69 /// Destructor 70 //------------------------------------------------------------------ 71 ~IRForTarget(); 72 73 //------------------------------------------------------------------ 74 /// Run this IR transformer on a single module 75 /// 76 /// @param[in] M 77 /// The module to run on. This module is searched for the function 78 /// ___clang_expr, and that function is passed to the passes one by 79 /// one. 80 /// 81 /// @return 82 /// True on success; false otherwise 83 //------------------------------------------------------------------ 84 bool runOnModule(llvm::Module &M); 85 86 //------------------------------------------------------------------ 87 /// Interface stub 88 //------------------------------------------------------------------ 89 void assignPassManager(llvm::PMStack &PMS, 90 llvm::PassManagerType T = llvm::PMT_ModulePassManager); 91 92 //------------------------------------------------------------------ 93 /// Returns PMT_ModulePassManager 94 //------------------------------------------------------------------ 95 llvm::PassManagerType getPotentialPassManagerType() const; 96private: 97 //------------------------------------------------------------------ 98 /// A function-level pass to take the generated global value 99 /// ___clang_expr_result and make it into a persistent variable. 100 /// Also see ClangResultSynthesizer. 101 //------------------------------------------------------------------ 102 103 //------------------------------------------------------------------ 104 /// The top-level pass implementation 105 /// 106 /// @param[in] M 107 /// The module currently being processed. 108 /// 109 /// @param[in] F 110 /// The function currently being processed. 111 /// 112 /// @return 113 /// True on success; false otherwise 114 //------------------------------------------------------------------ 115 bool createResultVariable(llvm::Module &M, 116 llvm::Function &F); 117 118 //------------------------------------------------------------------ 119 /// A basic block-level pass to find all Objective-C method calls and 120 /// rewrite them to use sel_registerName instead of statically allocated 121 /// selectors. The reason is that the selectors are created on the 122 /// assumption that the Objective-C runtime will scan the appropriate 123 /// section and prepare them. This doesn't happen when code is copied 124 /// into the target, though, and there's no easy way to induce the 125 /// runtime to scan them. So instead we get our selectors from 126 /// sel_registerName. 127 //------------------------------------------------------------------ 128 129 //------------------------------------------------------------------ 130 /// Replace a single selector reference 131 /// 132 /// @param[in] selector_load 133 /// The load of the statically-allocated selector. 134 /// 135 /// @param[in] BB 136 /// The basic block currently being processed. 137 /// 138 /// @return 139 /// True on success; false otherwise 140 //------------------------------------------------------------------ 141 bool RewriteObjCSelector(llvm::Instruction* selector_load, 142 llvm::Module &M); 143 144 //------------------------------------------------------------------ 145 /// The top-level pass implementation 146 /// 147 /// @param[in] M 148 /// The module currently being processed. 149 /// 150 /// @param[in] BB 151 /// The basic block currently being processed. 152 /// 153 /// @return 154 /// True on success; false otherwise 155 //------------------------------------------------------------------ 156 bool rewriteObjCSelectors(llvm::Module &M, 157 llvm::BasicBlock &BB); 158 159 //------------------------------------------------------------------ 160 /// A basic block-level pass to find all newly-declared persistent 161 /// variables and register them with the ClangExprDeclMap. This 162 /// allows them to be materialized and dematerialized like normal 163 /// external variables. Before transformation, these persistent 164 /// variables look like normal locals, so they have an allocation. 165 /// This pass excises these allocations and makes references look 166 /// like external references where they will be resolved -- like all 167 /// other external references -- by resolveExternals(). 168 //------------------------------------------------------------------ 169 170 //------------------------------------------------------------------ 171 /// Handle a single allocation of a persistent variable 172 /// 173 /// @param[in] persistent_alloc 174 /// The allocation of the persistent variable. 175 /// 176 /// @param[in] M 177 /// The module currently being processed. 178 /// 179 /// @return 180 /// True on success; false otherwise 181 //------------------------------------------------------------------ 182 bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc, 183 llvm::Module &M); 184 185 //------------------------------------------------------------------ 186 /// The top-level pass implementation 187 /// 188 /// @param[in] M 189 /// The module currently being processed. 190 /// 191 /// @param[in] BB 192 /// The basic block currently being processed. 193 //------------------------------------------------------------------ 194 bool rewritePersistentAllocs(llvm::Module &M, 195 llvm::BasicBlock &BB); 196 197 //------------------------------------------------------------------ 198 /// A basic block-level pass to find all external variables and 199 /// functions used in the IR. Each found external variable is added 200 /// to the struct, and each external function is resolved in place, 201 /// its call replaced with a call to a function pointer whose value 202 /// is the address of the function in the target process. 203 //------------------------------------------------------------------ 204 205 //------------------------------------------------------------------ 206 /// Handle a single externally-defined variable 207 /// 208 /// @param[in] M 209 /// The module currently being processed. 210 /// 211 /// @param[in] V 212 /// The variable. 213 /// 214 /// @param[in] Store 215 /// True if the access is a store. 216 /// 217 /// @return 218 /// True on success; false otherwise 219 //------------------------------------------------------------------ 220 bool MaybeHandleVariable(llvm::Module &M, 221 llvm::Value *V, 222 bool Store); 223 224 //------------------------------------------------------------------ 225 /// Handle a single external function call 226 /// 227 /// @param[in] M 228 /// The module currently being processed. 229 /// 230 /// @param[in] C 231 /// The call instruction. 232 /// 233 /// @return 234 /// True on success; false otherwise 235 //------------------------------------------------------------------ 236 bool MaybeHandleCall(llvm::Module &M, 237 llvm::CallInst *C); 238 239 //------------------------------------------------------------------ 240 /// The top-level pass implementation 241 /// 242 /// @param[in] M 243 /// The module currently being processed. 244 /// 245 /// @param[in] BB 246 /// The basic block currently being processed. 247 /// 248 /// @return 249 /// True on success; false otherwise 250 //------------------------------------------------------------------ 251 bool resolveExternals(llvm::Module &M, 252 llvm::BasicBlock &BB); 253 254 //------------------------------------------------------------------ 255 /// A basic block-level pass to excise guard variables from the code. 256 /// The result for the function is passed through Clang as a static 257 /// variable. Static variables normally have guard variables to 258 /// ensure that they are only initialized once. 259 //------------------------------------------------------------------ 260 261 //------------------------------------------------------------------ 262 /// The top-level pass implementation 263 /// 264 /// @param[in] M 265 /// The module currently being processed. 266 /// 267 /// @param[in] BB 268 /// The basic block currently being processed. 269 /// 270 /// @return 271 /// True on success; false otherwise 272 //------------------------------------------------------------------ 273 bool removeGuards(llvm::Module &M, 274 llvm::BasicBlock &BB); 275 276 //------------------------------------------------------------------ 277 /// A function-level pass to make all external variable references 278 /// point at the correct offsets from the void* passed into the 279 /// function. ClangExpressionDeclMap::DoStructLayout() must be called 280 /// beforehand, so that the offsets are valid. 281 //------------------------------------------------------------------ 282 283 //------------------------------------------------------------------ 284 /// The top-level pass implementation 285 /// 286 /// @param[in] M 287 /// The module currently being processed. 288 /// 289 /// @param[in] F 290 /// The function currently being processed. 291 /// 292 /// @return 293 /// True on success; false otherwise 294 //------------------------------------------------------------------ 295 bool replaceVariables(llvm::Module &M, 296 llvm::Function &F); 297 298 lldb_private::ClangExpressionDeclMap *m_decl_map; ///< The DeclMap containing the Decls 299 const llvm::TargetData *m_target_data; ///< The TargetData for use in determining type sizes 300 llvm::Constant *m_sel_registerName; ///< The address of the function sel_registerName, cast to the appropriate function pointer type 301}; 302 303#endif 304