Skip to content

Commit 15ab4bb

Browse files
authored
[Hexagon] Implement shouldConvertConstantLoadToIntImm (#146452)
This will convert loads of constant strings to immediate values. Put this behind a flag that is enabled by default so that we can toggle it if need be.
1 parent 67b7946 commit 15ab4bb

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

llvm/lib/Target/Hexagon/HexagonISelLowering.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ static cl::opt<int>
102102
MaxStoresPerMemsetOptSizeCL("max-store-memset-Os", cl::Hidden, cl::init(4),
103103
cl::desc("Max #stores to inline memset"));
104104

105+
static cl::opt<bool>
106+
ConstantLoadsToImm("constant-loads-to-imm", cl::Hidden, cl::init(true),
107+
cl::desc("Convert constant loads to immediate values."));
108+
105109
static cl::opt<bool> AlignLoads("hexagon-align-loads",
106110
cl::Hidden, cl::init(false),
107111
cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
@@ -3607,6 +3611,18 @@ bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
36073611
return true;
36083612
}
36093613

3614+
/// Returns true if it is beneficial to convert a load of a constant
3615+
/// to just the constant itself.
3616+
bool HexagonTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
3617+
Type *Ty) const {
3618+
if (!ConstantLoadsToImm)
3619+
return false;
3620+
3621+
assert(Ty->isIntegerTy());
3622+
unsigned BitSize = Ty->getPrimitiveSizeInBits();
3623+
return (BitSize > 0 && BitSize <= 64);
3624+
}
3625+
36103626
/// isLegalAddressingMode - Return true if the addressing mode represented by
36113627
/// AM is legal for this target, for a load/store of the specified type.
36123628
bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,

llvm/lib/Target/Hexagon/HexagonISelLowering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ class HexagonTargetLowering : public TargetLowering {
342342
SDValue getPICJumpTableRelocBase(SDValue Table, SelectionDAG &DAG)
343343
const override;
344344

345+
/// Returns true if it is beneficial to convert a load of a constant
346+
/// to just the constant itself.
347+
bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
348+
Type *Ty) const override;
349+
345350
bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy, EVT NewVT,
346351
std::optional<unsigned> ByteOffset) const override;
347352

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -march=hexagon -verify-machineinstrs < %s | FileCheck %s
3+
4+
@.str = private unnamed_addr constant [31 x i8] c"DHRYSTONE PROGRAM, 3'RD STRING\00", align 1
5+
@.str1 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
6+
7+
; Function Attrs: nounwind
8+
declare i32 @printf(i8* nocapture readonly, ...)
9+
10+
; Function Attrs: nounwind
11+
define i32 @main() {
12+
; CHECK-LABEL: main:
13+
; CHECK: .cfi_startproc
14+
; CHECK-NEXT: // %bb.0: // %entry
15+
; CHECK-NEXT: .cfi_def_cfa r30, 8
16+
; CHECK-NEXT: .cfi_offset r31, -4
17+
; CHECK-NEXT: .cfi_offset r30, -8
18+
; CHECK-NEXT: {
19+
; CHECK-NEXT: r0 = ##.L.str1
20+
; CHECK-NEXT: r3:2 = CONST64(#2325073635944967245)
21+
; CHECK-NEXT: allocframe(r29,#40):raw
22+
; CHECK-NEXT: }
23+
; CHECK-NEXT: {
24+
; CHECK-NEXT: r1 = add(r29,#8)
25+
; CHECK-NEXT: r7:6 = CONST64(#4706902966564560965)
26+
; CHECK-NEXT: r5:4 = CONST64(#5642821575076104260)
27+
; CHECK-NEXT: }
28+
; CHECK-NEXT: {
29+
; CHECK-NEXT: memb(r29+#38) = #0
30+
; CHECK-NEXT: memw(r29+#0) = r1
31+
; CHECK-NEXT: }
32+
; CHECK-NEXT: {
33+
; CHECK-NEXT: memd(r29+#24) = r3:2
34+
; CHECK-NEXT: memd(r29+#16) = r7:6
35+
; CHECK-NEXT: }
36+
; CHECK-NEXT: {
37+
; CHECK-NEXT: memd(r29+#8) = r5:4
38+
; CHECK-NEXT: memh(r29+#36) = ##18254
39+
; CHECK-NEXT: }
40+
; CHECK-NEXT: {
41+
; CHECK-NEXT: call printf
42+
; CHECK-NEXT: memw(r29+#32) = ##1230132307
43+
; CHECK-NEXT: }
44+
; CHECK-NEXT: {
45+
; CHECK-NEXT: r0 = #0
46+
; CHECK-NEXT: dealloc_return
47+
; CHECK-NEXT: }
48+
entry:
49+
%blah = alloca [30 x i8], align 8
50+
%arraydecay = getelementptr inbounds [30 x i8], [30 x i8]* %blah, i32 0, i32 0
51+
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %arraydecay, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i32 0, i32 0), i32 31, i32 1, i1 false)
52+
%call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str1, i32 0, i32 0), i8* %arraydecay)
53+
ret i32 0
54+
}
55+
56+
; Function Attrs: nounwind
57+
declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture readonly, i32, i32, i1)

0 commit comments

Comments
 (0)