[LLVMdev] How to use ConstantFoldConstantExpression?

ihusar ihusar at fit.vutbr.cz
Tue Feb 15 05:09:57 PST 2011


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



More information about the llvm-dev mailing list