[llvm-commits] [llvm] r94318 - in /llvm/trunk: lib/Target/README.txt lib/Transforms/InstCombine/InstCombineShifts.cpp test/Transforms/InstCombine/intrinsics.ll

Eli Friedman eli.friedman at gmail.com
Sat Jan 23 10:55:47 PST 2010


On Sat, Jan 23, 2010 at 10:49 AM, Chris Lattner <sabre at nondot.org> wrote:
> Author: lattner
> Date: Sat Jan 23 12:49:30 2010
> New Revision: 94318
>
> URL: http://llvm.org/viewvc/llvm-project?rev=94318&view=rev
> Log:
> implement a simple instcombine xform that has been in the
> readme forever.
>
> Modified:
>    llvm/trunk/lib/Target/README.txt
>    llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
>    llvm/trunk/test/Transforms/InstCombine/intrinsics.ll
>
> Modified: llvm/trunk/lib/Target/README.txt
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=94318&r1=94317&r2=94318&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Target/README.txt (original)
> +++ llvm/trunk/lib/Target/README.txt Sat Jan 23 12:49:30 2010
> @@ -237,24 +237,6 @@
>
>  //===---------------------------------------------------------------------===//
>
> -dag/inst combine "clz(x)>>5 -> x==0" for 32-bit x.
> -
> -Compile:
> -
> -int bar(int x)
> -{
> -  int t = __builtin_clz(x);
> -  return -(t>>5);
> -}
> -
> -to:
> -
> -_bar:   addic r3,r3,-1
> -        subfe r3,r3,r3
> -        blr
> -
> -//===---------------------------------------------------------------------===//
> -
>  quantum_sigma_x in 462.libquantum contains the following loop:
>
>       for(i=0; i<reg->size; i++)
> @@ -294,6 +276,8 @@
>
>  //===---------------------------------------------------------------------===//
>
> +[LOOP RECOGNITION]
> +
>  These idioms should be recognized as popcount (see PR1488):
>
>  unsigned countbits_slow(unsigned v) {
>
> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp?rev=94318&r1=94317&r2=94318&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (original)
> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp Sat Jan 23 12:49:30 2010
> @@ -12,6 +12,7 @@
>  //===----------------------------------------------------------------------===//
>
>  #include "InstCombine.h"
> +#include "llvm/IntrinsicInst.h"
>  #include "llvm/Support/PatternMatch.h"
>  using namespace llvm;
>  using namespace PatternMatch;
> @@ -69,10 +70,9 @@
>   if (Op1->uge(TypeBits)) {
>     if (I.getOpcode() != Instruction::AShr)
>       return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
> -    else {
> -      I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
> -      return &I;
> -    }
> +    // ashr i32 X, 32 --> ashr i32 X, 31
> +    I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
> +    return &I;
>   }
>
>   // ((X*C1) << C2) == (X * (C1 << C2))
> @@ -387,7 +387,29 @@
>  }
>
>  Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
> -  return commonShiftTransforms(I);
> +  if (Instruction *R = commonShiftTransforms(I))
> +    return R;
> +
> +  Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
> +
> +  if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1))
> +    if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
> +      // ctlz.i32(x)>>5  --> zext(x == 0)
> +      // cttz.i32(x)>>5  --> zext(x == 0)
> +      // ctpop.i32(x)>>5 --> zext(x == -1)
> +      if ((II->getIntrinsicID() == Intrinsic::ctlz ||
> +           II->getIntrinsicID() == Intrinsic::cttz ||
> +           II->getIntrinsicID() == Intrinsic::ctpop) &&
> +          (1ULL << Op1C->getZExtValue()) ==
> +            Op0->getType()->getScalarSizeInBits()) {

This shift isn't safe; the result is undefined if Op1C->getZExtValue() >= 64.

-Eli




More information about the llvm-commits mailing list