[SLSR] garbage-collect unused instructions
authorJingyue Wu <jingyue@google.com>
Tue, 21 Apr 2015 19:56:18 +0000 (19:56 +0000)
committerJingyue Wu <jingyue@google.com>
Tue, 21 Apr 2015 19:56:18 +0000 (19:56 +0000)
Summary:
After we rewrite a candidate, the instructions used by the old form may
become unused. This patch cleans up these unused instructions so that we
needn't run DCE after SLSR.

Test Plan: removed -dce in all the SLSR tests

Reviewers: broune, meheff

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D9101

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235410 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll
test/Transforms/StraightLineStrengthReduce/slsr-add.ll
test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
test/Transforms/StraightLineStrengthReduce/slsr-mul.ll

index 2fc93681e9835ddb3823d6c0dbcb2b0d6085a1ad..afe0c9d2fc4cf68ab0ae02277e46f9f72e2ad182 100644 (file)
@@ -68,6 +68,7 @@
 #include "llvm/IR/PatternMatch.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Local.h"
 
 using namespace llvm;
 using namespace PatternMatch;
@@ -599,9 +600,14 @@ void StraightLineStrengthReduce::rewriteCandidateWithBasis(
   switch (C.CandidateKind) {
   case Candidate::Add:
   case Candidate::Mul:
+    // C = Basis + Bump
     if (BinaryOperator::isNeg(Bump)) {
+      // If Bump is a neg instruction, emit C = Basis - (-Bump).
       Reduced =
           Builder.CreateSub(Basis.Ins, BinaryOperator::getNegArgument(Bump));
+      // We only use the negative argument of Bump, and Bump itself may be
+      // trivially dead.
+      RecursivelyDeleteTriviallyDeadInstructions(Bump);
     } else {
       Reduced = Builder.CreateAdd(Basis.Ins, Bump);
     }
@@ -637,7 +643,6 @@ void StraightLineStrengthReduce::rewriteCandidateWithBasis(
   };
   Reduced->takeName(C.Ins);
   C.Ins->replaceAllUsesWith(Reduced);
-  C.Ins->dropAllReferences();
   // Unlink C.Ins so that we can skip other candidates also corresponding to
   // C.Ins. The actual deletion is postponed to the end of runOnFunction.
   C.Ins->removeFromParent();
@@ -670,8 +675,13 @@ bool StraightLineStrengthReduce::runOnFunction(Function &F) {
   }
 
   // Delete all unlink instructions.
-  for (auto I : UnlinkedInstructions) {
-    delete I;
+  for (auto *UnlinkedInst : UnlinkedInstructions) {
+    for (unsigned I = 0, E = UnlinkedInst->getNumOperands(); I != E; ++I) {
+      Value *Op = UnlinkedInst->getOperand(I);
+      UnlinkedInst->setOperand(I, nullptr);
+      RecursivelyDeleteTriviallyDeadInstructions(Op);
+    }
+    delete UnlinkedInst;
   }
   bool Ret = !UnlinkedInstructions.empty();
   UnlinkedInstructions.clear();
index e2201ce23f6b1c99cd58943df624143ae9df39d0..f11cbc5897a6163b9297278ce2b7743964d3f19c 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -slsr -gvn -dce -S | FileCheck %s
+; RUN: opt < %s -slsr -gvn -S | FileCheck %s
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
index 4c79be0577e326edc6492d37c6076d08457c58d3..e25ddc2888a3e4f05fe0a964a16d28e10b9479a6 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -slsr -gvn -dce -S | FileCheck %s
+; RUN: opt < %s -slsr -gvn -S | FileCheck %s
 
 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
 
index 3944739aeaa040f6105dbd8ee77969ffe644800b..bd92780a036c783ef68861425420557f5e71ff7c 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -slsr -gvn -dce -S | FileCheck %s
+; RUN: opt < %s -slsr -gvn -S | FileCheck %s
 
 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
 
index 1c7333dab9feee7b9c713ec212e9878729a30475..97e68d5bf51f1e5e7a46978e1912f447dbdf4d85 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: opt < %s -slsr -gvn -dce -S | FileCheck %s
+; RUN: opt < %s -slsr -gvn -S | FileCheck %s
 
 target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"