GBE: handle the first index of GEP correctly.
authorZhigang Gong <zhigang.gong@intel.com>
Tue, 7 Jan 2014 04:14:54 +0000 (12:14 +0800)
committerZhigang Gong <zhigang.gong@intel.com>
Tue, 7 Jan 2014 07:51:02 +0000 (15:51 +0800)
The first index of GEP instruction is to step over the pointer[0]
to the index. We just need to calculate the *pointer's size, and
step over *pointer's size * Index to reach the position of the
data strucutre. Then we start to iterate the composite data type.

Signed-off-by: Zhigang Gong <zhigang.gong@intel.com>
Reviewed-by: "Yang, Rong R" <rong.r.yang@intel.com>
backend/src/llvm/llvm_gen_backend.cpp
backend/src/llvm/llvm_passes.cpp

index 8130882..f178585 100644 (file)
@@ -933,17 +933,27 @@ namespace gbe
           ConstantInt* ConstOP = dyn_cast<ConstantInt>(ce->getOperand(op));
           GBE_ASSERT(ConstOP);
           TypeIndex = ConstOP->getZExtValue();
-          for(uint32_t ty_i=0; ty_i<TypeIndex; ty_i++)
-          {
-            Type* elementType = CompTy->getTypeAtIndex(ty_i);
-            uint32_t align = getAlignmentByte(unit, elementType);
+          if (op == 1) {
+            if (TypeIndex != 0) {
+              Type *elementType = (cast<PointerType>(pointer->getType()))->getElementType();
+              uint32_t elementSize = getTypeByteSize(unit, elementType);
+              uint32_t align = getAlignmentByte(unit, elementType);
+              elementSize += getPadding(elementSize, align);
+              offset += elementSize * TypeIndex;
+            }
+          } else {
+            for(uint32_t ty_i=0; ty_i<TypeIndex; ty_i++)
+            {
+              Type* elementType = CompTy->getTypeAtIndex(ty_i);
+              uint32_t align = getAlignmentByte(unit, elementType);
+              offset += getPadding(offset, align);
+              offset += getTypeByteSize(unit, elementType);
+            }
+
+            const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
             offset += getPadding(offset, align);
-            offset += getTypeByteSize(unit, elementType);
           }
 
-          const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
-          offset += getPadding(offset, align);
-
           constantOffset += offset;
           CompTy = dyn_cast<CompositeType>(CompTy->getTypeAtIndex(TypeIndex));
         }
index d31f07e..3bb6f71 100644 (file)
@@ -274,18 +274,28 @@ namespace gbe
       {
         uint32_t offset = 0;
         TypeIndex = ConstOP->getZExtValue();
-        for(uint32_t ty_i=0; ty_i<TypeIndex; ty_i++)
-        {
-          Type* elementType = CompTy->getTypeAtIndex(ty_i);
-          uint32_t align = getAlignmentByte(unit, elementType);
+        if (op == 1) {
+          if (TypeIndex != 0) {
+            Type *elementType = (cast<PointerType>(parentPointer->getType()))->getElementType();
+            uint32_t elementSize = getTypeByteSize(unit, elementType);
+            uint32_t align = getAlignmentByte(unit, elementType);
+            elementSize += getPadding(elementSize, align);
+            offset += elementSize * TypeIndex;
+          }
+        } else {
+          for(uint32_t ty_i=0; ty_i<TypeIndex; ty_i++)
+          {
+            Type* elementType = CompTy->getTypeAtIndex(ty_i);
+            uint32_t align = getAlignmentByte(unit, elementType);
+            offset += getPadding(offset, align);
+            offset += getTypeByteSize(unit, elementType);
+          }
+
+          //add getPaddingding for accessed type
+          const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
           offset += getPadding(offset, align);
-          offset += getTypeByteSize(unit, elementType);
         }
 
-        //add getPaddingding for accessed type
-        const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
-        offset += getPadding(offset, align);
-
         constantOffset += offset;
       }
       // none constant index (=> only array/verctor allowed)