--- /dev/null
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc --mtriple=loongarch32 < %s | FileCheck %s
+; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s
+
+declare i32 @llvm.loongarch.csrrd.w(i32 immarg) nounwind
+declare i32 @llvm.loongarch.csrwr.w(i32, i32 immarg) nounwind
+declare void @bug()
+
+define dso_local void @foo(i32 noundef signext %flag) nounwind {
+; CHECK-LABEL: foo:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: beqz $a0, .LBB0_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: csrrd $a0, 2
+; CHECK-NEXT: ori $a0, $a0, 1
+; CHECK-NEXT: csrwr $a0, 2
+; CHECK-NEXT: .LBB0_2: # %if.end
+; CHECK-NEXT: csrrd $a0, 2
+; CHECK-NEXT: andi $a0, $a0, 1
+; CHECK-NEXT: bnez $a0, .LBB0_4
+; CHECK-NEXT: # %bb.3: # %if.then2
+; CHECK-NEXT: b %plt(bug)
+; CHECK-NEXT: .LBB0_4: # %if.end3
+; CHECK-NEXT: ret
+entry:
+ %tobool.not = icmp eq i32 %flag, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then: ; preds = %entry
+ %0 = tail call i32 @llvm.loongarch.csrrd.w(i32 2)
+ %or = or i32 %0, 1
+ %1 = tail call i32 @llvm.loongarch.csrwr.w(i32 %or, i32 2)
+ br label %if.end
+
+if.end: ; preds = %if.then, %entry
+ %2 = tail call i32 @llvm.loongarch.csrrd.w(i32 2)
+ %and = and i32 %2, 1
+ %tobool1.not = icmp eq i32 %and, 0
+ br i1 %tobool1.not, label %if.then2, label %if.end3
+
+if.then2: ; preds = %if.end
+ tail call void @bug()
+ br label %if.end3
+
+if.end3: ; preds = %if.then2, %if.end
+ ret void
+}
--- /dev/null
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s
+
+declare i32 @llvm.loongarch.iocsrrd.b(i32) nounwind
+declare void @llvm.loongarch.iocsrwr.b(i32, i32) nounwind
+declare i32 @llvm.loongarch.iocsrrd.h(i32) nounwind
+declare void @llvm.loongarch.iocsrwr.h(i32, i32) nounwind
+declare i32 @llvm.loongarch.iocsrrd.w(i32) nounwind
+declare void @llvm.loongarch.iocsrwr.w(i32, i32) nounwind
+declare i64 @llvm.loongarch.iocsrrd.d(i32) nounwind
+declare void @llvm.loongarch.iocsrwr.d(i64, i32) nounwind
+declare void @bug()
+
+define dso_local void @test_b(i32 noundef signext %flag) nounwind {
+; CHECK-LABEL: test_b:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: beqz $a0, .LBB0_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.b $a1, $a0
+; CHECK-NEXT: ori $a1, $a1, 1
+; CHECK-NEXT: iocsrwr.b $a1, $a0
+; CHECK-NEXT: .LBB0_2: # %if.end
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.b $a0, $a0
+; CHECK-NEXT: andi $a0, $a0, 1
+; CHECK-NEXT: bnez $a0, .LBB0_4
+; CHECK-NEXT: # %bb.3: # %if.then2
+; CHECK-NEXT: b %plt(bug)
+; CHECK-NEXT: .LBB0_4: # %if.end3
+; CHECK-NEXT: ret
+entry:
+ %tobool.not = icmp eq i32 %flag, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then: ; preds = %entry
+ %0 = tail call i32 @llvm.loongarch.iocsrrd.b(i32 2)
+ %or = or i32 %0, 1
+ tail call void @llvm.loongarch.iocsrwr.b(i32 %or, i32 2)
+ br label %if.end
+
+if.end: ; preds = %if.then, %entry
+ %1 = tail call i32 @llvm.loongarch.iocsrrd.b(i32 2)
+ %and = and i32 %1, 1
+ %tobool1.not = icmp eq i32 %and, 0
+ br i1 %tobool1.not, label %if.then2, label %if.end3
+
+if.then2: ; preds = %if.end
+ tail call void @bug()
+ br label %if.end3
+
+if.end3: ; preds = %if.then2, %if.end
+ ret void
+}
+
+define dso_local void @test_h(i32 noundef signext %flag) nounwind {
+; CHECK-LABEL: test_h:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: beqz $a0, .LBB1_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.h $a1, $a0
+; CHECK-NEXT: ori $a1, $a1, 1
+; CHECK-NEXT: iocsrwr.h $a1, $a0
+; CHECK-NEXT: .LBB1_2: # %if.end
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.h $a0, $a0
+; CHECK-NEXT: andi $a0, $a0, 1
+; CHECK-NEXT: bnez $a0, .LBB1_4
+; CHECK-NEXT: # %bb.3: # %if.then2
+; CHECK-NEXT: b %plt(bug)
+; CHECK-NEXT: .LBB1_4: # %if.end3
+; CHECK-NEXT: ret
+entry:
+ %tobool.not = icmp eq i32 %flag, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then: ; preds = %entry
+ %0 = tail call i32 @llvm.loongarch.iocsrrd.h(i32 2)
+ %or = or i32 %0, 1
+ tail call void @llvm.loongarch.iocsrwr.h(i32 %or, i32 2)
+ br label %if.end
+
+if.end: ; preds = %if.then, %entry
+ %1 = tail call i32 @llvm.loongarch.iocsrrd.h(i32 2)
+ %and = and i32 %1, 1
+ %tobool1.not = icmp eq i32 %and, 0
+ br i1 %tobool1.not, label %if.then2, label %if.end3
+
+if.then2: ; preds = %if.end
+ tail call void @bug()
+ br label %if.end3
+
+if.end3: ; preds = %if.then2, %if.end
+ ret void
+}
+
+define dso_local void @test_w(i32 noundef signext %flag) nounwind {
+; CHECK-LABEL: test_w:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: beqz $a0, .LBB2_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.w $a1, $a0
+; CHECK-NEXT: ori $a1, $a1, 1
+; CHECK-NEXT: iocsrwr.w $a1, $a0
+; CHECK-NEXT: .LBB2_2: # %if.end
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.w $a0, $a0
+; CHECK-NEXT: andi $a0, $a0, 1
+; CHECK-NEXT: bnez $a0, .LBB2_4
+; CHECK-NEXT: # %bb.3: # %if.then2
+; CHECK-NEXT: b %plt(bug)
+; CHECK-NEXT: .LBB2_4: # %if.end3
+; CHECK-NEXT: ret
+entry:
+ %tobool.not = icmp eq i32 %flag, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then: ; preds = %entry
+ %0 = tail call i32 @llvm.loongarch.iocsrrd.w(i32 2)
+ %or = or i32 %0, 1
+ tail call void @llvm.loongarch.iocsrwr.w(i32 %or, i32 2)
+ br label %if.end
+
+if.end: ; preds = %if.then, %entry
+ %1 = tail call i32 @llvm.loongarch.iocsrrd.w(i32 2)
+ %and = and i32 %1, 1
+ %tobool1.not = icmp eq i32 %and, 0
+ br i1 %tobool1.not, label %if.then2, label %if.end3
+
+if.then2: ; preds = %if.end
+ tail call void @bug()
+ br label %if.end3
+
+if.end3: ; preds = %if.then2, %if.end
+ ret void
+}
+
+define dso_local void @test_d(i32 noundef signext %flag) nounwind {
+; CHECK-LABEL: test_d:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: beqz $a0, .LBB3_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.d $a1, $a0
+; CHECK-NEXT: ori $a1, $a1, 1
+; CHECK-NEXT: iocsrwr.d $a1, $a0
+; CHECK-NEXT: .LBB3_2: # %if.end
+; CHECK-NEXT: ori $a0, $zero, 2
+; CHECK-NEXT: iocsrrd.d $a0, $a0
+; CHECK-NEXT: andi $a0, $a0, 1
+; CHECK-NEXT: bnez $a0, .LBB3_4
+; CHECK-NEXT: # %bb.3: # %if.then2
+; CHECK-NEXT: b %plt(bug)
+; CHECK-NEXT: .LBB3_4: # %if.end3
+; CHECK-NEXT: ret
+entry:
+ %tobool.not = icmp eq i32 %flag, 0
+ br i1 %tobool.not, label %if.end, label %if.then
+
+if.then: ; preds = %entry
+ %0 = tail call i64 @llvm.loongarch.iocsrrd.d(i32 2)
+ %or = or i64 %0, 1
+ tail call void @llvm.loongarch.iocsrwr.d(i64 %or, i32 2)
+ br label %if.end
+
+if.end: ; preds = %if.then, %entry
+ %1 = tail call i64 @llvm.loongarch.iocsrrd.d(i32 2)
+ %and = and i64 %1, 1
+ %tobool1.not = icmp eq i64 %and, 0
+ br i1 %tobool1.not, label %if.then2, label %if.end3
+
+if.then2: ; preds = %if.end
+ tail call void @bug()
+ br label %if.end3
+
+if.end3: ; preds = %if.then2, %if.end
+ ret void
+}