[PATCH] D42424: [InstCombine] Allow common type conversions to i8/i16

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 04:06:47 PST 2018


dmgreen added a comment.

OK. For some technical details. This is what we used to have heading into the recogniser (slightly edited for clarity):

  %v64 = phi i8 [ 0, %b2 ], [ %v45, %b9 ]
  %v53 = phi i16 [ %a1, %b2 ], [ %v43, %b9 ]
  %v42 = phi i8 [ %a0, %b2 ], [ %a, %b9 ]
  
  %0 = and i8 %v42, 1
  %v11 = zext i8 %0 to i32
  %1 = and i16 %v53, 1
  %v14 = zext i16 %1 to i32
  %v15 = xor i32 %v14, %v11
  
  %a = lshr i8 %v42, 1
  %v21 = icmp eq i32 %v15, 1
  %b = lshr i16 %v53, 1
  %v36 = xor i16 %b, -24575
  %v43 = select i1 %v21, i16 %v36, i16 %b
  %v45 = add nuw nsw i8 %v64, 1
  %v8 = icmp ult i8 %v45, 8
  br i1 %v8, label %b9, label %b46

This is what it is now:

  %v64 = phi i8 [ 0, %b2 ], [ %v45, %b9 ]
  %v53 = phi i16 [ %a1, %b2 ], [ %v43, %b9 ]
  %v42 = phi i8 [ %a0, %b2 ], [ %a, %b9 ]
  
  %0 = trunc i16 %v53 to i8
  %v111 = xor i8 %v42, %0
  %v15 = and i8 %v111, 1
  
  %a = lshr i8 %v42, 1
  %v21 = icmp eq i8 %v15, 0
  %b = lshr i16 %v53, 1
  %v36 = xor i16 %b, -24575
  %v43 = select i1 %v21, i16 %b, i16 %v36
  %v45 = add nuw nsw i8 %v64, 1
  %v8 = icmp ult i8 %v45, 8
  br i1 %v8, label %b9, label %b46

I managed to hackily get things working by adding Trunc as cases to isPromotableTo, commutesWithShift and keepsHighBitsZero, and then doing something like "if(isa<TruncInst>(Var)) Var = Var->getOperand(0)" in scanSelect. I can't claim these changes are complete or properly thought through or tested at-all :-/ but they did get at least this test case to work correctly again.

I was impressed that the rest of the pass just worked correctly, even with the other knock-on changes. When I was looking through it, I felt like an "is equivalent to" matcher would have been useful. Might be very hard to make in practice though.


https://reviews.llvm.org/D42424





More information about the llvm-commits mailing list