Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bfa717

Browse files
authored
[SPARC] Add TTI implementation for getPopcntSupport (llvm#178843)
This is to inform the middle-end transform passes about the capabilities of the codegen backend. This should solve the issue where popcount loops not gets converted properly to `popc`, even if it's available (llvm#171969).
1 parent 352932c commit 1bfa717

7 files changed

Lines changed: 409 additions & 0 deletions

File tree

llvm/lib/Target/Sparc/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ add_llvm_target(SparcCodeGen
3030
SparcSubtarget.cpp
3131
SparcTargetMachine.cpp
3232
SparcTargetObjectFile.cpp
33+
SparcTargetTransformInfo.cpp
3334

3435
LINK_COMPONENTS
36+
Analysis
3537
AsmPrinter
3638
CodeGen
3739
CodeGenTypes
@@ -43,6 +45,7 @@ add_llvm_target(SparcCodeGen
4345
Support
4446
Target
4547
TargetParser
48+
TransformUtils
4649

4750
ADD_TO_COMPONENT
4851
Sparc

llvm/lib/Target/Sparc/SparcTargetMachine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Sparc.h"
1515
#include "SparcMachineFunctionInfo.h"
1616
#include "SparcTargetObjectFile.h"
17+
#include "SparcTargetTransformInfo.h"
1718
#include "TargetInfo/SparcTargetInfo.h"
1819
#include "llvm/CodeGen/Passes.h"
1920
#include "llvm/CodeGen/TargetPassConfig.h"
@@ -149,6 +150,11 @@ TargetPassConfig *SparcTargetMachine::createPassConfig(PassManagerBase &PM) {
149150
return new SparcPassConfig(*this, PM);
150151
}
151152

153+
TargetTransformInfo
154+
SparcTargetMachine::getTargetTransformInfo(const Function &F) const {
155+
return TargetTransformInfo(std::make_unique<SparcTTIImpl>(this, F));
156+
}
157+
152158
void SparcPassConfig::addIRPasses() {
153159
addPass(createAtomicExpandLegacyPass());
154160

llvm/lib/Target/Sparc/SparcTargetMachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SparcTargetMachine : public CodeGenTargetMachineImpl {
4040
TargetLoweringObjectFile *getObjFileLowering() const override {
4141
return TLOF.get();
4242
}
43+
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
4344

4445
MachineFunctionInfo *
4546
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- SparcTargetTransformInfo.cpp - SPARC specific TTI -----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "SparcTargetTransformInfo.h"
10+
#include "llvm/Support/MathExtras.h"
11+
12+
using namespace llvm;
13+
14+
#define DEBUG_TYPE "sparctti"
15+
16+
TargetTransformInfo::PopcntSupportKind
17+
SparcTTIImpl::getPopcntSupport(unsigned TyWidth) const {
18+
assert(isPowerOf2_32(TyWidth) && "Type width must be power of 2");
19+
if (ST->usePopc())
20+
return TTI::PSK_FastHardware;
21+
return TTI::PSK_Software;
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- SparcTargetTransformInfo.cpp - SPARC specific TTI--------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file a TargetTransformInfoImplBase conforming object specific to the
10+
/// SPARC target machine. It uses the target's detailed information to
11+
/// provide more precise answers to certain TTI queries, while letting the
12+
/// target independent and default TTI implementations handle the rest.
13+
///
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_LIB_TARGET_SPARC_SPARCTARGETTRANSFORMINFO_H
17+
#define LLVM_LIB_TARGET_SPARC_SPARCTARGETTRANSFORMINFO_H
18+
19+
#include "SparcTargetMachine.h"
20+
#include "llvm/Analysis/TargetTransformInfo.h"
21+
#include "llvm/CodeGen/BasicTTIImpl.h"
22+
23+
namespace llvm {
24+
25+
class SparcTTIImpl final : public BasicTTIImplBase<SparcTTIImpl> {
26+
typedef BasicTTIImplBase<SparcTTIImpl> BaseT;
27+
typedef TargetTransformInfo TTI;
28+
friend BaseT;
29+
30+
const SparcSubtarget *ST;
31+
const SparcTargetLowering *TLI;
32+
33+
const SparcSubtarget *getST() const { return ST; }
34+
const SparcTargetLowering *getTLI() const { return TLI; }
35+
36+
public:
37+
explicit SparcTTIImpl(const SparcTargetMachine *TM, const Function &F)
38+
: BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
39+
TLI(ST->getTargetLowering()) {}
40+
41+
/// \name Scalar TTI Implementations
42+
/// @{
43+
44+
TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
45+
/// @}
46+
};
47+
48+
} // end namespace llvm
49+
50+
#endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if not "Sparc" in config.root.targets:
2+
config.unsupported = True

0 commit comments

Comments
 (0)