From d93b8a0740c8c5aecd53a7ffff1bff58647f2e20 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 16 Apr 2018 15:19:24 +0000 Subject: [PATCH] [InstCombine] simplify getBinOpsForFactorization(); NFC llvm-svn: 330129 --- .../InstCombine/InstructionCombining.cpp | 40 ++++++++-------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index af4449a..d33fba8 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -486,37 +486,27 @@ static Value *getIdentityValue(Instruction::BinaryOps Opcode, Value *V) { return ConstantExpr::getBinOpIdentity(Opcode, V->getType()); } -/// This function factors binary ops which can be combined using distributive -/// laws. This function tries to transform 'Op' based TopLevelOpcode to enable -/// factorization e.g for ADD(SHL(X , 2), MUL(X, 5)), When this function called -/// with TopLevelOpcode == Instruction::Add and Op = SHL(X, 2), transforms -/// SHL(X, 2) to MUL(X, 4) i.e. returns Instruction::Mul with LHS set to 'X' and -/// RHS to 4. +/// This function predicates factorization using distributive laws. By default, +/// it just returns the 'Op' inputs. But for special-cases like +/// 'add(shl(X, 5), ...)', this function will have TopOpcode == Instruction::Add +/// and Op = shl(X, 5). The 'shl' is treated as the more general 'mul X, 32' to +/// allow more factorization opportunities. static Instruction::BinaryOps -getBinOpsForFactorization(Instruction::BinaryOps TopLevelOpcode, - BinaryOperator *Op, Value *&LHS, Value *&RHS) { +getBinOpsForFactorization(Instruction::BinaryOps TopOpcode, BinaryOperator *Op, + Value *&LHS, Value *&RHS) { assert(Op && "Expected a binary operator"); - LHS = Op->getOperand(0); RHS = Op->getOperand(1); - - switch (TopLevelOpcode) { - default: - return Op->getOpcode(); - - case Instruction::Add: - case Instruction::Sub: - if (Op->getOpcode() == Instruction::Shl) { - if (Constant *CST = dyn_cast(Op->getOperand(1))) { - // The multiplier is really 1 << CST. - RHS = ConstantExpr::getShl(ConstantInt::get(Op->getType(), 1), CST); - return Instruction::Mul; - } + if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) { + Constant *C; + if (match(Op, m_Shl(m_Value(), m_Constant(C)))) { + // X << C --> X * (1 << C) + RHS = ConstantExpr::getShl(ConstantInt::get(Op->getType(), 1), C); + return Instruction::Mul; } - return Op->getOpcode(); + // TODO: We can add other conversions e.g. shr => div etc. } - - // TODO: We can add other conversions e.g. shr => div etc. + return Op->getOpcode(); } /// This tries to simplify binary operations by factorizing out common terms -- 2.7.4