[llvm] [DeadStoreElimination] Optimize tautological assignments (PR #75744)

Shreyansh Chouhan via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 11 17:41:26 PST 2024


BK1603 wrote:

There are two issues with the code.
1. The marked store in the following code is getting optimized, which should not happen? (As per my expectations, the match should fail in this case and we should exit the optimization function immediately.) I am unable to reproduce this using a simpler test case.
```32:                                               ; preds = %31, %26
  %33 = getelementptr %"struct.llvm::RAGreedy::GlobalSplitCandidate", ptr %19, i64 -1, i32 2
  %34 = getelementptr %"struct.llvm::RAGreedy::GlobalSplitCandidate", ptr %19, i64 -1, i32 2, i32 1
  store ptr null, ptr %34, align 8, !tbaa !550
  %35 = load ptr, ptr %33, align 8, !tbaa !552
  %36 = icmp eq ptr %35, null
  br i1 %36, label %41, label %37

37:                                               ; preds = %32
  %38 = getelementptr inbounds %"class.llvm::InterferenceCache::Entry", ptr %35, i64 0, i32 2
  %39 = load i32, ptr %38, align 8, !tbaa !494
  %40 = add i32 %39, -1
  store i32 %40, ptr %38, align 8, !tbaa !494  
  br label %41

41:                                               ; preds = %37, %32
  store ptr null, ptr %33, align 8, !tbaa !552            ; >>>>>>>>>>>>>>>>>>>>>>>>>>>>> get's removed
  %42 = icmp eq ptr %20, %11
  br i1 %42, label %43, label %18, !llvm.loop !553
```
2. We cannot optimise the store in cases where either of the true/false blocks post dominates the icmp condition. (For reasons similar to not doing so when both blocks are the same.)

@nikic What are your thoughts on the optimisation happening in 1? Is it expected? Any ideas as to why I am unable to reproduce it using the following test case (in `noop-stores.ll`):
```
define void @remove_tautological_store_bug(ptr %x) {
; CHECK-LABEL: @remove_tautological_store_bug(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[X1:%.*]] = load ptr, ptr [[X:%.*]], align 8
; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[X1]], null
; CHECK-NEXT:    br i1 [[CMP]], label [[IF_EQ:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    br label [[IF_EQ]]
; CHECK:       if.eq:
; CHECK-NEXT:    store ptr null, ptr [[X]], align 8
; CHECK-NEXT:    br label [[END:%.*]]
; CHECK:       end:
; CHECK-NEXT:    ret void
;
entry:                                               ; preds = %31, %26
  %x1 = load ptr, ptr %x, align 8
  %cmp = icmp eq ptr %x1, null
  br i1 %cmp, label %if.eq, label %if.else

if.else:                                               ; preds = %32
  br label %if.eq

if.eq:                                               ; preds = %37, %32
  store ptr null, ptr %x, align 8
  br label %end

end:
  ret void
}
```

https://github.com/llvm/llvm-project/pull/75744


More information about the llvm-commits mailing list