1 Pull in r224890 from upstream llvm trunk (by David Majnemer):
3 PowerPC: CTR shouldn't fire if a TLS call is in the loop
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.
9 In such cases, do not attempt to rely on the CTR register for loop
10 optimization purposes.
14 Differential Revision: http://reviews.llvm.org/D6786
16 This fixes a "Invalid PPC CTR loop" error when compiling parts of libc
19 Introduced here: http://svnweb.freebsd.org/changeset/base/276324
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
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);
36 + if (!GV->isThreadLocal())
40 + TLSModel::Model Model = TM->getTLSModel(GV);
41 + return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic;
44 bool PPCCTRLoops::mightUseCTR(const Triple &TT, BasicBlock *BB) {
45 for (BasicBlock::iterator J = BB->begin(), JE = BB->end();
47 @@ -390,6 +405,9 @@ bool PPCCTRLoops::mightUseCTR(const Triple &TT, Ba
48 SI->getNumCases()+1 >= (unsigned) TLI->getMinimumJumpTableEntries())
51 + for (Value *Operand : J->operands())
52 + if (memAddrUsesCTR(TM, Operand))
57 Index: test/CodeGen/PowerPC/ctrloops.ll
58 ===================================================================
59 --- test/CodeGen/PowerPC/ctrloops.ll
60 +++ test/CodeGen/PowerPC/ctrloops.ll
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
67 @a = common global i32 0, align 4
69 @@ -73,3 +73,26 @@ for.end:
74 +@tls_var = external thread_local global i8
76 +define i32 @test4() {
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
87 +return: ; preds = %for.body
89 +; CHECK-LABEL: @test4
91 +; CHECK: addi {{[0-9]+}}