Skip to content

Commit c520636

Browse files
committed
Improve AVR loop code generation
1 parent 69d0078 commit c520636

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

clang/lib/Basic/Targets/AVR.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
5757
Int16Type = SignedInt;
5858
Char32Type = UnsignedLong;
5959
SigAtomicType = SignedChar;
60-
resetDataLayout("e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8");
60+
resetDataLayout(
61+
"e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-n16:8-a:8");
6162
}
6263

6364
void getTargetDefines(const LangOptions &Opts,

llvm/lib/Target/AVR/AVRTargetMachine.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "AVR.h"
2121
#include "AVRMachineFunctionInfo.h"
22+
#include "AVRTargetTransformInfo.h"
2223
#include "AVRTargetObjectFile.h"
2324
#include "MCTargetDesc/AVRMCTargetDesc.h"
2425
#include "TargetInfo/AVRTargetInfo.h"
@@ -28,7 +29,7 @@
2829
namespace llvm {
2930

3031
static const char *AVRDataLayout =
31-
"e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8";
32+
"e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-n16:8-a:8";
3233

3334
/// Processes a CPU name.
3435
static StringRef getCPU(StringRef CPU) {
@@ -62,7 +63,9 @@ namespace {
6263
class AVRPassConfig : public TargetPassConfig {
6364
public:
6465
AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM)
65-
: TargetPassConfig(TM, PM) {}
66+
: TargetPassConfig(TM, PM) {
67+
EnableLoopTermFold = true;
68+
}
6669

6770
AVRTargetMachine &getAVRTargetMachine() const {
6871
return getTM<AVRTargetMachine>();
@@ -107,6 +110,10 @@ const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {
107110
return &SubTarget;
108111
}
109112

113+
TargetTransformInfo AVRTargetMachine::getTargetTransformInfo(const Function &F) const {
114+
return TargetTransformInfo(std::make_unique<AVRTTIImpl>(this, F));
115+
}
116+
110117
MachineFunctionInfo *AVRTargetMachine::createMachineFunctionInfo(
111118
BumpPtrAllocator &Allocator, const Function &F,
112119
const TargetSubtargetInfo *STI) const {

llvm/lib/Target/AVR/AVRTargetMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class AVRTargetMachine : public CodeGenTargetMachineImpl {
4848
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
4949
const TargetSubtargetInfo *STI) const override;
5050

51+
TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
52+
5153
bool isNoopAddrSpaceCast(unsigned SrcAs, unsigned DestAs) const override {
5254
// While AVR has different address spaces, they are all represented by
5355
// 16-bit pointers that can be freely casted between (of course, a pointer
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===- AVRTargetTransformInfo.h - AVR 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 defines a TargetTransformInfoImplBase conforming object specific
10+
/// to the AVR 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_AVR_AVRTARGETTRANSFORMINFO_H
17+
#define LLVM_LIB_TARGET_AVR_AVRTARGETTRANSFORMINFO_H
18+
19+
#include "AVRSubtarget.h"
20+
#include "AVRTargetMachine.h"
21+
#include "llvm/Analysis/TargetTransformInfo.h"
22+
#include "llvm/CodeGen/BasicTTIImpl.h"
23+
#include "llvm/IR/Function.h"
24+
#include <optional>
25+
26+
namespace llvm {
27+
28+
class AVRTTIImpl final : public BasicTTIImplBase<AVRTTIImpl> {
29+
using BaseT = BasicTTIImplBase<AVRTTIImpl>;
30+
using TTI = TargetTransformInfo;
31+
32+
friend BaseT;
33+
34+
const AVRSubtarget *ST;
35+
const AVRTargetLowering *TLI;
36+
37+
const AVRSubtarget *getST() const { return ST; }
38+
const AVRTargetLowering *getTLI() const { return TLI; }
39+
40+
public:
41+
explicit AVRTTIImpl(const AVRTargetMachine *TM, const Function &F)
42+
: BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),
43+
TLI(ST->getTargetLowering()) {}
44+
45+
#if 0 // TODO Examine if these options result in better code generation
46+
/// Return the cost of materializing an immediate for a value operand of
47+
/// a store instruction.
48+
InstructionCost getStoreImmCost(Type *VecTy, TTI::OperandValueInfo OpInfo,
49+
TTI::TargetCostKind CostKind) const;
50+
51+
InstructionCost getIntImmCost(const APInt &Imm, Type *Ty,
52+
TTI::TargetCostKind CostKind) const override;
53+
InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx,
54+
const APInt &Imm, Type *Ty,
55+
TTI::TargetCostKind CostKind,
56+
Instruction *Inst = nullptr) const override;
57+
InstructionCost
58+
getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
59+
Type *Ty, TTI::TargetCostKind CostKind) const override;
60+
61+
InstructionCost
62+
getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
63+
TTI::TargetCostKind CostKind) const override;
64+
65+
InstructionCost getCmpSelInstrCost(
66+
unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred,
67+
TTI::TargetCostKind CostKind,
68+
TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
69+
TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
70+
const Instruction *I = nullptr) const override;
71+
#endif
72+
73+
bool isLSRCostLess(const TargetTransformInfo::LSRCost &C1,
74+
const TargetTransformInfo::LSRCost &C2) const override {return C1.Insns < C2.Insns;}
75+
};
76+
77+
} // end namespace llvm
78+
79+
#endif // LLVM_LIB_TARGET_AVR_AVRTARGETTRANSFORMINFO_H

0 commit comments

Comments
 (0)