[llvm-commits] [dragonegg] r97804 - /dragonegg/trunk/llvm-backend.cpp

Duncan Sands baldrick at free.fr
Fri Mar 5 05:05:53 PST 2010


Author: baldrick
Date: Fri Mar  5 07:05:53 2010
New Revision: 97804

URL: http://llvm.org/viewvc/llvm-project?rev=97804&view=rev
Log:
When performing a thunk, use inbounds GEP rather than ptrtoint-add-inttoptr.
This should be correct because (by definition) all pointer movement is inside
the underlying objects, and may result in better code. 

Modified:
    dragonegg/trunk/llvm-backend.cpp

Modified: dragonegg/trunk/llvm-backend.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-backend.cpp?rev=97804&r1=97803&r2=97804&view=diff
==============================================================================
--- dragonegg/trunk/llvm-backend.cpp (original)
+++ dragonegg/trunk/llvm-backend.cpp Fri Mar  5 07:05:53 2010
@@ -1620,29 +1620,30 @@
 static Value *ApplyVirtualOffset(Value *This, HOST_WIDE_INT virtual_value,
                                  LLVMBuilder &Builder) {
   LLVMContext &Context = getGlobalContext();
+  const Type *BytePtrTy = Type::getInt8PtrTy(Context); // i8*
+  const Type *HandleTy = BytePtrTy->getPointerTo(); // i8**
   const Type *IntPtrTy = TheTarget->getTargetData()->getIntPtrType(Context);
 
   // The vptr is always at offset zero in the object.
-  const Type *HandleTy = Type::getInt8PtrTy(Context)->getPointerTo();
-  Value *VPtr = Builder.CreateBitCast(This, HandleTy->getPointerTo());
+  Value *VPtr = Builder.CreateBitCast(This, HandleTy->getPointerTo()); // i8***
 
   // Form the vtable address.
-  Value *VTableAddr = Builder.CreateLoad(VPtr);
+  Value *VTableAddr = Builder.CreateLoad(VPtr); // i8**
 
   // Find the entry with the vcall offset.
-  VTableAddr = Builder.CreatePtrToInt(VTableAddr, IntPtrTy);
   Value *VOffset = ConstantInt::get(IntPtrTy, virtual_value);
-  VTableAddr = Builder.CreateNSWAdd(VTableAddr, VOffset);
-  VTableAddr = Builder.CreateIntToPtr(VTableAddr, HandleTy);
+  VTableAddr = Builder.CreateBitCast(VTableAddr, BytePtrTy);
+  VTableAddr = Builder.CreateInBoundsGEP(VTableAddr, VOffset);
+  VTableAddr = Builder.CreateBitCast(VTableAddr, HandleTy); // i8**
 
   // Get the offset itself.
-  Value *VCallOffset = Builder.CreateLoad(VTableAddr);
+  Value *VCallOffset = Builder.CreateLoad(VTableAddr); // i8*
   VCallOffset = Builder.CreatePtrToInt(VCallOffset, IntPtrTy);
 
   // Adjust the 'this' pointer.
-  Value *Adjusted = Builder.CreatePtrToInt(This, IntPtrTy);
-  Adjusted = Builder.CreateNSWAdd(Adjusted, VCallOffset);
-  return Builder.CreateIntToPtr(Adjusted, This->getType());
+  Value *Adjusted = Builder.CreateBitCast(This, BytePtrTy);
+  Adjusted = Builder.CreateInBoundsGEP(Adjusted, VCallOffset);
+  return Builder.CreateBitCast(Adjusted, This->getType());
 }
 
 /// emit_thunk - Turn a thunk into LLVM IR.
@@ -1668,6 +1669,7 @@
   bool ThisAdjusting = node->thunk.this_adjusting;
 
   LLVMContext &Context = getGlobalContext();
+  const Type *BytePtrTy = Type::getInt8Ty(Context)->getPointerTo();
   const Type *IntPtrTy = TheTarget->getTargetData()->getIntPtrType(Context);
   LLVMBuilder Builder(Context, *TheFolder);
   Builder.SetInsertPoint(BasicBlock::Create(Context, "entry", Thunk));
@@ -1694,10 +1696,10 @@
 
     // Adjust 'this' according to the thunk offsets.  First, the fixed offset.
     if (node->thunk.fixed_offset) {
-      This = Builder.CreatePtrToInt(This, IntPtrTy);
       Value *Offset = ConstantInt::get(IntPtrTy, node->thunk.fixed_offset);
-      This = Builder.CreateNSWAdd(This, Offset);
-      This = Builder.CreateIntToPtr(This, AI->getType());
+      This = Builder.CreateBitCast(This, BytePtrTy);
+      This = Builder.CreateInBoundsGEP(This, Offset);
+      This = Builder.CreateBitCast(This, AI->getType());
     }
 
     // Then by the virtual offset, if any.
@@ -1746,10 +1748,10 @@
 
   // Then move 'this' by the fixed offset.
   if (node->thunk.fixed_offset) {
-    RetVal = Builder.CreatePtrToInt(RetVal, IntPtrTy);
     Value *Offset = ConstantInt::get(IntPtrTy, node->thunk.fixed_offset);
-    RetVal = Builder.CreateNSWAdd(RetVal, Offset);
-    RetVal = Builder.CreateIntToPtr(RetVal, Thunk->getReturnType());
+    RetVal = Builder.CreateBitCast(RetVal, BytePtrTy);
+    RetVal = Builder.CreateInBoundsGEP(RetVal, Offset);
+    RetVal = Builder.CreateBitCast(RetVal, Thunk->getReturnType());
   }
 
   // Return the adjusted value.





More information about the llvm-commits mailing list