[llvm] r235410 - [SLSR] garbage-collect unused instructions

Jingyue Wu jingyue at google.com
Tue Apr 21 12:56:19 PDT 2015


Author: jingyue
Date: Tue Apr 21 14:56:18 2015
New Revision: 235410

URL: http://llvm.org/viewvc/llvm-project?rev=235410&view=rev
Log:
[SLSR] garbage-collect unused instructions

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

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

Modified: llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp?rev=235410&r1=235409&r2=235410&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp Tue Apr 21 14:56:18 2015
@@ -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::rewrite
   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::rewrite
   };
   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::runOnFu
   }
 
   // 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();

Modified: llvm/trunk/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll?rev=235410&r1=235409&r2=235410&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll (original)
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/X86/no-slsr.ll Tue Apr 21 14:56:18 2015
@@ -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"

Modified: llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-add.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-add.ll?rev=235410&r1=235409&r2=235410&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-add.ll (original)
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-add.ll Tue Apr 21 14:56:18 2015
@@ -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"
 

Modified: llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll?rev=235410&r1=235409&r2=235410&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll (original)
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-gep.ll Tue Apr 21 14:56:18 2015
@@ -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"
 

Modified: llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll?rev=235410&r1=235409&r2=235410&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll (original)
+++ llvm/trunk/test/Transforms/StraightLineStrengthReduce/slsr-mul.ll Tue Apr 21 14:56:18 2015
@@ -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"
 





More information about the llvm-commits mailing list