[llvm-commits] CVS: llvm/lib/Transforms/Scalar/Reassociate.cpp

Chris Lattner lattner at cs.uiuc.edu
Fri May 6 21:24:27 PDT 2005



Changes in directory llvm/lib/Transforms/Scalar:

Reassociate.cpp updated: 1.37 -> 1.38
---
Log message:

Convert shifts to muls to assist reassociation.  This implements
Reassociate/shifttest.ll


---
Diffs of the changes:  (+27 -2)

 Reassociate.cpp |   29 +++++++++++++++++++++++++++--
 1 files changed, 27 insertions(+), 2 deletions(-)


Index: llvm/lib/Transforms/Scalar/Reassociate.cpp
diff -u llvm/lib/Transforms/Scalar/Reassociate.cpp:1.37 llvm/lib/Transforms/Scalar/Reassociate.cpp:1.38
--- llvm/lib/Transforms/Scalar/Reassociate.cpp:1.37	Fri May  6 23:08:02 2005
+++ llvm/lib/Transforms/Scalar/Reassociate.cpp	Fri May  6 23:24:13 2005
@@ -22,11 +22,11 @@
 
 #define DEBUG_TYPE "reassociate"
 #include "llvm/Transforms/Scalar.h"
+#include "llvm/Constants.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
-#include "llvm/Type.h"
 #include "llvm/Pass.h"
-#include "llvm/Constant.h"
+#include "llvm/Type.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/ADT/PostOrderIterator.h"
@@ -243,6 +243,25 @@
   return New;
 }
 
+/// ConvertShiftToMul - If this is a shift of a reassociable multiply or is used
+/// by one, change this into a multiply by a constant to assist with further
+/// reassociation.
+static Instruction *ConvertShiftToMul(Instruction *Shl) {
+  if (!isReassociableOp(Shl->getOperand(0), Instruction::Mul) &&
+      !(Shl->hasOneUse() && isReassociableOp(Shl->use_back(),Instruction::Mul)))
+    return 0;
+
+  Constant *MulCst = ConstantInt::get(Shl->getType(), 1);
+  MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1)));
+
+  std::string Name = Shl->getName();  Shl->setName("");
+  Instruction *Mul = BinaryOperator::createMul(Shl->getOperand(0), MulCst,
+                                               Name, Shl);
+  Shl->replaceAllUsesWith(Mul);
+  Shl->eraseFromParent();
+  return Mul;
+}
+
 
 /// ReassociateBB - Inspect all of the instructions in this basic block,
 /// reassociating them as we go.
@@ -256,6 +275,12 @@
         Changed = true;
         BI = NI;
       }
+    if (BI->getOpcode() == Instruction::Shl &&
+        isa<ConstantInt>(BI->getOperand(1)))
+      if (Instruction *NI = ConvertShiftToMul(BI)) {
+        Changed = true;
+        BI = NI;
+      }
 
     // If this instruction is a commutative binary operator, and the ranks of
     // the two operands are sorted incorrectly, fix it now.






More information about the llvm-commits mailing list