[llvm-commits] [llvm] r122950 - in /llvm/trunk: lib/Analysis/ConstantFolding.cpp test/Transforms/InstCombine/constant-fold-gep.ll

Duncan Sands baldrick at free.fr
Thu Jan 6 09:16:31 PST 2011


Hi Chris,

> ==============================================================================
> --- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
> +++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Jan  6 00:19:46 2011
> @@ -575,8 +575,26 @@
>     // If this is a constant expr gep that is effectively computing an
>     // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
>     for (unsigned i = 1; i != NumOps; ++i)
> -    if (!isa<ConstantInt>(Ops[i]))
> +    if (!isa<ConstantInt>(Ops[i])) {
> +
> +      // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
> +      // "inttoptr (sub (ptrtoint Ptr), V)"
> +      if (NumOps == 2&&
> +          cast<PointerType>(ResultTy)->getElementType()->isIntegerTy(8)) {

you could do this for other pointer types by introducing a multiplication.
But perhaps it isn't interesting?

> +        ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
> +        if (CE&&  CE->getOpcode() == Instruction::Sub&&
> +            isa<ConstantInt>(CE->getOperand(0))&&
> +            cast<ConstantInt>(CE->getOperand(0))->isZero()) {

The above two lines could be turned into: CE->getOperand(0)->isNullValue()

> +          Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());

Here you convert the pointer to an integer of the type of the sub.  I don't
think that makes sense, since can't the GEP index have any type?  For example,
suppose the type of the sub was i8, then here you convert the pointer to an i8
which is wrong.

> +          Res = ConstantExpr::getSub(Res, CE->getOperand(1));
> +          Res = ConstantExpr::getIntToPtr(Res, ResultTy);
> +          if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
> +            Res = ConstantFoldConstantExpression(ResCE, TD);
> +          return Res;
> +        }
> +      }

Ciao, Duncan.



More information about the llvm-commits mailing list