[llvm] r259736 - [SCEV] Try to reuse existing value during SCEV expansion

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 4 03:01:55 PST 2016


On 02/04/2016 02:27 AM, Wei Mi via llvm-commits wrote:
> Author: wmi
> Date: Wed Feb  3 19:27:38 2016
> New Revision: 259736
>
> URL: http://llvm.org/viewvc/llvm-project?rev=259736&view=rev
> Log:
> [SCEV] Try to reuse existing value during SCEV expansion
>
> Current SCEV expansion will expand SCEV as a sequence of operations
> and doesn't utilize the value already existed. This will introduce
> redundent computation which may not be cleaned up throughly by
> following optimizations.
>
> This patch introduces an ExprValueMap which is a map from SCEV to the
> set of equal values with the same SCEV. When a SCEV is expanded, the
> set of values is checked and reused whenever possible before generating
> a sequence of operations.
>
> The original commit triggered regressions in Polly tests. The regressions
> exposed two problems which have been fixed in current version.
>
> 1. Polly will generate a new function based on the old one. To generate an
> instruction for the new function, it builds SCEV for the old instruction,
> applies some tranformation on the SCEV generated, then expands the transformed
> SCEV and insert the expanded value into new function. Because SCEV expansion
> may reuse value cached in ExprValueMap, the value in old function may be
> inserted into new function, which is wrong.
>     In SCEVExpander::expand, there is a logic to check the cached value to
> be used should dominate the insertion point. However, for the above
> case, the check always passes. That is because the insertion point is
> in a new function, which is unreachable from the old function. However
> for unreachable node, DominatorTreeBase::dominates thinks it will be
> dominated by any other node.
>     The fix is to simply add a check that the cached value to be used in
> expansion should be in the same function as the insertion point instruction.
>
> 2. When the SCEV is of scConstant type, expanding it directly is cheaper than
> reusing a normal value cached. Although in the cached value set in ExprValueMap,
> there is a Constant type value, but it is not easy to find it out -- the cached
> Value set is not sorted according to the potential cost. Existing reuse logic
> in SCEVExpander::expand simply chooses the first legal element from the cached
> value set.
>     The fix is that when the SCEV is of scConstant type, don't try the reuse
> logic. simply expand it.

This unfortunately breaks -indvars (visible in our LNT servers):

opt: lib/Transforms/Scalar/IndVarSimplify.cpp:544: void (anonymous 
namespace)::IndVarSimplify::rewriteLoopExitValues(llvm::Loop *, 
llvm::SCEVExpander &): Assertion `L->isRecursivelyLCSSAForm(*DT) && 
"Indvars did not preserve LCSSA!"' failed.
#0 0x00007f0884d907c8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) 
(bin/../lib/libLLVMSupport.so+0xcc7c8)

Best,
Tobias
-------------- next part --------------
; ModuleID = 'bugpoint-reduced-simplified.bc'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define void @hoge() #0 {
bb:
  br label %bb1

bb1:                                              ; preds = %bb29, %bb
  %tmp = phi i32 [ %tmp30, %bb29 ], [ 0, %bb ]
  br i1 undef, label %bb2, label %bb4

bb2:                                              ; preds = %bb1
  br i1 undef, label %bb29, label %bb3

bb3:                                              ; preds = %bb2
  br label %bb29

bb4:                                              ; preds = %bb1
  br i1 undef, label %bb5, label %bb22

bb5:                                              ; preds = %bb4
  br i1 undef, label %bb29, label %bb9

bb6:                                              ; preds = %bb9
  %tmp7 = add nsw i32 %tmp, -1
  %tmp8 = icmp sgt i32 %tmp, 1
  br i1 %tmp8, label %bb15, label %bb19

bb9:                                              ; preds = %bb13, %bb5
  %tmp10 = phi i32 [ %tmp14, %bb13 ], [ 0, %bb5 ]
  %tmp11 = add nsw i32 %tmp, -1
  %tmp12 = icmp slt i32 %tmp10, %tmp11
  br i1 %tmp12, label %bb13, label %bb6

bb13:                                             ; preds = %bb9
  %tmp14 = add nsw i32 %tmp10, 1
  br label %bb9

bb15:                                             ; preds = %bb15, %bb6
  %tmp16 = phi i32 [ %tmp17, %bb15 ], [ 0, %bb6 ]
  %tmp17 = add nsw i32 %tmp16, 1
  %tmp18 = icmp slt i32 %tmp17, %tmp7
  br i1 %tmp18, label %bb15, label %bb19

bb19:                                             ; preds = %bb15, %bb6
  br i1 undef, label %bb20, label %bb21

bb20:                                             ; preds = %bb19
  br label %bb29

bb21:                                             ; preds = %bb19
  br label %bb29

bb22:                                             ; preds = %bb4
  br i1 undef, label %bb28, label %bb23

bb23:                                             ; preds = %bb22
  br i1 undef, label %bb26, label %bb24

bb24:                                             ; preds = %bb23
  br i1 false, label %bb29, label %bb25

bb25:                                             ; preds = %bb24
  br label %bb29

bb26:                                             ; preds = %bb23
  br i1 undef, label %bb29, label %bb27

bb27:                                             ; preds = %bb26
  br label %bb29

bb28:                                             ; preds = %bb22
  br label %bb29

bb29:                                             ; preds = %bb28, %bb27, %bb26, %bb25, %bb24, %bb21, %bb20, %bb5, %bb3, %bb2
  %tmp30 = phi i32 [ %tmp, %bb2 ], [ %tmp, %bb3 ], [ %tmp, %bb5 ], [ %tmp, %bb21 ], [ %tmp, %bb20 ], [ %tmp, %bb24 ], [ %tmp, %bb25 ], [ %tmp, %bb26 ], [ %tmp, %bb27 ], [ %tmp, %bb28 ]
  br i1 undef, label %bb1, label %bb31

bb31:                                             ; preds = %bb29
  ret void
}

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.9.0 (trunk 259751) (llvm/trunk 259771)"}


More information about the llvm-commits mailing list