diff options
author | Chris Lattner <sabre@nondot.org> | 2007-04-13 05:04:18 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-04-13 05:04:18 +0000 |
commit | e71f1447f79fe2d92eb7353cb25edca57856863e (patch) | |
tree | a93b8a11091194bf90756a6506422da2329a1bf3 /llvm/lib/Analysis | |
parent | c821a9ac44f7c8cd8f28f25f685ec94eae4a5ecf (diff) | |
download | bcm5719-llvm-e71f1447f79fe2d92eb7353cb25edca57856863e.tar.gz bcm5719-llvm-e71f1447f79fe2d92eb7353cb25edca57856863e.zip |
CSE simple binary expressions when they are inserted. This makes LSR produce
less huge code that needs to be cleaned up by sdisel.
llvm-svn: 35959
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r-- | llvm/lib/Analysis/ScalarEvolutionExpander.cpp | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp index a50d572f68a..a9b88c1155e 100644 --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -68,6 +68,25 @@ Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, return CastInst::create(opcode, V, Ty, V->getName(), IP); } +/// InsertBinop - Insert the specified binary operator, doing a small amount +/// of work to avoid inserting an obviously redundant operation. +Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, + Value *RHS, Instruction *InsertPt) { + // Do a quick scan to see if we have this binop nearby. If so, reuse it. + unsigned ScanLimit = 6; + for (BasicBlock::iterator IP = InsertPt, E = InsertPt->getParent()->begin(); + ScanLimit; --IP, --ScanLimit) { + if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP)) + if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS && + BinOp->getOperand(1) == RHS) + return BinOp; + if (IP == E) break; + } + + // If we don't have + return BinaryOperator::create(Opcode, LHS, RHS, "tmp.", InsertPt); +} + Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { const Type *Ty = S->getType(); int FirstOp = 0; // Set if we should emit a subtract. @@ -80,11 +99,12 @@ Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { // Emit a bunch of multiply instructions for (; i >= FirstOp; --i) - V = BinaryOperator::createMul(V, expandInTy(S->getOperand(i), Ty), - "tmp.", InsertPt); + V = InsertBinop(Instruction::Mul, V, expandInTy(S->getOperand(i), Ty), + InsertPt); // -1 * ... ---> 0 - ... if (FirstOp == 1) - V = BinaryOperator::createNeg(V, "tmp.", InsertPt); + V = InsertBinop(Instruction::Sub, Constant::getNullValue(V->getType()), V, + InsertPt); return V; } @@ -103,7 +123,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty); // FIXME: look for an existing add to use. - return BinaryOperator::createAdd(Rest, Start, "tmp.", InsertPt); + return InsertBinop(Instruction::Add, Rest, Start, InsertPt); } // {0,+,1} --> Insert a canonical induction variable into the loop! @@ -164,7 +184,7 @@ Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { } } - return BinaryOperator::createMul(I, F, "tmp.", MulInsertPt); + return InsertBinop(Instruction::Mul, I, F, MulInsertPt); } // If this is a chain of recurrences, turn it into a closed form, using the |