<div dir="ltr"><div><span style="font-family:arial,sans-serif;font-size:13px">@@ -1330,7 +1336,8 @@</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">   if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">     Value *Operand = BCI->getOperand(0);</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">     PointerType *OpType = cast<PointerType>(Operand-></span><span style="font-family:arial,sans-serif;font-size:13px">getType());</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">-    unsigned OffsetBits = TD->getPointerSizeInBits();</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">+    unsigned OffsetBits</span><br style="font-family:arial,sans-serif;font-size:13px">

<span style="font-family:arial,sans-serif;font-size:13px">+      = TD->getPointerSizeInBits(</span><span style="font-family:arial,sans-serif;font-size:13px">OpType-></span><span style="font-family:arial,sans-serif;font-size:13px">getPointerAddressSpace());</span><br style="font-family:arial,sans-serif;font-size:13px">

</div><div><br></div><div>getPointerTypeSizeInBits()?</div><div><br></div><div>Otherwise, LGTM.</div><div><br></div><div>-Eli</div><div><br></div>On Fri, Aug 16, 2013 at 11:27 AM, Matt Arsenault <span dir="ltr"><<a href="mailto:Matthew.Arsenault@amd.com" target="_blank">Matthew.Arsenault@amd.com</a>></span> wrote:<br>

<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Partially extracted from <a href="http://llvm-reviews.chandlerc.com/D1250" target="_blank">http://llvm-reviews.chandlerc.com/D1250</a>, partially stuff missed before<br>


<br>
<a href="http://llvm-reviews.chandlerc.com/D1429" target="_blank">http://llvm-reviews.chandlerc.com/D1429</a><br>
<br>
Files:<br>
  lib/Transforms/InstCombine/InstCombine.h<br>
  lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
  lib/Transforms/InstCombine/InstructionCombining.cpp<br>
  test/Transforms/InstCombine/cast.ll<br>
<br>
Index: lib/Transforms/InstCombine/InstCombine.h<br>
===================================================================<br>
--- lib/Transforms/InstCombine/InstCombine.h<br>
+++ lib/Transforms/InstCombine/InstCombine.h<br>
@@ -212,8 +212,8 @@<br>
   bool ShouldChangeType(Type *From, Type *To) const;<br>
   Value *dyn_castNegVal(Value *V) const;<br>
   Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const;<br>
-  Type *FindElementAtOffset(Type *Ty, int64_t Offset,<br>
-                                  SmallVectorImpl<Value*> &NewIndices);<br>
+  Type *FindElementAtOffset(Type *PtrTy, int64_t Offset,<br>
+                            SmallVectorImpl<Value*> &NewIndices);<br>
   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);<br>
<br>
   /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually<br>
Index: lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
===================================================================<br>
--- lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
+++ lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
@@ -1385,9 +1385,10 @@<br>
         GEP->accumulateConstantOffset(*TD, Offset)) {<br>
       // Get the base pointer input of the bitcast, and the type it points to.<br>
       Value *OrigBase = BCI->getOperand(0);<br>
-      Type *GEPIdxTy = OrigBase->getType()->getPointerElementType();<br>
       SmallVector<Value*, 8> NewIndices;<br>
-      if (FindElementAtOffset(GEPIdxTy, Offset.getSExtValue(), NewIndices)) {<br>
+      if (FindElementAtOffset(OrigBase->getType(),<br>
+                              Offset.getSExtValue(),<br>
+                              NewIndices)) {<br>
         // If we were able to index down into an element, create the GEP<br>
         // and bitcast the result.  This eliminates one bitcast, potentially<br>
         // two.<br>
Index: lib/Transforms/InstCombine/InstructionCombining.cpp<br>
===================================================================<br>
--- lib/Transforms/InstCombine/InstructionCombining.cpp<br>
+++ lib/Transforms/InstCombine/InstructionCombining.cpp<br>
@@ -755,19 +755,25 @@<br>
   return ReplaceInstUsesWith(I, NewPN);<br>
 }<br>
<br>
-/// FindElementAtOffset - Given a type and a constant offset, determine whether<br>
-/// or not there is a sequence of GEP indices into the type that will land us at<br>
-/// the specified offset.  If so, fill them into NewIndices and return the<br>
-/// resultant element type, otherwise return null.<br>
-Type *InstCombiner::FindElementAtOffset(Type *Ty, int64_t Offset,<br>
-                                          SmallVectorImpl<Value*> &NewIndices) {<br>
-  if (!TD) return 0;<br>
-  if (!Ty->isSized()) return 0;<br>
+/// FindElementAtOffset - Given a pointer type and a constant offset, determine<br>
+/// whether or not there is a sequence of GEP indices into the pointed type that<br>
+/// will land us at the specified offset.  If so, fill them into NewIndices and<br>
+/// return the resultant element type, otherwise return null.<br>
+Type *InstCombiner::FindElementAtOffset(Type *PtrTy, int64_t Offset,<br>
+                                        SmallVectorImpl<Value*> &NewIndices) {<br>
+  assert(PtrTy->isPtrOrPtrVectorTy());<br>
+<br>
+  if (!TD)<br>
+    return 0;<br>
+<br>
+  Type *Ty = PtrTy->getPointerElementType();<br>
+  if (!Ty->isSized())<br>
+    return 0;<br>
<br>
   // Start with the index over the outer type.  Note that the type size<br>
   // might be zero (even if the offset isn't zero) if the indexed type<br>
   // is something like [0 x {int, int}]<br>
-  Type *IntPtrTy = TD->getIntPtrType(Ty->getContext());<br>
+  Type *IntPtrTy = TD->getIntPtrType(PtrTy);<br>
   int64_t FirstIdx = 0;<br>
   if (int64_t TySize = TD->getTypeAllocSize(Ty)) {<br>
     FirstIdx = Offset/TySize;<br>
@@ -1235,7 +1241,7 @@<br>
       if (TD && SrcElTy->isArrayTy() &&<br>
           TD->getTypeAllocSize(SrcElTy->getArrayElementType()) ==<br>
           TD->getTypeAllocSize(ResElTy)) {<br>
-        Type *IdxType = TD->getIntPtrType(GEP.getContext());<br>
+        Type *IdxType = TD->getIntPtrType(GEP.getType());<br>
         Value *Idx[2] = { Constant::getNullValue(IdxType), GEP.getOperand(1) };<br>
         Value *NewGEP = GEP.isInBounds() ?<br>
           Builder->CreateInBoundsGEP(StrippedPtr, Idx, GEP.getName()) :<br>
@@ -1260,7 +1266,7 @@<br>
<br>
           // Earlier transforms ensure that the index has type IntPtrType, which<br>
           // considerably simplifies the logic by eliminating implicit casts.<br>
-          assert(Idx->getType() == TD->getIntPtrType(GEP.getContext()) &&<br>
+          assert(Idx->getType() == TD->getIntPtrType(GEP.getType()) &&<br>
                  "Index not cast to pointer width?");<br>
<br>
           bool NSW;<br>
@@ -1295,16 +1301,16 @@<br>
<br>
           // Earlier transforms ensure that the index has type IntPtrType, which<br>
           // considerably simplifies the logic by eliminating implicit casts.<br>
-          assert(Idx->getType() == TD->getIntPtrType(GEP.getContext()) &&<br>
+          assert(Idx->getType() == TD->getIntPtrType(GEP.getType()) &&<br>
                  "Index not cast to pointer width?");<br>
<br>
           bool NSW;<br>
           if (Value *NewIdx = Descale(Idx, APInt(BitWidth, Scale), NSW)) {<br>
             // Successfully decomposed Idx as NewIdx * Scale, form a new GEP.<br>
             // If the multiplication NewIdx * Scale may overflow then the new<br>
             // GEP may not be "inbounds".<br>
             Value *Off[2] = {<br>
-              Constant::getNullValue(TD->getIntPtrType(GEP.getContext())),<br>
+              Constant::getNullValue(TD->getIntPtrType(GEP.getType())),<br>
               NewIdx<br>
             };<br>
<br>
@@ -1330,7 +1336,8 @@<br>
   if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {<br>
     Value *Operand = BCI->getOperand(0);<br>
     PointerType *OpType = cast<PointerType>(Operand->getType());<br>
-    unsigned OffsetBits = TD->getPointerSizeInBits();<br>
+    unsigned OffsetBits<br>
+      = TD->getPointerSizeInBits(OpType->getPointerAddressSpace());<br>
     APInt Offset(OffsetBits, 0);<br>
     if (!isa<BitCastInst>(Operand) &&<br>
         GEP.accumulateConstantOffset(*TD, Offset) &&<br>
@@ -1359,8 +1366,7 @@<br>
       // field at Offset in 'A's type.  If so, we can pull the cast through the<br>
       // GEP.<br>
       SmallVector<Value*, 8> NewIndices;<br>
-      Type *InTy = OpType->getElementType();<br>
-      if (FindElementAtOffset(InTy, Offset.getSExtValue(), NewIndices)) {<br>
+      if (FindElementAtOffset(OpType, Offset.getSExtValue(), NewIndices)) {<br>
         Value *NGEP = GEP.isInBounds() ?<br>
           Builder->CreateInBoundsGEP(Operand, NewIndices) :<br>
           Builder->CreateGEP(Operand, NewIndices);<br>
Index: test/Transforms/InstCombine/cast.ll<br>
===================================================================<br>
--- test/Transforms/InstCombine/cast.ll<br>
+++ test/Transforms/InstCombine/cast.ll<br>
@@ -1,6 +1,6 @@<br>
 ; Tests to make sure elimination of casts is working correctly<br>
 ; RUN: opt < %s -instcombine -S | FileCheck %s<br>
-target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64"<br>
+target datalayout = "E-p:64:64:64-p1:32:32:32-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64"<br>
<br>
 @inbuf = external global [32832 x i8]           ; <[32832 x i8]*> [#uses=1]<br>
<br>
@@ -708,6 +708,19 @@<br>
 ; CHECK-NEXT: ret %s<br>
 }<br>
<br>
+define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {<br>
+; CHECK-LABEL: @test68_as1(<br>
+  %o = mul i32 %i, 12<br>
+  %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*<br>
+  %pp = getelementptr inbounds i8 addrspace(1)* %q, i32 %o<br>
+; CHECK-NEXT: getelementptr %s addrspace(1)*<br>
+  %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*<br>
+  %l = load %s addrspace(1)* %r<br>
+; CHECK-NEXT: load %s addrspace(1)*<br>
+  ret %s %l<br>
+; CHECK-NEXT: ret %s<br>
+}<br>
+<br>
 define double @test69(double *%p, i64 %i) {<br>
 ; CHECK-LABEL: @test69(<br>
   %o = shl nsw i64 %i, 3<br>
@@ -890,6 +903,20 @@<br>
 ; CHECK-NEXT: ret double<br>
 }<br>
<br>
+define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {<br>
+; CHECK-LABEL: @test80_as1(<br>
+  %tmp = mul nsw i16 %i, 8<br>
+; CHECK-NEXT: sext i16 %i to i32<br>
+  %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*<br>
+  %pp = getelementptr i8 addrspace(1)* %q, i16 %tmp<br>
+; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*<br>
+  %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*<br>
+  %l = load double addrspace(1)* %r<br>
+; CHECK-NEXT: load double addrspace(1)*<br>
+  ret double %l<br>
+; CHECK-NEXT: ret double<br>
+}<br>
+<br>
 define double @test81(double *%p, float %f) {<br>
   %i = fptosi float %f to i64<br>
   %q = bitcast double* %p to i8*<br>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div>