ucb-bar / esp-llvm

UCB-BAR fork of LLVM! NOT UPSTREAM RISCV LLVM
Other
123 stars 55 forks source link

InsertBranch works incorrectly #16

Closed stettberger closed 8 years ago

stettberger commented 9 years ago

In attached .ll file, the first conditional branch (icmp slt %0, 0) will be optimized away. Although it is absolutely neccessary. In my case this resulted in the fact, that my printf implmentation printed always '-' before each number, although it is positive.

Ok, so, what happened?

The current RISCV InsertBranch implementation checks for the current layout of the machine basic blocks. This is not allowed! The BranchFolding pass explicitly inserts a two-way branch before removing an empty block. So, the branch folder wants to reorganize the structure, and demands the insertion of branches, but we do not obey, so invalid code is generated.

stettberger commented 9 years ago
; ModuleID = './arch/osek-v/CMakeFiles/arch.dir/startup.cc.obj'
target datalayout = "e-p:64:64:64-i1:8:16-i8:8:16-i16:16-i32:32-i64:64-f64:64-f128:128-n32:64"
target triple = "riscv"

%class.HTIFOutputStream = type { %class.O_Stream }
%class.O_Stream = type { i8 }

@a = external global i32, align 4
@kout = external global %class.HTIFOutputStream

; Function Attrs: noimplicitfloat noreturn nounwind
define void @arch_startup() #0 {
entry:
  %0 = load i32* @a, align 4, !tbaa !0
  %cmp.i.i = icmp slt i32 %0, 0
  br i1 %cmp.i.i, label %if.then.i.i, label %do.body.i.i

if.then.i.i:                                      ; preds = %entry
  tail call void @_ZN16HTIFOutputStream7putcharEc(%class.HTIFOutputStream* @kout, i8 45) #2
  %mul.i.i = sub i32 0, %0
  br label %do.body.i.i

do.body.i.i:                                      ; preds = %do.body.i.i, %if.then.i.i, %entry
  %value.addr.0.i.i = phi i32 [ %div.i.i, %do.body.i.i ], [ %0, %entry ], [ %mul.i.i, %if.then.i.i ]
  %1 = load i8* getelementptr inbounds (%class.HTIFOutputStream* @kout, i64 0, i32 0, i32 0), align 1, !tbaa !1
  %conv.i.i = zext i8 %1 to i32
  %div.i.i = sdiv i32 %value.addr.0.i.i, %conv.i.i
  tail call void @_ZN16HTIFOutputStream7putcharEc(%class.HTIFOutputStream* @kout, i8 97) #2
  %tobool.i.i = icmp eq i32 %div.i.i, 0
  br i1 %tobool.i.i, label %_ZN8O_StreamI16HTIFOutputStreamElsEi.exit, label %do.body.i.i

_ZN8O_StreamI16HTIFOutputStreamElsEi.exit:        ; preds = %do.body.i.i
  tail call void @_ZN16HTIFOutputStream7putcharEc(%class.HTIFOutputStream* @kout, i8 10) #2
  br label %do.body.i

do.body.i:                                        ; preds = %do.body.i, %_ZN8O_StreamI16HTIFOutputStreamElsEi.exit
  tail call void asm sideeffect "csrw $0, $1", "i,r"(i32 1920, i64 1) #3, !srcloc !3
  br label %do.body.i
}

; Function Attrs: noimplicitfloat
declare void @_ZN16HTIFOutputStream7putcharEc(%class.HTIFOutputStream*, i8) #1

attributes #0 = { noimplicitfloat noreturn nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { noimplicitfloat "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { nobuiltin noimplicitfloat nounwind }
attributes #3 = { nounwind }

!0 = metadata !{metadata !"int", metadata !1}
!1 = metadata !{metadata !"omnipotent char", metadata !2}
!2 = metadata !{metadata !"Simple C/C++ TBAA"}
!3 = metadata !{i32 14059}
stettberger commented 9 years ago

Oh, and I don't know wheter this bug really can occur in the RISCV branch. Since i applied svenstucki's patches. Nevertheless, it is a bug.