[opaque pointer type] Allow gep_type_iterator to work with the pointee type from...
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 21 May 2015 21:12:43 +0000 (21:12 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 21 May 2015 21:12:43 +0000 (21:12 +0000)
The raw non-instruction/constant form of this is still relying on being
able to access the pointee type from a pointer type - those will be
cleaned up later. For now, just focus on the cases where the pointee
type is easily accessible.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237958 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/GetElementPtrTypeIterator.h
include/llvm/IR/Operator.h
lib/IR/Constants.cpp
lib/Target/AArch64/AArch64ISelLowering.cpp
lib/Target/Mips/MipsFastISel.cpp

index dcf8e64dbb27ffc66a1a65efe805716fa50c5294..6bba0ae29e987e180563a016fe88e7b4d8138bf4 100644 (file)
@@ -16,7 +16,9 @@
 #define LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
 
 #include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Operator.h"
 #include "llvm/IR/User.h"
+#include "llvm/ADT/PointerIntPair.h"
 
 namespace llvm {
   template<typename ItTy = User::const_op_iterator>
@@ -26,19 +28,28 @@ namespace llvm {
                           Type *, ptrdiff_t> super;
 
     ItTy OpIt;
-    Type *CurTy;
+    PointerIntPair<Type *, 1> CurTy;
+    unsigned AddrSpace;
     generic_gep_type_iterator() {}
   public:
 
     static generic_gep_type_iterator begin(Type *Ty, ItTy It) {
       generic_gep_type_iterator I;
-      I.CurTy = Ty;
+      I.CurTy.setPointer(Ty);
+      I.OpIt = It;
+      return I;
+    }
+    static generic_gep_type_iterator begin(Type *Ty, unsigned AddrSpace,
+                                           ItTy It) {
+      generic_gep_type_iterator I;
+      I.CurTy.setPointer(Ty);
+      I.CurTy.setInt(true);
+      I.AddrSpace = AddrSpace;
       I.OpIt = It;
       return I;
     }
     static generic_gep_type_iterator end(ItTy It) {
       generic_gep_type_iterator I;
-      I.CurTy = nullptr;
       I.OpIt = It;
       return I;
     }
@@ -51,11 +62,15 @@ namespace llvm {
     }
 
     Type *operator*() const {
-      return CurTy;
+      if (CurTy.getInt())
+        return CurTy.getPointer()->getPointerTo(AddrSpace);
+      return CurTy.getPointer();
     }
 
     Type *getIndexedType() const {
-      CompositeType *CT = cast<CompositeType>(CurTy);
+      if (CurTy.getInt())
+        return CurTy.getPointer();
+      CompositeType *CT = cast<CompositeType>(CurTy.getPointer());
       return CT->getTypeAtIndex(getOperand());
     }
 
@@ -66,10 +81,13 @@ namespace llvm {
     Value *getOperand() const { return *OpIt; }
 
     generic_gep_type_iterator& operator++() {   // Preincrement
-      if (CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
-        CurTy = CT->getTypeAtIndex(getOperand());
+      if (CurTy.getInt()) {
+        CurTy.setInt(false);
+      } else if (CompositeType *CT =
+                     dyn_cast<CompositeType>(CurTy.getPointer())) {
+        CurTy.setPointer(CT->getTypeAtIndex(getOperand()));
       } else {
-        CurTy = nullptr;
+        CurTy.setPointer(nullptr);
       }
       ++OpIt;
       return *this;
@@ -83,15 +101,23 @@ namespace llvm {
   typedef generic_gep_type_iterator<> gep_type_iterator;
 
   inline gep_type_iterator gep_type_begin(const User *GEP) {
-    return gep_type_iterator::begin
-      (GEP->getOperand(0)->getType()->getScalarType(), GEP->op_begin()+1);
+    auto *GEPOp = cast<GEPOperator>(GEP);
+    return gep_type_iterator::begin(
+        GEPOp->getSourceElementType(),
+        cast<PointerType>(GEPOp->getPointerOperandType()->getScalarType())
+            ->getAddressSpace(),
+        GEP->op_begin() + 1);
   }
   inline gep_type_iterator gep_type_end(const User *GEP) {
     return gep_type_iterator::end(GEP->op_end());
   }
   inline gep_type_iterator gep_type_begin(const User &GEP) {
-    return gep_type_iterator::begin
-      (GEP.getOperand(0)->getType()->getScalarType(), GEP.op_begin()+1);
+    auto &GEPOp = cast<GEPOperator>(GEP);
+    return gep_type_iterator::begin(
+        GEPOp.getSourceElementType(),
+        cast<PointerType>(GEPOp.getPointerOperandType()->getScalarType())
+            ->getAddressSpace(),
+        GEP.op_begin() + 1);
   }
   inline gep_type_iterator gep_type_end(const User &GEP) {
     return gep_type_iterator::end(GEP.op_end());
index 81eec413c845efdcf59c3a389efcedb10a4fe7c0..1b9102ecc7e431fca7b8ddd78b9ee5666613d78c 100644 (file)
@@ -18,7 +18,6 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/DerivedTypes.h"
-#include "llvm/IR/GetElementPtrTypeIterator.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Type.h"
 
@@ -447,36 +446,7 @@ public:
   /// undefined (it is *not* preserved!). The APInt passed into this routine
   /// must be at exactly as wide as the IntPtr type for the address space of the
   /// base GEP pointer.
-  bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const {
-    assert(Offset.getBitWidth() ==
-           DL.getPointerSizeInBits(getPointerAddressSpace()) &&
-           "The offset must have exactly as many bits as our pointer.");
-
-    for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
-         GTI != GTE; ++GTI) {
-      ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
-      if (!OpC)
-        return false;
-      if (OpC->isZero())
-        continue;
-
-      // Handle a struct index, which adds its field offset to the pointer.
-      if (StructType *STy = dyn_cast<StructType>(*GTI)) {
-        unsigned ElementIdx = OpC->getZExtValue();
-        const StructLayout *SL = DL.getStructLayout(STy);
-        Offset += APInt(Offset.getBitWidth(),
-                        SL->getElementOffset(ElementIdx));
-        continue;
-      }
-
-      // For array or vector indices, scale the index by the size of the type.
-      APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
-      Offset += Index * APInt(Offset.getBitWidth(),
-                              DL.getTypeAllocSize(GTI.getIndexedType()));
-    }
-    return true;
-  }
-
+  bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
 };
 
 class PtrToIntOperator
index d35372a2f8eff751f306ff2b9100417707205a97..3665d5abb41eb287adb2b5922e2826ca394e2448 100644 (file)
@@ -3050,3 +3050,33 @@ Instruction *ConstantExpr::getAsInstruction() {
     return BO;
   }
 }
+
+bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
+                                           APInt &Offset) const {
+  assert(Offset.getBitWidth() ==
+             DL.getPointerSizeInBits(getPointerAddressSpace()) &&
+         "The offset must have exactly as many bits as our pointer.");
+
+  for (gep_type_iterator GTI = gep_type_begin(this), GTE = gep_type_end(this);
+       GTI != GTE; ++GTI) {
+    ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
+    if (!OpC)
+      return false;
+    if (OpC->isZero())
+      continue;
+
+    // Handle a struct index, which adds its field offset to the pointer.
+    if (StructType *STy = dyn_cast<StructType>(*GTI)) {
+      unsigned ElementIdx = OpC->getZExtValue();
+      const StructLayout *SL = DL.getStructLayout(STy);
+      Offset += APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
+      continue;
+    }
+
+    // For array or vector indices, scale the index by the size of the type.
+    APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
+    Offset += Index * APInt(Offset.getBitWidth(),
+                            DL.getTypeAllocSize(GTI.getIndexedType()));
+  }
+  return true;
+}
index 8f7e661070f4ab02b755bef9a478b1d40f67705d..e6108c3e95e2848c3738b8d9b9f0c83513669311 100644 (file)
@@ -25,6 +25,7 @@
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/GetElementPtrTypeIterator.h"
 #include "llvm/IR/Intrinsics.h"
 #include "llvm/IR/Type.h"
 #include "llvm/Support/CommandLine.h"
index 3ad55e036f75c55c72c04b03f9a35824dc36ee6e..4faee10744b3c97df55d4c7a26d4ad847925978f 100644 (file)
@@ -13,6 +13,7 @@
 #include "llvm/CodeGen/FunctionLoweringInfo.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/IR/GetElementPtrTypeIterator.h"
 #include "llvm/IR/GlobalAlias.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/Target/TargetInstrInfo.h"