How to use ConstantFoldConstantExpression?

Hello,

   i need to fold constants, i found that a function ConstantFoldConstantExpression could be used,
however I am not able to make it fold anything. Could you please give me some advice, what I am doing wrong?

My code looks something like this:

//data layout is obtained from clang-generated code for triple arm-none-linux-gnueabi with added v32:32:32
const char* const TARGET_DATA_LAYOUT =
  "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v128:128:128-a0:0:64-n32";

TargetData TD(TARGET_DATA_LAYOUT);

void transformConstantExpr(ConstantExpr* cop)
{
   Constant* val = ConstantFoldConstantExpression(cop, &TD);
   if (val != NULL)
   {
    errs() << "**" << *cop << " to \n--" << *val << "\n";
   }
}

And this is the output i get, all constants should result in "i32 0" (at least this is what I need):

**i32 bitcast (<4 x i8> zeroinitializer to i32) to
--i32 bitcast (<4 x i8> zeroinitializer to i32)

**i32 sext (i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8) to i32) to
--i32 sext (i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8) to i32)

**i32 bitcast (<4 x i8> <i8 trunc (i32 shl (i32 sext (i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8) to i32), i32 0) to i8), i8 0, i8 0, i8 0> to i32) to
--i32 bitcast (<4 x i8> <i8 trunc (i32 shl (i32 sext (i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8) to i32), i32 0) to i8), i8 0, i8 0, i8 0> to i32)

**i32 and (i32 bitcast (<4 x i8> zeroinitializer to i32), i32 255) to
--i32 and (i32 bitcast (<4 x i8> zeroinitializer to i32), i32 255)

**i32 bitcast (<4 x i8> <i8 trunc (i32 lshr (i32 and (i32 bitcast (<4 x i8> zeroinitializer to i32), i32 255), i32 0) to i8), i8 0, i8 0, i8 0> to i32) to
--i32 bitcast (<4 x i8> <i8 trunc (i32 lshr (i32 and (i32 bitcast (<4 x i8> zeroinitializer to i32), i32 255), i32 0) to i8), i8 0, i8 0, i8 0> to i32)

**i32 bitcast (<4 x i8> <i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8), i8 0, i8 0, i8 0> to i32) to
--i32 bitcast (<4 x i8> <i8 trunc (i32 bitcast (<4 x i8> zeroinitializer to i32) to i8), i8 0, i8 0, i8 0> to i32)

**i32 bitcast (<2 x i16> zeroinitializer to i32) to
--i32 bitcast (<2 x i16> zeroinitializer to i32)

**i32 bitcast (<4 x i8> bitcast (i32 bitcast (<2 x i16> <i16 zext (i8 extractelement (<4 x i8> bitcast (i32 bitcast (<4 x i8> zeroinitializer to i32) to <4 x i8>), i32 2) to i16), i16 zext (i8 extractelement (<4 x i8> bitcast (i32 bitcast (<4 x i8> zeroinitializer to i32) to <4 x i8>), i32 3) to i16)> to i32) to <4 x i8>) to i32) to
--i32 bitcast (<4 x i8> bitcast (i32 bitcast (<2 x i16> <i16 zext (i8 extractelement (<4 x i8> bitcast (i32 bitcast (<4 x i8> zeroinitializer to i32) to <4 x i8>), i32 2) to i16), i16 zext (i8 extractelement (<4 x i8> bitcast (i32 bitcast (<4 x i8> zeroinitializer to i32) to <4 x i8>), i32 3) to i16)> to i32) to <4 x i8>) to i32)

Have I something wrong in the TargetData or do I miss something else?
I tried also big endian, but it did not help.

Thank you
   Adam Husar

I forgot to mention, that I use LLVM release 2.8, I did not try it with the latest revision, but I expect that I
am rather doing something wrong than using non-implemented functions.

Adam,

I just fixed this issue a few days ago. A version from the trunk should work for you.

Cheers,
Nadav

Also, ihusar: you don't need ConstantFoldConstantExpression() here as
they're folded automatically now.

Hi,

I forgot to mention, that I use LLVM release 2.8, I did not try it with the latest revision, but I expect that I
am rather doing something wrong than using non-implemented functions.

the constant folder has historically done a poor job of folding vector
expressions. Does it succeed in folding non-vector expressions?

Ciao, Duncan.

Hello Nadav, thank you for your quick answer.
Adam