[LLVMdev] failed folding with constant array with opt -O3

Peng Cheng gm4cheng at gmail.com
Tue Sep 9 09:30:44 PDT 2014


I have the following simplified llvm ir, which basically returns value
based on the first value of a constant array.

----
; ModuleID = 'simple_ir3.txt'

@f.b = constant [1 x i32] [i32 1], align 4          ; constant array with
value 1 at the first element

define void @f(i32* nocapture %l0) {
entry:
  %fc_ = alloca [1 x i32]
  %f.b.v = load [1 x i32]* @f.b
  store [1 x i32] %f.b.v, [1 x i32]* %fc_
  %0 = getelementptr [1 x i32]* %fc_, i64 0, i64 0  ; load the first
element of the constant array, which is actually 1
  %1 = load i32* %0
  %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
  br i1 %tobool, label %2, label %4

; <label>:2               ; true branch
  store i32 1, i32* %l0;
  %3 = load i32* %l0;
  br label %4

; <label>:4
  %storemerge = phi i32 [ %3, %2 ], [ 0, %entry ]
  store i32 %storemerge, i32* %l0
  ret void
}
---

I ran opt -O3 simple_ir.txt -S, and got:

---
; ModuleID = 'simple_ir3.txt'

@f.b = constant [1 x i32] [i32 1], align 4

; Function Attrs: nounwind
define void @f(i32* nocapture %l0) #0 {
entry:
  %fc_ = alloca [1 x i32]
  store [1 x i32] [i32 1], [1 x i32]* %fc_
  %0 = getelementptr [1 x i32]* %fc_, i64 0, i64 0
  %1 = load i32* %0
  %tobool = icmp eq i32 %1, 0
  br i1 %tobool, label %3, label %2

; <label>:2                                       ; preds = %entry
  store i32 1, i32* %l0
  br label %3

; <label>:3                                       ; preds = %entry, %2
  %storemerge = phi i32 [ 1, %2 ], [ 0, %entry ]
  store i32 %storemerge, i32* %l0
  ret void
}

attributes #0 = { nounwind }
---

I would expect that the constant folding, or some other transformations,
would be able to fold the constant to get the following ir:

---
define void @f(i32* nocapture %l0) #0 {
  store i32 1, i32* %l0
  ret void
}
---

How could I get the expected optimized ir?  update the original ir, or use
different set of transformations?

Any suggestions or comments?


Thanks,
-Peng
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140909/d90c01d7/attachment.html>


More information about the llvm-dev mailing list