[PATCH] Handle big index in getelementptr instruction

Reid Kleckner rnk at google.com
Tue Mar 10 12:56:44 PDT 2015


REPOSITORY
  rL LLVM

================
Comment at: lib/CodeGen/SelectionDAG/FastISel.cpp:500
@@ -499,3 +499,3 @@
     if (auto *StTy = dyn_cast<StructType>(Ty)) {
-      unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
+      auto Field = cast<ConstantInt>(Idx)->getZExtValue();
       if (Field) {
----------------
We usually don't use auto when the type is non-obvious. uint64_t here would help readability.

================
Comment at: lib/CodeGen/SelectionDAG/FastISel.cpp:521
@@ -520,3 +520,3 @@
         // N = N + Offset
-        TotalOffs +=
-            DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
+        auto I = CI->getValue().sextOrTrunc(sizeof(TotalOffs)*8).getSExtValue();
+        TotalOffs += DL.getTypeAllocSize(Ty) * I;
----------------
Don't shadow the outer 'I' variable.

================
Comment at: lib/CodeGen/SelectionDAG/FastISel.cpp:521
@@ -520,3 +520,3 @@
         // N = N + Offset
-        TotalOffs +=
-            DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
+        auto I = CI->getValue().sextOrTrunc(sizeof(TotalOffs)*8).getSExtValue();
+        TotalOffs += DL.getTypeAllocSize(Ty) * I;
----------------
rnk wrote:
> Don't shadow the outer 'I' variable.
This sizeof computation doesn't get PtrSize. LLVM is a cross compiler. You can compute it out of the loop.

================
Comment at: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:3432-3442
@@ -3431,20 +3431,13 @@
       Ty = cast<SequentialType>(Ty)->getElementType();
+      auto PtrTy = DAG.getTargetLoweringInfo().getPointerTy(AS);
+      auto PtrSize = PtrTy.getSizeInBits();
+      auto ElementSize = APInt{PtrSize, DL->getTypeAllocSize(Ty)};
 
       // If this is a constant subscript, handle it quickly.
-      const TargetLowering &TLI = DAG.getTargetLoweringInfo();
-      if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
-        if (CI->isZero()) continue;
-        uint64_t Offs =
-            DL->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
-        SDValue OffsVal;
-        EVT PTy = TLI.getPointerTy(AS);
-        unsigned PtrBits = PTy.getSizeInBits();
-        if (PtrBits < 64)
-          OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
-                                DAG.getConstant(Offs, MVT::i64));
-        else
-          OffsVal = DAG.getConstant(Offs, PTy);
-
-        N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
-                        OffsVal);
+      if (auto CI = dyn_cast<ConstantInt>(Idx)) {
+        if (CI->isZero())
+          continue;
+        auto Offs = ElementSize * CI->getValue().sextOrTrunc(PtrSize);
+        auto OffsVal = DAG.getConstant(Offs, PtrTy);
+        N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, OffsVal);
         continue;
----------------
Too much auto is bad for readability.

http://reviews.llvm.org/D8219

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the llvm-commits mailing list