[llvm-commits] [llvm] r42864 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2007-10-10-EliminateMemCpy.ll

Chris Lattner clattner at apple.com
Fri Oct 12 10:24:00 PDT 2007


On Oct 11, 2007, at 10:46 AM, Dale Johannesen wrote:

> You aren't checking alignment when choosing the new pointer type.
> Are you sure this works on machines that don't have misaligned load/
> store?

The codegen should handle unaligned loads.  Devang is setting the  
alignment of the load correctly it appears.  If the codegen doesn't  
respect this, then it is a legalize bug :)

> In general memcpy handling is highly target specific.  I'd be wary of
> doing too
> much in target-independent code, although I think the general idea
> here is OK.

Some motivation:

There are two related issues in the mid-level optimizer.  First,  
memset/memcpy are not well understood by the aliasing machinery, so  
they can cause some pessimizations.  There is a bugzilla enhancement  
request about this.

Second, doing the lowering in the code generator misses some  
optimizations.  For example "strcpy(p, "x")" should turn into a  
single 16-bit store.  This does happen in the code generator  
currently, but then it leaves the dead "x" string in the output.   
Doing this in the optimizer allows the string to be deleted.

Still missing is the ability to tell that the load is coming from a  
constant string and using a an integer constant instead of the load.   
For example:

void foo(char *P) {
   strcpy(P, "abc");
}

produces:

@.str = internal constant [4 x i8] c"abc\00"            ; <[4 x i8]*>  
[#uses=1]

define void @foo(i8* %P) {
entry:
         %P1 = bitcast i8* %P to i32*            ; <i32*> [#uses=1]
         %tmp = load i32* bitcast ([4 x i8]* @.str to i32*), align  
1            ; <i32> [#uses=1]
         store i32 %tmp, i32* %P1, align 1
         ret void
}

Instcombine should now be taught that:

@.str = internal constant [4 x i8] c"abc\00"            ; <[4 x i8]*>  
[#uses=1]
..
         %tmp = load i32* bitcast ([4 x i8]* @.str to i32*), align  
1            ; <i32> [#uses=1]

can always be replaced with the corresponding integer immediate  
(independent of memcpy lowering).  This will make the string dead.

-Chris



More information about the llvm-commits mailing list