<div dir="ltr">I have the following simplified llvm ir, which basically returns value based on the first value of a constant array.  <div><br></div><div>----</div><div><div>; ModuleID = 'simple_ir3.txt'</div><div><br></div><div>@f.b = constant [1 x i32] [i32 1], align 4          ; constant array with value 1 at the first element</div><div><br></div><div>define void @f(i32* nocapture %l0) {</div><div>entry:</div><div>  %fc_ = alloca [1 x i32]</div><div>  %f.b.v = load [1 x i32]* @f.b</div><div>  store [1 x i32] %f.b.v, [1 x i32]* %fc_</div><div>  %0 = getelementptr [1 x i32]* %fc_, i64 0, i64 0  ; load the first element of the constant array, which is actually 1</div><div>  %1 = load i32* %0</div><div>  %tobool = icmp ne i32 %1, 0             ; check the first element to see if it is 1, which is actually always true since the first element of constant array is 1</div><div>  br i1 %tobool, label %2, label %4</div><div><br></div><div>; <label>:2               ; true branch</div><div>  store i32 1, i32* %l0;</div><div>  %3 = load i32* %l0;</div><div>  br label %4</div><div><br></div><div>; <label>:4</div><div>  %storemerge = phi i32 [ %3, %2 ], [ 0, %entry ]</div><div>  store i32 %storemerge, i32* %l0</div><div>  ret void</div><div>}</div></div><div>---</div><div><br></div><div>I ran opt -O3 simple_ir.txt -S, and got:</div><div><br></div><div>---</div><div><div>; ModuleID = 'simple_ir3.txt'</div><div><br></div><div>@f.b = constant [1 x i32] [i32 1], align 4</div><div><br></div><div>; Function Attrs: nounwind</div><div>define void @f(i32* nocapture %l0) #0 {</div><div>entry:</div><div>  %fc_ = alloca [1 x i32]</div><div>  store [1 x i32] [i32 1], [1 x i32]* %fc_</div><div>  %0 = getelementptr [1 x i32]* %fc_, i64 0, i64 0</div><div>  %1 = load i32* %0</div><div>  %tobool = icmp eq i32 %1, 0</div><div>  br i1 %tobool, label %3, label %2</div><div><br></div><div>; <label>:2                                       ; preds = %entry</div><div>  store i32 1, i32* %l0</div><div>  br label %3</div><div><br></div><div>; <label>:3                                       ; preds = %entry, %2</div><div>  %storemerge = phi i32 [ 1, %2 ], [ 0, %entry ]</div><div>  store i32 %storemerge, i32* %l0</div><div>  ret void</div><div>}</div><div><br></div><div>attributes #0 = { nounwind }</div></div><div>---</div><div><br></div><div>I would expect that the constant folding, or some other transformations, would be able to fold the constant to get the following ir:<br></div><div><br></div><div>---</div><div><div>define void @f(i32* nocapture %l0) #0 {</div><div>  store i32 1, i32* %l0</div><div>  ret void</div><div>}</div></div><div>---</div><div><br></div><div>How could I get the expected optimized ir?  update the original ir, or use different set of transformations?</div><div><br></div><div>Any suggestions or comments?  </div><div><br></div><div><br></div><div>Thanks,</div><div>-Peng</div></div>