Index: llvm/trunk/lib/Target/X86/X86InstrCompiler.td =================================================================== --- llvm/trunk/lib/Target/X86/X86InstrCompiler.td +++ llvm/trunk/lib/Target/X86/X86InstrCompiler.td @@ -1001,6 +1001,18 @@ // DAG Pattern Matching Rules //===----------------------------------------------------------------------===// +// Use AND/OR to store 0/-1 in memory when optimizing for minsize. This saves +// binary size compared to a regular MOV, but it introduces an unnecessary +// load, so is not suitable for regular or optsize functions. +let Predicates = [OptForMinSize] in { +def : Pat<(store (i16 0), addr:$dst), (AND16mi8 addr:$dst, 0)>; +def : Pat<(store (i32 0), addr:$dst), (AND32mi8 addr:$dst, 0)>; +def : Pat<(store (i64 0), addr:$dst), (AND64mi8 addr:$dst, 0)>; +def : Pat<(store (i16 -1), addr:$dst), (OR16mi8 addr:$dst, -1)>; +def : Pat<(store (i32 -1), addr:$dst), (OR32mi8 addr:$dst, -1)>; +def : Pat<(store (i64 -1), addr:$dst), (OR64mi8 addr:$dst, -1)>; +} + // ConstantPool GlobalAddress, ExternalSymbol, and JumpTable def : Pat<(i32 (X86Wrapper tconstpool :$dst)), (MOV32ri tconstpool :$dst)>; def : Pat<(i32 (X86Wrapper tjumptable :$dst)), (MOV32ri tjumptable :$dst)>; Index: llvm/trunk/test/CodeGen/X86/store-zero-and-minus-one.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/store-zero-and-minus-one.ll +++ llvm/trunk/test/CodeGen/X86/store-zero-and-minus-one.ll @@ -0,0 +1,88 @@ +; RUN: llc -mtriple=i686-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=CHECK32 --check-prefix=CHECK +; RUN: llc -mtriple=x86_64-unknown-linux-gnu %s -o - | FileCheck %s --check-prefix=CHECK64 --check-prefix=CHECK + +define void @zero_optsize(i32* %p) optsize { +entry: + store i32 0, i32* %p + ret void + +; CHECK-LABEL: zero_optsize: +; CHECK: movl $0 +; CHECK: ret +} + +define void @minus_one_optsize(i32* %p) optsize { +entry: + store i32 -1, i32* %p + ret void + +; CHECK-LABEL: minus_one_optsize: +; CHECK: movl $-1 +; CHECK: ret +} + + +define void @zero_64(i64* %p) minsize { +entry: + store i64 0, i64* %p + ret void + +; CHECK-LABEL: zero_64: +; CHECK32: andl $0 +; CHECK32: andl $0 +; CHECK64: andq $0 +; CHECK: ret +} + +define void @zero_32(i32* %p) minsize { +entry: + store i32 0, i32* %p + ret void + +; CHECK-LABEL: zero_32: +; CHECK: andl $0 +; CHECK: ret +} + +define void @zero_16(i16* %p) minsize { +entry: + store i16 0, i16* %p + ret void + +; CHECK-LABEL: zero_16: +; CHECK: andw $0 +; CHECK: ret +} + + +define void @minus_one_64(i64* %p) minsize { +entry: + store i64 -1, i64* %p + ret void + +; CHECK-LABEL: minus_one_64: +; CHECK32: orl $-1 +; CHECK32: orl $-1 +; CHECK64: orq $-1 +; CHECK: ret +} + +define void @minus_one_32(i32* %p) minsize { +entry: + store i32 -1, i32* %p + ret void + +; CHECK-LABEL: minus_one_32: +; CHECK: orl $-1 +; CHECK: ret +} + +define void @minus_one_16(i16* %p) minsize { +entry: + store i16 -1, i16* %p + ret void + +; CHECK-LABEL: minus_one_16: +; CHECK: orw $-1 +; CHECK: ret +} Index: llvm/trunk/test/CodeGen/X86/tail-opts.ll =================================================================== --- llvm/trunk/test/CodeGen/X86/tail-opts.ll +++ llvm/trunk/test/CodeGen/X86/tail-opts.ll @@ -376,7 +376,7 @@ ; CHECK-LABEL: two_minsize: ; CHECK-NOT: XYZ ; CHECK: ret -; CHECK: movl $0, XYZ(%rip) +; CHECK: andl $0, XYZ(%rip) ; CHECK: movl $1, XYZ(%rip) ; CHECK-NOT: XYZ