[llvm] r314531 - [SimplifyIndVar] Do not fail when we constant fold an IV user to ConstantPointerNull

Hongbin Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 29 09:32:12 PDT 2017


Author: ether
Date: Fri Sep 29 09:32:12 2017
New Revision: 314531

URL: http://llvm.org/viewvc/llvm-project?rev=314531&view=rev
Log:
[SimplifyIndVar] Do not fail when we constant fold an IV user to ConstantPointerNull

The type of a SCEVConstant may not match the corresponding LLVM Value.
In this case, we skip the constant folding for now.

TODO: Replace ConstantInt Zero by ConstantPointerNull

Added:
    llvm/trunk/test/Transforms/IndVarSimplify/constant-fold-1.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp
    llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp?rev=314531&r1=314530&r2=314531&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyIndVar.cpp Fri Sep 29 09:32:12 2017
@@ -546,18 +546,25 @@ bool SimplifyIndvar::foldConstantSCEV(In
 
   const Loop *L = LI->getLoopFor(I->getParent());
   S = SE->getSCEVAtScope(S, L);
+  auto *C = dyn_cast<SCEVConstant>(S);
 
-  if (auto *C = dyn_cast<SCEVConstant>(S)) {
-    I->replaceAllUsesWith(C->getValue());
-    DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
-                 << " with constant: " << *C << '\n');
-    ++NumFoldedUser;
-    Changed = true;
-    DeadInsts.emplace_back(I);
-    return true;
-  }
+  if (!C)
+    return false;
 
-  return false;
+  Constant *V = C->getValue();
+  // The SCEV will have a different type than the instruction if the instruction
+  // has a pointer type. Skip the replacement
+  // TODO: Replace ConstantInt Zero by ConstantPointerNull
+  if (V->getType() != I->getType())
+    return false;
+
+  I->replaceAllUsesWith(V);
+  DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I << " with constant: " << *C
+               << '\n');
+  ++NumFoldedUser;
+  Changed = true;
+  DeadInsts.emplace_back(I);
+  return true;
 }
 
 /// Eliminate any operation that SCEV can prove is an identity function.

Added: llvm/trunk/test/Transforms/IndVarSimplify/constant-fold-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/constant-fold-1.ll?rev=314531&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/constant-fold-1.ll (added)
+++ llvm/trunk/test/Transforms/IndVarSimplify/constant-fold-1.ll Fri Sep 29 09:32:12 2017
@@ -0,0 +1,39 @@
+; RUN: opt < %s -indvars -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @test(i64* %arg) unnamed_addr align 2 {
+bb:
+  switch i64 undef, label %bb1 [
+  ]
+
+bb1:                                              ; preds = %bb
+  br label %bb2
+
+bb2:                                              ; preds = %bb6, %bb1
+  %tmp = phi i64* [%arg, %bb1 ], [ %tmp7, %bb6 ]
+  switch i2 undef, label %bb6 [
+    i2 1, label %bb5
+    i2 -2, label %bb3
+    i2 -1, label %bb3
+  ]
+
+bb3:                                              ; preds = %bb2, %bb2
+  %tmp4 = call fastcc i32* @wobble(i64* nonnull %tmp, i32* null)
+  %tmp5 = load i32, i32* %tmp4 , align 8
+  br label %bb6
+
+bb5:                                              ; preds = %bb2
+  unreachable
+
+bb6:                                              ; preds = %bb3, %bb2
+  %tmp7 = load i64*, i64** undef, align 8
+  br label %bb2
+}
+
+declare i32* @wobble(i64*, i32* returned)
+
+; Should not fail when SCEV is fold to ConstantPointerNull
+; CHECK-LABEL: void @test
+; CHECK:         load i32, i32* %{{[a-zA-Z$._0-9]+}}

Modified: llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll?rev=314531&r1=314530&r2=314531&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll (original)
+++ llvm/trunk/test/Transforms/IndVarSimplify/constant-fold.ll Fri Sep 29 09:32:12 2017
@@ -19,7 +19,7 @@ for.end:
 }
 
 ; Should fold the condition of the select into constant
-; CHECK-LABEL: void @test
+; CHECK-LABEL: void @test0(
 ; CHECK:         icmp eq i32 0, 0
 
 define void @test1(i32* %a) {




More information about the llvm-commits mailing list