]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/patches/patch-28-llvm-r224890-ppc-ctr-tls-loop.diff
Merge ^/head r274961 through r276418.
[FreeBSD/FreeBSD.git] / contrib / llvm / patches / patch-28-llvm-r224890-ppc-ctr-tls-loop.diff
1 Pull in r224890 from upstream llvm trunk (by David Majnemer):
2
3   PowerPC: CTR shouldn't fire if a TLS call is in the loop
4
5   Determining the address of a TLS variable results in a function call in
6   certain TLS models.  This means that a simple ICmpInst might actually
7   result in invalidating the CTR register.
8
9   In such cases, do not attempt to rely on the CTR register for loop
10   optimization purposes.
11
12   This fixes PR22034.
13
14   Differential Revision: http://reviews.llvm.org/D6786
15
16 This fixes a "Invalid PPC CTR loop" error when compiling parts of libc
17 for PowerPC-32.
18
19 Introduced here: http://svnweb.freebsd.org/changeset/base/276324
20
21 Index: lib/Target/PowerPC/PPCCTRLoops.cpp
22 ===================================================================
23 --- lib/Target/PowerPC/PPCCTRLoops.cpp
24 +++ lib/Target/PowerPC/PPCCTRLoops.cpp
25 @@ -194,6 +194,21 @@ static bool isLargeIntegerTy(bool Is32Bit, Type *T
26    return false;
27  }
28  
29 +// Determining the address of a TLS variable results in a function call in
30 +// certain TLS models.
31 +static bool memAddrUsesCTR(const PPCTargetMachine *TM,
32 +                           const llvm::Value *MemAddr) {
33 +  const auto *GV = dyn_cast<GlobalValue>(MemAddr);
34 +  if (!GV)
35 +    return false;
36 +  if (!GV->isThreadLocal())
37 +    return false;
38 +  if (!TM)
39 +    return true;
40 +  TLSModel::Model Model = TM->getTLSModel(GV);
41 +  return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic;
42 +}
43 +
44  bool PPCCTRLoops::mightUseCTR(const Triple &TT, BasicBlock *BB) {
45    for (BasicBlock::iterator J = BB->begin(), JE = BB->end();
46         J != JE; ++J) {
47 @@ -390,6 +405,9 @@ bool PPCCTRLoops::mightUseCTR(const Triple &TT, Ba
48            SI->getNumCases()+1 >= (unsigned) TLI->getMinimumJumpTableEntries())
49          return true;
50      }
51 +    for (Value *Operand : J->operands())
52 +      if (memAddrUsesCTR(TM, Operand))
53 +        return true;
54    }
55  
56    return false;
57 Index: test/CodeGen/PowerPC/ctrloops.ll
58 ===================================================================
59 --- test/CodeGen/PowerPC/ctrloops.ll
60 +++ test/CodeGen/PowerPC/ctrloops.ll
61 @@ -1,6 +1,6 @@
62  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-v128:128:128-n32:64"
63  target triple = "powerpc64-unknown-freebsd10.0"
64 -; RUN: llc < %s -march=ppc64 | FileCheck %s
65 +; RUN: llc < %s -march=ppc64 -relocation-model=pic | FileCheck %s
66  
67  @a = common global i32 0, align 4
68  
69 @@ -73,3 +73,26 @@ for.end:
70  ; CHECK-NOT: cmplwi
71  ; CHECK: bdnz
72  }
73 +
74 +@tls_var = external thread_local global i8
75 +
76 +define i32 @test4() {
77 +entry:
78 +  br label %for.body
79 +
80 +for.body:                                         ; preds = %for.body, %entry
81 +  %phi = phi i32 [ %dec, %for.body ], [ undef, %entry ]
82 +  %load = ptrtoint i8* @tls_var to i32
83 +  %dec = add i32 %phi, -1
84 +  %cmp = icmp sgt i32 %phi, 1
85 +  br i1 %cmp, label %for.body, label %return
86 +
87 +return:                                           ; preds = %for.body
88 +  ret i32 %load
89 +; CHECK-LABEL: @test4
90 +; CHECK-NOT: mtctr
91 +; CHECK: addi {{[0-9]+}}
92 +; CHECK: cmpwi
93 +; CHECK-NOT: bdnz
94 +; CHECK: bgt
95 +}