<div dir="ltr">Hi,<div><br></div><div>I'm compiling a large code base that uses tagged data, with the tag in the two lowest bits.</div><div><br></div><div>I.e. ints are shifted two steps to the left and have 2 in the tag bits, pointers have 0 in the tag bits, etc.</div><div><br></div><div>When I compile the code, I notice that there are places where -O3 doesn't remove</div><div>unnecessary tag bit tests and manipulations, when they are performed with bitwise</div><div>manipulation (which is how it is implemented in the large code base I'm working with).</div><div><br></div><div>I've provided a small example below.</div><div><br></div><div>However, when I change from using 'and' and 'or' to using subtraction and addition, llvm</div><div>is able to detect and optimise the code correctly.</div><div><br></div><div>Is there perhaps an optional optimisation pass that I could run that could detect this optimisation opportunity?</div><div><br></div><div>Thanks for any ideas,</div><div>/Lars</div><br>/***************************************************/<br><div><br></div><div>/*  The two LSB of x0 are 'tag bits'  */</div><div>/*  that we want to manipulate.       */</div><div>extern long x0;</div><div><br></div><div>void go_error(void) __attribute__ ((noreturn));</div><div><br></div><div>void example_not_optimized(void)</div><div>{</div><div>  if((x0 & 3) == 2) {</div><div>    // Here the tag bits are removed and added</div><div>    // with bitwise 'and' and 'or'.</div><div>    x0 = ((x0 & ~3) | 2) + 12;</div><div>  } else {</div><div>    go_error();</div><div>  }</div><div>}</div><div><br></div><div>/*</div><div>define void @example_not_optimized() #0 {</div><div>  %1 = load i64* @x0, align 8, !tbaa !1</div><div>  %2 = and i64 %1, 3</div><div>  %3 = icmp eq i64 %2, 2</div><div>  br i1 %3, label %4, label %8</div><div><br></div><div>; <label>:4                                       ; preds = %0</div><div>  %5 = and i64 %1, -4                ; this should be optimized away</div><div>  %6 = or i64 %5, 2                  ; this should be optimized away</div><div>  %7 = add nsw i64 %6, 12</div><div>  store i64 %7, i64* @x0, align 8, !tbaa !1</div><div>  ret void</div><div><br></div><div>; <label>:8                                       ; preds = %0</div><div>  tail call void @go_error() #2</div><div>  unreachable</div><div>}</div><div>*/</div><div><br></div><div><br></div><div>void example_optimized(void)</div><div>{</div><div>  if((x0 & 3) == 2) {</div><div>    // Here the tag bits are removed and added</div><div>    // with subtraction and addition.</div><div>    x0 = (x0 - (x0 & 3) + 2) + 12;</div><div>  } else {</div><div>    go_error();</div><div>  }</div><div>}</div><div><br></div><div>/*</div><div>define void @example_optimized() #0 {</div><div>  %1 = load i64* @x0, align 8, !tbaa !1</div><div>  %2 = and i64 %1, 3</div><div>  %3 = icmp eq i64 %2, 2</div><div>  br i1 %3, label %4, label %6</div><div><br></div><div>; <label>:4                                       ; preds = %0</div><div>  %5 = add i64 %1, 12</div><div>  store i64 %5, i64* @x0, align 8, !tbaa !1</div><div>  ret void</div><div><br></div><div>; <label>:6                                       ; preds = %0</div><div>  tail call void @go_error() #2</div><div>  unreachable</div><div>}</div><div><br></div><div> */</div><div><br></div></div>