[Lldb-commits] [lldb] r197829 - Updated our IR processing to reflect best practices

Sean Callanan scallanan at apple.com
Fri Dec 20 11:55:03 PST 2013


Author: spyffe
Date: Fri Dec 20 13:55:02 2013
New Revision: 197829

URL: http://llvm.org/viewvc/llvm-project?rev=197829&view=rev
Log:
Updated our IR processing to reflect best practices
for making pointer-valued constants.

Modified:
    lldb/trunk/include/lldb/Expression/IRForTarget.h
    lldb/trunk/source/Expression/IRDynamicChecks.cpp
    lldb/trunk/source/Expression/IRForTarget.cpp

Modified: lldb/trunk/include/lldb/Expression/IRForTarget.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/IRForTarget.h?rev=197829&r1=197828&r2=197829&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/IRForTarget.h (original)
+++ lldb/trunk/include/lldb/Expression/IRForTarget.h Fri Dec 20 13:55:02 2013
@@ -30,6 +30,7 @@ namespace llvm {
     class GlobalValue;
     class GlobalVariable;
     class Instruction;
+    class IntegerType;
     class Module;
     class StoreInst;
     class DataLayout;
@@ -650,6 +651,7 @@ private:
     StaticDataAllocator                     m_data_allocator;           ///< The allocator to use for constant strings
     llvm::Constant                         *m_CFStringCreateWithBytes;  ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
     llvm::Constant                         *m_sel_registerName;         ///< The address of the function sel_registerName, cast to the appropriate function pointer type
+    llvm::IntegerType                      *m_intptr_ty;                ///< The type of an integer large enough to hold a pointer.
     lldb_private::Stream                   *m_error_stream;             ///< If non-NULL, the stream on which errors should be printed
     
     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.

Modified: lldb/trunk/source/Expression/IRDynamicChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRDynamicChecks.cpp?rev=197829&r1=197828&r2=197829&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRDynamicChecks.cpp (original)
+++ lldb/trunk/source/Expression/IRDynamicChecks.cpp Fri Dec 20 13:55:02 2013
@@ -19,6 +19,7 @@
 
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Module.h"
@@ -145,7 +146,8 @@ public:
                   DynamicCheckerFunctions &checker_functions) :
         m_module(module),
         m_checker_functions(checker_functions),
-        m_i8ptr_ty(NULL)
+        m_i8ptr_ty(NULL),
+        m_intptr_ty(NULL)
     {
     }
     
@@ -279,8 +281,6 @@ protected:
     //------------------------------------------------------------------
     llvm::Value *BuildPointerValidatorFunc(lldb::addr_t start_address)
     {
-        IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), 64);
-        
         llvm::Type *param_array[1];
         
         param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
@@ -289,7 +289,7 @@ protected:
         
         FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
         PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
-        Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
+        Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
         return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
     }
     
@@ -305,8 +305,6 @@ protected:
     //------------------------------------------------------------------
     llvm::Value *BuildObjectCheckerFunc(lldb::addr_t start_address)
     {
-        IntegerType *intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), 64);
-        
         llvm::Type *param_array[2];
         
         param_array[0] = const_cast<llvm::PointerType*>(GetI8PtrTy());
@@ -316,7 +314,7 @@ protected:
         
         FunctionType *fun_ty = FunctionType::get(llvm::Type::getVoidTy(m_module.getContext()), params, true);
         PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
-        Constant *fun_addr_int = ConstantInt::get(intptr_ty, start_address, false);
+        Constant *fun_addr_int = ConstantInt::get(GetIntptrTy(), start_address, false);
         return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
     }
     
@@ -328,6 +326,18 @@ protected:
         return m_i8ptr_ty;
     }
     
+    IntegerType *GetIntptrTy()
+    {
+        if (!m_intptr_ty)
+        {
+            llvm::DataLayout data_layout(&m_module);
+            
+            m_intptr_ty = llvm::Type::getIntNTy(m_module.getContext(), data_layout.getPointerSizeInBits());
+        }
+        
+        return m_intptr_ty;
+    }
+    
     typedef std::vector <llvm::Instruction *>   InstVector;
     typedef InstVector::iterator                InstIterator;
     
@@ -336,6 +346,7 @@ protected:
     DynamicCheckerFunctions    &m_checker_functions;    ///< The dynamic checker functions for the process
 private:
     PointerType                *m_i8ptr_ty;
+    IntegerType                *m_intptr_ty;
 };
 
 class ValidPointerChecker : public Instrumenter

Modified: lldb/trunk/source/Expression/IRForTarget.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/IRForTarget.cpp?rev=197829&r1=197828&r2=197829&view=diff
==============================================================================
--- lldb/trunk/source/Expression/IRForTarget.cpp (original)
+++ lldb/trunk/source/Expression/IRForTarget.cpp Fri Dec 20 13:55:02 2013
@@ -105,6 +105,7 @@ IRForTarget::IRForTarget (lldb_private::
     m_data_allocator(execution_unit),
     m_CFStringCreateWithBytes(NULL),
     m_sel_registerName(NULL),
+    m_intptr_ty(NULL),
     m_error_stream(error_stream),
     m_result_store(NULL),
     m_result_is_pointer(false),
@@ -285,9 +286,8 @@ llvm::Constant *
 IRForTarget::BuildFunctionPointer (llvm::Type *type,
                                    uint64_t ptr)
 {
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
     PointerType *fun_ptr_ty = PointerType::getUnqual(type);
-    Constant *fun_addr_int = ConstantInt::get(intptr_ty, ptr, false);
+    Constant *fun_addr_int = ConstantInt::get(m_intptr_ty, ptr, false);
     return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
 }
 
@@ -725,7 +725,6 @@ IRForTarget::RewriteObjCConstString (llv
     Type *ns_str_ty = ns_str->getType();
     
     Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
     Type *i32_ty = Type::getInt32Ty(m_module->getContext());
     Type *i8_ty = Type::getInt8Ty(m_module->getContext());
     
@@ -772,7 +771,7 @@ IRForTarget::RewriteObjCConstString (llv
         
         arg_type_array[0] = i8_ptr_ty;
         arg_type_array[1] = i8_ptr_ty;
-        arg_type_array[2] = intptr_ty;
+        arg_type_array[2] = m_intptr_ty;
         arg_type_array[3] = i32_ty;
         arg_type_array[4] = i8_ty;
         
@@ -782,7 +781,7 @@ IRForTarget::RewriteObjCConstString (llv
         
         // Build the constant containing the pointer to the function
         PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
-        Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
+        Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
         m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
     }
     
@@ -793,7 +792,7 @@ IRForTarget::RewriteObjCConstString (llv
                             
     Constant *alloc_arg         = Constant::getNullValue(i8_ptr_ty);
     Constant *bytes_arg         = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
-    Constant *numBytes_arg      = ConstantInt::get(intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
+    Constant *numBytes_arg      = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
     Constant *encoding_arg      = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
     Constant *isExternal_arg    = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
     
@@ -1146,9 +1145,8 @@ IRForTarget::RewriteObjCSelector (Instru
         llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
         
         // Build the constant containing the pointer to the function
-        IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
         PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
-        Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
+        Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
         m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
     }
     
@@ -1595,9 +1593,8 @@ IRForTarget::HandleSymbol (Value *symbol
         log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
     
     Type *symbol_type = symbol->getType();
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
     
-    Constant *symbol_addr_int = ConstantInt::get(intptr_ty, symbol_addr, false);
+    Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
     
     Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
     
@@ -1675,9 +1672,7 @@ IRForTarget::HandleObjCClass(Value *clas
     if (load_instructions.empty())
         return false;
     
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
-    
-    Constant *class_addr = ConstantInt::get(intptr_ty, (uint64_t)class_ptr);
+    Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
     
     for (LoadInst *load_instruction : load_instructions)
     {
@@ -2492,9 +2487,7 @@ IRForTarget::ReplaceVariables (Function
 llvm::Constant *
 IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
 {
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
-    
-    llvm::Constant *offset_int = ConstantInt::get(intptr_ty, offset);
+    llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
     
     llvm::Constant *offset_array[1];
     
@@ -2529,9 +2522,7 @@ IRForTarget::CompleteDataAllocation ()
     if (!allocation || allocation == LLDB_INVALID_ADDRESS)
         return false;
     
-    IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(), 64);
-    
-    Constant *relocated_addr = ConstantInt::get(intptr_ty, (uint64_t)allocation);
+    Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
     Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
     
     m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
@@ -2598,6 +2589,7 @@ IRForTarget::runOnModule (Module &llvm_m
     
     m_module = &llvm_module;
     m_target_data.reset(new DataLayout(m_module));
+    m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
    
     if (log)
     {
@@ -2632,13 +2624,13 @@ IRForTarget::runOnModule (Module &llvm_m
         return false;
     }
     
-    llvm::Type *intptr_ty = Type::getInt8Ty(m_module->getContext());
+    llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
     
     m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
-                                                   intptr_ty,
+                                                   int8_ty,
                                                    false /* IsConstant */,
                                                    GlobalVariable::InternalLinkage,
-                                                   Constant::getNullValue(intptr_ty),
+                                                   Constant::getNullValue(int8_ty),
                                                    "reloc_placeholder",
                                                    NULL /* InsertBefore */,
                                                    GlobalVariable::NotThreadLocal /* ThreadLocal */,





More information about the lldb-commits mailing list