SimplifyCFG: GEPs with just one non-constant index are also cheap.
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 24 Feb 2011 23:26:09 +0000 (23:26 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 24 Feb 2011 23:26:09 +0000 (23:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126452 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/SimplifyCFG.cpp
test/Transforms/SimplifyCFG/select-gep.ll

index c6708857cb5601255cf26295524e527e004f8d4d..3968d6e8d3bb10e745b95fb641e7e516bdca1fa9 100644 (file)
@@ -247,11 +247,13 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
     if (PBB->getFirstNonPHIOrDbg() != I)
       return false;
     break;
     if (PBB->getFirstNonPHIOrDbg() != I)
       return false;
     break;
-  case Instruction::GetElementPtr:
-    // GEPs are cheap if all indices are constant.
-    if (!cast<GetElementPtrInst>(I)->hasAllConstantIndices())
+  case Instruction::GetElementPtr: {
+    // GEPs are cheap if all indices are constant or if there's only one index.
+    GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
+    if (!GEP->hasAllConstantIndices() && GEP->getNumIndices() > 1)
       return false;
     break;
       return false;
     break;
+  }
   case Instruction::Add:
   case Instruction::Sub:
   case Instruction::And:
   case Instruction::Add:
   case Instruction::Sub:
   case Instruction::And:
index 6330ccdb291432e2fdf88bb4c4c0ae40807896bf..98650f0866395502a26d3658f27bcf3dde9059ec 100644 (file)
@@ -1,13 +1,13 @@
 ; RUN: opt -S -simplifycfg %s | FileCheck %s
 
 ; RUN: opt -S -simplifycfg %s | FileCheck %s
 
-define i8* @test1(i8* %x) nounwind {
+define i8* @test1(i8* %x, i64 %y) nounwind {
 entry:
   %tmp1 = load i8* %x, align 1
   %cmp = icmp eq i8 %tmp1, 47
   br i1 %cmp, label %if.then, label %if.end
 
 if.then:
 entry:
   %tmp1 = load i8* %x, align 1
   %cmp = icmp eq i8 %tmp1, 47
   br i1 %cmp, label %if.then, label %if.end
 
 if.then:
-  %incdec.ptr = getelementptr inbounds i8* %x, i64 1
+  %incdec.ptr = getelementptr inbounds i8* %x, i64 %y
   br label %if.end
 
 if.end:
   br label %if.end
 
 if.end:
@@ -18,3 +18,23 @@ if.end:
 ; CHECK: %x.addr = select i1 %cmp, i8* %incdec.ptr, i8* %x
 ; CHECK: ret i8* %x.addr
 }
 ; CHECK: %x.addr = select i1 %cmp, i8* %incdec.ptr, i8* %x
 ; CHECK: ret i8* %x.addr
 }
+
+%ST = type { i8, i8 }
+
+define i8* @test2(%ST* %x, i8* %y) nounwind {
+entry:
+  %cmp = icmp eq %ST* %x, null
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  %incdec.ptr = getelementptr %ST* %x, i32 0, i32 1
+  br label %if.end
+
+if.end:
+  %x.addr = phi i8* [ %incdec.ptr, %if.then ], [ %y, %entry ]
+  ret i8* %x.addr
+
+; CHECK: @test2
+; CHECK: %x.addr = select i1 %cmp, i8* %incdec.ptr, i8* %y
+; CHECK: ret i8* %x.addr
+}