From ab51f3fa965e7d79ea2ccb2bb6d137ca16172370 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 4 Mar 2006 06:04:02 +0000 Subject: [PATCH] Canonicalize (X+C1)*C2 -> X*C2+C1*C2 This implements Transforms/InstCombine/add.ll:test31 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26519 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 6d006b1815d..c409e91ef00 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -1622,6 +1622,19 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (Op1F->getValue() == 1.0) return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' } + + if (BinaryOperator *Op0I = dyn_cast(Op0)) + if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && + isa(Op0I->getOperand(1))) { + // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. + Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0), + Op1, "tmp"); + InsertNewInstBefore(Add, I); + Value *C1C2 = ConstantExpr::getMul(Op1, + cast(Op0I->getOperand(1))); + return BinaryOperator::createAdd(Add, C1C2); + + } // Try to fold constant mul into select arguments. if (SelectInst *SI = dyn_cast(Op0)) -- 2.34.1