[llvm-branch-commits] [llvm-branch] r95997 - in /llvm/branches/Apple/Hermes: lib/Transforms/InstCombine/InstCombineCalls.cpp test/Transforms/InstCombine/objsize.ll

Eric Christopher echristo at apple.com
Fri Feb 12 10:46:41 PST 2010


Author: echristo
Date: Fri Feb 12 12:46:41 2010
New Revision: 95997

URL: http://llvm.org/viewvc/llvm-project?rev=95997&view=rev
Log:
Merge from mainline:

------------------------------------------------------------------------
r95877 | echristo | 2010-02-11 09:44:04 -0800 (Thu, 11 Feb 2010) | 5 lines

Make sure that ConstantExpr offsets also aren't off of extern
symbols.

Thanks to Duncan Sands for the testcase!

------------------------------------------------------------------------
r95846 | echristo | 2010-02-10 17:48:54 -0800 (Wed, 10 Feb 2010) | 5 lines

Add ConstantExpr handling to Intrinsic::objectsize lowering.

Update testcase accordingly now that we can optimize another
section.


Modified:
    llvm/branches/Apple/Hermes/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/branches/Apple/Hermes/test/Transforms/InstCombine/objsize.ll

Modified: llvm/branches/Apple/Hermes/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Hermes/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=95997&r1=95996&r2=95997&view=diff

==============================================================================
--- llvm/branches/Apple/Hermes/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/branches/Apple/Hermes/lib/Transforms/InstCombine/InstCombineCalls.cpp Fri Feb 12 12:46:41 2010
@@ -308,9 +308,14 @@
     Value *Op1 = II->getOperand(1);
     bool Min = (cast<ConstantInt>(II->getOperand(2))->getZExtValue() == 1);
     
+    // We need target data for just about everything so depend on it.
     if (!TD) break;
+    
+    // Get to the real allocated thing and offset as fast as possible.
     Op1 = Op1->stripPointerCasts();
     
+    // If we've stripped down to a single global variable that we
+    // can know the size of then just return that.
     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) {
       if (GV->hasDefinitiveInitializer()) {
         Constant *C = GV->getInitializer();
@@ -320,6 +325,32 @@
         Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
         return ReplaceInstUsesWith(CI, RetVal);
       }
+    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) {
+      
+      // Only handle constant GEPs here.
+      if (CE->getOpcode() != Instruction::GetElementPtr) break;
+      GEPOperator *GEP = cast<GEPOperator>(CE);
+      
+      // Make sure we're not a constant offset from an external
+      // global.
+      Value *Operand = GEP->getPointerOperand();
+      if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand))
+        if (!GV->hasDefinitiveInitializer()) break;
+      
+      // Get what we're pointing to and its size.
+      const PointerType *PT = 
+        cast<PointerType>(Operand->getType());
+      size_t Size = TD->getTypeAllocSize(PT->getElementType());
+      
+      // Get the current byte offset into the thing.
+      SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end());
+      size_t Offset = TD->getIndexedOffset(PT, &Ops[0], Ops.size());
+
+      assert(Size >= Offset);
+      
+      Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
+      return ReplaceInstUsesWith(CI, RetVal);
+      
     }
   }
   case Intrinsic::bswap:

Modified: llvm/branches/Apple/Hermes/test/Transforms/InstCombine/objsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Hermes/test/Transforms/InstCombine/objsize.ll?rev=95997&r1=95996&r2=95997&view=diff

==============================================================================
--- llvm/branches/Apple/Hermes/test/Transforms/InstCombine/objsize.ll (original)
+++ llvm/branches/Apple/Hermes/test/Transforms/InstCombine/objsize.ll Fri Feb 12 12:46:41 2010
@@ -31,10 +31,9 @@
   ret i8* %2;
 }
 
-; FIXME: Should be ret i32 0
 define i32 @f() nounwind {
 ; CHECK: @f
-; CHECK-NEXT: llvm.objectsize.i32
+; CHECK-NEXT: ret i32 0
   %1 = call i32 @llvm.objectsize.i32(i8* getelementptr ([60 x i8]* @a, i32 1, i32 0), i1 false)
   ret i32 %1
 }
@@ -49,4 +48,19 @@
   ret i1 %2
 }
 
+define void @test1(i8* %q, i32 %x) nounwind noinline {
+; CHECK: @test1
+; CHECK: objectsize.i32
+entry:
+  %0 = call i32 @llvm.objectsize.i32(i8* getelementptr inbounds ([0 x i8]* @window, i32 0, i32 10), i1 false) ; <i64> [#uses=1]
+  %1 = icmp eq i32 %0, -1                         ; <i1> [#uses=1]
+  br i1 %1, label %"47", label %"46"
+
+"46":                                             ; preds = %entry
+  unreachable
+
+"47":                                             ; preds = %entry
+  unreachable
+}
+
 declare i32 @llvm.objectsize.i32(i8*, i1) nounwind readonly
\ No newline at end of file





More information about the llvm-branch-commits mailing list