PPC: Add CTR-register clobber to builtin setjmp
authorHal Finkel <hfinkel@anl.gov>
Wed, 17 Jul 2013 05:35:44 +0000 (05:35 +0000)
committerHal Finkel <hfinkel@anl.gov>
Wed, 17 Jul 2013 05:35:44 +0000 (05:35 +0000)
Because the builtin longjmp implementation uses a CTR-based indirect jump, when
the control flow arrives at the builtin setjmp call, the CTR register has
necessarily been clobbered. Correspondingly, this adds CTR to the list of
implicit definitions of the builtin setjmp pseudo instruction.

We don't need to add CTR to the implicit definitions of builtin longjmp
because, even though it does clobber the CTR register, the control flow cannot
return to inside the loop unless there is also a builtin setjmp call.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186488 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/PowerPC/PPCCTRLoops.cpp
lib/Target/PowerPC/PPCInstr64Bit.td
lib/Target/PowerPC/PPCInstrInfo.td
test/CodeGen/PowerPC/sj-ctr-loop.ll [new file with mode: 0644]

index 30181a027a04b617b76477689c54ef2528291fcf..4e30c537645a524de23efbcb8c2a94159dfc9f67 100644 (file)
@@ -233,6 +233,13 @@ bool PPCCTRLoops::mightUseCTR(const Triple &TT, BasicBlock *BB) {
 #endif
 
           case Intrinsic::longjmp:
+
+          // Exclude eh_sjlj_setjmp; we don't need to exclude eh_sjlj_longjmp
+          // because, although it does clobber the counter register, the
+          // control can't then return to inside the loop unless there is also
+          // an eh_sjlj_setjmp.
+          case Intrinsic::eh_sjlj_setjmp:
+
           case Intrinsic::memcpy:
           case Intrinsic::memmove:
           case Intrinsic::memset:
index e7bb25941d848e056b4d3d4173b5258abdec26d2..f04820ad5238b3af2a1b28fc7524aee19d405d53 100644 (file)
@@ -280,6 +280,7 @@ def MFCR8 : XFXForm_3<31, 19, (outs g8rc:$rT), (ins),
 } // neverHasSideEffects = 1
 
 let hasSideEffects = 1, isBarrier = 1, usesCustomInserter = 1 in {
+  let Defs = [CTR8] in
   def EH_SjLj_SetJmp64  : Pseudo<(outs gprc:$dst), (ins memr:$buf),
                             "#EH_SJLJ_SETJMP64",
                             [(set i32:$dst, (PPCeh_sjlj_setjmp addr:$buf))]>,
index d4969f688ed504c2da0d1bd2cac1aad1719032d9..398a11b29d2abf715faaef33ee02e2ca6407c923 100644 (file)
@@ -1093,6 +1093,7 @@ def TAILBA   : IForm<18, 0, 0, (outs), (ins abscalltarget:$dst),
 }
 
 let hasSideEffects = 1, isBarrier = 1, usesCustomInserter = 1 in {
+  let Defs = [CTR] in
   def EH_SjLj_SetJmp32  : Pseudo<(outs gprc:$dst), (ins memr:$buf),
                             "#EH_SJLJ_SETJMP32",
                             [(set i32:$dst, (PPCeh_sjlj_setjmp addr:$buf))]>,
diff --git a/test/CodeGen/PowerPC/sj-ctr-loop.ll b/test/CodeGen/PowerPC/sj-ctr-loop.ll
new file mode 100644 (file)
index 0000000..1866bcd
--- /dev/null
@@ -0,0 +1,50 @@
+; RUN: llc -mtriple=powerpc64-unknown-linux-gnu < %s | FileCheck %s
+target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f128:128:128-v128:128:128-n32:64"
+target triple = "powerpc64-unknown-linux-gnu"
+
+%struct.__jmp_buf_tag.1.15.17.21.25.49.53.55 = type { [64 x i64], i32, %struct.__sigset_t.0.14.16.20.24.48.52.54, [8 x i8] }
+%struct.__sigset_t.0.14.16.20.24.48.52.54 = type { [16 x i64] }
+
+@env_sigill = external global [1 x %struct.__jmp_buf_tag.1.15.17.21.25.49.53.55], align 16
+
+; CHECK-LABEL: @main
+; CHECK-NOT: mtctr
+
+; Function Attrs: nounwind
+define void @main() #0 {
+entry:
+  br i1 undef, label %return, label %if.end
+
+if.end:                                           ; preds = %entry
+  br i1 undef, label %for.body.lr.ph, label %for.end.thread
+
+for.end.thread:                                   ; preds = %if.end
+  br label %return
+
+for.body.lr.ph:                                   ; preds = %if.end
+  br label %for.body
+
+for.cond:                                         ; preds = %for.body
+  %cmp2 = icmp slt i32 %inc, undef
+  br i1 %cmp2, label %for.body, label %for.end
+
+for.body:                                         ; preds = %for.cond, %for.body.lr.ph
+  %i.032 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.cond ]
+  %0 = call i32 @llvm.eh.sjlj.setjmp(i8* bitcast ([1 x %struct.__jmp_buf_tag.1.15.17.21.25.49.53.55]* @env_sigill to i8*))
+  %inc = add nsw i32 %i.032, 1
+  br i1 false, label %if.else, label %for.cond
+
+if.else:                                          ; preds = %for.body
+  unreachable
+
+for.end:                                          ; preds = %for.cond
+  unreachable
+
+return:                                           ; preds = %for.end.thread, %entry
+  ret void
+}
+
+; Function Attrs: nounwind
+declare i32 @llvm.eh.sjlj.setjmp(i8*) #0
+
+attributes #0 = { nounwind }