llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

InstCombine: SimplifyDemandedVectorElts wrongly analyzes select statements with ConstantExpr elements in the mask.

Open adibiagio opened this issue 11 years ago • 4 comments

Bugzilla Link 24922
Resolution FIXED
Resolved on Oct 06, 2015 09:22
Version trunk
OS Windows NT
Attachments Reproducible
CC @gregbedwell,@hfinkel,@RKSimon,@pogo59,@rotateright

Extended Description

Method 'Instcombiner::SimplifyDemandedVectorElts' (file InstCombineSimplifyDemanded.cpp) wrongly assumes that the vector elements of a constant select mask cannot be 'ConstantExpr' values.

Example:

;;;;;;;; test.ll ;;;;;;;;; target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"

define <2 x i64> @​foo(<2 x i64> %invec) { entry: %0 = extractelement <2 x i64> bitcast (<4 x i32><i32 15, i32 15, i32 15, i32 15> to <2 x i64>), i64 0 %1 = extractelement <2 x i64> bitcast (<4 x i32><i32 15, i32 15, i32 15, i32 15> to <2 x i64>), i64 1 %2 = icmp eq i64 %0, 0 %3 = icmp eq i64 %1, 0 %4 = insertelement <2 x i1> undef, i1 %2, i32 0 %5 = insertelement <2 x i1> %4, i1 %3, i32 1 %result = select <2 x i1> %5, <2 x i64> %invec, <2 x i64> <i64 0, i64 0> ret <2 x i64> %result }

define <2 x i64> @​bar(<2 x i64> %invec) { entry: %result = select <2 x i1> <i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 15, i32 15, i32 15> to <2 x i64>), i64 0), i64 0), i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 0, i32 15, i32 15> to <2 x i64>), i64 1), i64 0)>, <2 x i64> %invec, <2 x i64> <i64 0, i64 0> ret <2 x i64> %result } ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Both function @​foo and @​bar can be constant folded to a return of a zero vector. However, if we pass that code to opt -instcombine -S -o - we get:

define <2 x i64> @​foo(<2 x i64> %invec) { entry: ret <2 x i64> zeroinitializer }

define <2 x i64> @​bar(<2 x i64> %invec) { entry: ret <2 x i64> %invec }

If I pass '-debug' to opt, I get the following debug output:

IC: Mod = %result = select <2 x i1> <i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 15, i32 15, i32 15> to <2 x i64>), i64 0), i64 0), i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 0, i32 15, i32 15> to <2 x i64>), i64 1), i64 0)>, <2 x i64> %invec, <2 x i64> zeroinitializer New = %result = select <2 x i1> <i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 15, i32 15, i32 15> to <2 x i64>), i64 0), i64 0), i1 icmp eq (i64 extractelement (<2 x i64> bitcast (<4 x i32><i32 15, i32 0, i32 15, i32 15> to <2 x i64>), i64 1), i64 0)>, <2 x i64> %invec, <2 x i64> undef

So, the zero vector has been replaced with 'undef'. Later on, the instruction combiner erases the select statement and propagates %invec to all the users of %result.

If I run the debugger, I can see how the zero vector is replaced with 'undef' as a result of a call to 'SimplifyDemandedVectorElts' in 'InstCombiner::visitSelectInst'.

adibiagio avatar Sep 24 '15 12:09 adibiagio

Fixed at revision 249390. http://llvm.org/viewvc/llvm-project?view=revision&revision=249390

adibiagio avatar Oct 06 '15 16:10 adibiagio

A tentative patch has been uploaded here: http://reviews.llvm.org/D13219

adibiagio avatar Sep 28 '15 19:09 adibiagio

For the record, this bug was found when investigating on bug 24965.

adibiagio avatar Sep 28 '15 14:09 adibiagio

assigned to @adibiagio

adibiagio avatar Sep 24 '15 12:09 adibiagio