llvm.org GIT mirror llvm / 347248d
[InstSimplify] simplify power-of-2 (single bit set) sequences As discussed in PR42314: https://bugs.llvm.org/show_bug.cgi?id=42314 Improving the canonicalization for these patterns: rL363956 ...means we should adjust/enhance the related simplification. https://rise4fun.com/Alive/w1cp Name: isPow2 or zero %x = and i32 %xx, 2048 %a = add i32 %x, -1 %r = and i32 %a, %x => %r = i32 0 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363997 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjay Patel 2 months ago
2 changed file(s) with 12 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
18431843 Q.DT))
18441844 return Op1;
18451845 }
1846
1847 // This is a similar pattern used for checking if a value is a power-of-2:
1848 // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
1849 // A & (A - 1) --> 0 (if A is a power-of-2 or 0)
1850 if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
1851 isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
1852 return Constant::getNullValue(Op1->getType());
1853 if (match(Op1, m_Add(m_Specific(Op0), m_AllOnes())) &&
1854 isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
1855 return Constant::getNullValue(Op0->getType());
18461856
18471857 if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
18481858 return V;
9292
9393 define i32 @pow2_decrement(i32 %p) {
9494 ; CHECK-LABEL: @pow2_decrement(
95 ; CHECK-NEXT: [[X:%.*]] = shl i32 1, [[P:%.*]]
96 ; CHECK-NEXT: [[A:%.*]] = add i32 [[X]], -1
97 ; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], [[X]]
98 ; CHECK-NEXT: ret i32 [[R]]
95 ; CHECK-NEXT: ret i32 0
9996 ;
10097 %x = shl i32 1, %p
10198 %a = add i32 %x, -1
105102
106103 define <2 x i32> @pow2_decrement_commute_vec(<2 x i32> %p) {
107104 ; CHECK-LABEL: @pow2_decrement_commute_vec(
108 ; CHECK-NEXT: [[X:%.*]] = and <2 x i32> [[P:%.*]],
109 ; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[X]],
110 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X]], [[A]]
111 ; CHECK-NEXT: ret <2 x i32> [[R]]
105 ; CHECK-NEXT: ret <2 x i32> zeroinitializer
112106 ;
113107 %x = and <2 x i32> %p,
114108 %a = add <2 x i32> %x,