diff options
author | Chris Lattner <sabre@nondot.org> | 2005-05-07 04:24:13 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2005-05-07 04:24:13 +0000 |
commit | cea579932d97d07ec7fc982684dc77273219eb9a (patch) | |
tree | 3c619fbf9ea93522a7d1b58433dd724c9316e477 /llvm/lib/Transforms | |
parent | 30555b0d1439e59d1d5b8086d1583f11cce85621 (diff) | |
download | llvm-cea579932d97d07ec7fc982684dc77273219eb9a.zip llvm-cea579932d97d07ec7fc982684dc77273219eb9a.tar.gz llvm-cea579932d97d07ec7fc982684dc77273219eb9a.tar.bz2 |
Convert shifts to muls to assist reassociation. This implements
Reassociate/shifttest.ll
llvm-svn: 21761
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r-- | llvm/lib/Transforms/Scalar/Reassociate.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp index d91b24f..d00423e 100644 --- a/llvm/lib/Transforms/Scalar/Reassociate.cpp +++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp @@ -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 @@ static Instruction *BreakUpSubtract(Instruction *Sub) { 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 @@ bool Reassociate::ReassociateBB(BasicBlock *BB) { 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. |