Promote constant expressions to registers

Hello,

Is there a way to generate LLVM IR without constant expressions by
promoting constant expressions to registers?

e.g.:
  %tmp1 = tail call i8* @foo(i8* bitcast (i32* @x to i8*), i8* %tmp, i64 4) #1
would be converted to:
  %tmp1 = bitcast i32* @x to i8*
  %tmp2 = tail call i8* @foo(i8* %tmp1, i8* %tmp, i64 4) #1

I find quite inconvenient and redundant to maintain constant
expressions when they are a subset of the instruction set. Are
constant expressions being highly exploited for a particular set of
optimizations?

Regards,
Marcelo

Hello,

Is there a way to generate LLVM IR without constant expressions by
promoting constant expressions to registers?

e.g.:
   %tmp1 = tail call i8* @foo(i8* bitcast (i32* @x to i8*), i8* %tmp, i64 4) #1
would be converted to:
   %tmp1 = bitcast i32* @x to i8*
   %tmp2 = tail call i8* @foo(i8* %tmp1, i8* %tmp, i64 4) #1

I find quite inconvenient and redundant to maintain constant
expressions when they are a subset of the instruction set. Are
constant expressions being highly exploited for a particular set of
optimizations?

Constant expressions, I believe, are used by the optimizations and code generators to create more efficient code. The best way to think of them is as symbolic constants.

SAFECode has a pass that can convert various constant expressions into LLVM instructions. It is the BreakConstantGEPs pass (http://llvm.org/viewvc/llvm-project/safecode/branches/release_32/lib/ArrayBoundChecks/BreakConstantGEPs.cpp?view=log). The URL points to the version in the release_32 branch that works with LLVM 3.2. The version in SAFECode trunk should work with LLVM 3.3, but I haven't tested it much myself.

In my experience, converting constant expressions to instructions hurts the performance of the resulting code. Therefore, you should avoid it if at all possible. If you must transform a constant expression to an instruction, you should convert as few of them as possible (which is an enhancement that I need to make to SAFECode at some point).

-- John T.