[llvm-dev] How to add optimizations to InstCombine correctly?

Haidl, Michael via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 13 10:01:52 PDT 2017


Hi,

I am working on PR34474 and try to add a new optimization to 
InstCombine. Like in other parts of the visitMul function I add a Shl 
through the IR builder and create a new BinaryOp which I return from 
visitMul. If I understand correctly the new BinaryOp returned from 
visitMul should replace the original Instruction in the Worklist. 
However, I end up in an infinite loop and the Instruction I try to 
replace gets scheduled again and again. What is wrong in my code?

// Replace X * (2^C+/-1) with (X << C) -/+ X
APInt Plus1 = *IVal + 1;
APInt Minus1 = *IVal - 1;
int isPow2 = Plus1.isPowerOf2() ? 1 : Minus1.isPowerOf2() ? -1 : 0;

if (isPow2) {
   APInt &Pow2 = isPow2 > 0 ? Plus1 : Minus1;
   Value *Shl = Builder.CreateShl(Op0, Pow2.logBase2());
   return BinaryOperator::Create(isPow2 > 0 ? BinaryOperator::Sub : 
BinaryOperator::Add, Shl, Op0);
}

Thanks,
Michael


More information about the llvm-dev mailing list