Skip to content

Commit ed2c001

Browse files
Hal Finkelamboar
Hal Finkel
authored andcommitted
Fix address-offset folding for plain addi
When folding an addi into a memory access that can take an immediate offset, we were implicitly assuming that the existing offset was zero. This was incorrect. If we're dealing with an addi with a plain constant, we can add it to the existing offset (assuming that doesn't overflow the immediate, etc.), but if we have anything else (i.e. something that will become a relocation expression), we'll go back to requiring the existing immediate offset to be zero (because we don't know what the requirements on that relocation expression might be - e.g. maybe it is paired with some addis in some relevant way). On the other hand, when dealing with a plain addi with a regular constant immediate, the alignment restrictions (from the TOC base pointer, etc.) are irrelevant. I've added the test case from PR30280, which demonstrated the bug, but also demonstrates a missed optimization opportunity (i.e. we don't need the memory accesses at all). Fixes PR30280. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280789 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1e14ac7 commit ed2c001

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

lib/Target/PowerPC/PPCISelDAGToDAG.cpp

+38-15
Original file line numberDiff line numberDiff line change
@@ -4371,25 +4371,48 @@ void PPCDAGToDAGISel::PeepholePPC64() {
43714371
SDValue HBase = Base.getOperand(0);
43724372

43734373
int Offset = N->getConstantOperandVal(FirstOp);
4374-
if (Offset < 0 || Offset > MaxDisplacement) {
4375-
// If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
4376-
// one use, then we can do this for any offset, we just need to also
4377-
// update the offset (i.e. the symbol addend) on the addis also.
4378-
if (Base.getMachineOpcode() != PPC::ADDItocL)
4379-
continue;
4374+
if (ReplaceFlags) {
4375+
if (Offset < 0 || Offset > MaxDisplacement) {
4376+
// If we have a addi(toc@l)/addis(toc@ha) pair, and the addis has only
4377+
// one use, then we can do this for any offset, we just need to also
4378+
// update the offset (i.e. the symbol addend) on the addis also.
4379+
if (Base.getMachineOpcode() != PPC::ADDItocL)
4380+
continue;
43804381

4381-
if (!HBase.isMachineOpcode() ||
4382-
HBase.getMachineOpcode() != PPC::ADDIStocHA)
4383-
continue;
4382+
if (!HBase.isMachineOpcode() ||
4383+
HBase.getMachineOpcode() != PPC::ADDIStocHA)
4384+
continue;
43844385

4385-
if (!Base.hasOneUse() || !HBase.hasOneUse())
4386-
continue;
4386+
if (!Base.hasOneUse() || !HBase.hasOneUse())
4387+
continue;
43874388

4388-
SDValue HImmOpnd = HBase.getOperand(1);
4389-
if (HImmOpnd != ImmOpnd)
4390-
continue;
4389+
SDValue HImmOpnd = HBase.getOperand(1);
4390+
if (HImmOpnd != ImmOpnd)
4391+
continue;
43914392

4392-
UpdateHBase = true;
4393+
UpdateHBase = true;
4394+
}
4395+
} else {
4396+
// If we're directly folding the addend from an addi instruction, then:
4397+
// 1. In general, the offset on the memory access must be zero.
4398+
// 2. If the addend is a constant, then it can be combined with a
4399+
// non-zero offset, but only if the result meets the encoding
4400+
// requirements.
4401+
if (auto *C = dyn_cast<ConstantSDNode>(ImmOpnd)) {
4402+
Offset += C->getSExtValue();
4403+
4404+
if ((StorageOpcode == PPC::LWA || StorageOpcode == PPC::LD ||
4405+
StorageOpcode == PPC::STD) && (Offset % 4) != 0)
4406+
continue;
4407+
4408+
if (!isInt<16>(Offset))
4409+
continue;
4410+
4411+
ImmOpnd = CurDAG->getTargetConstant(Offset, SDLoc(ImmOpnd),
4412+
ImmOpnd.getValueType());
4413+
} else if (Offset != 0) {
4414+
continue;
4415+
}
43934416
}
43944417

43954418
// We found an opportunity. Reverse the operands from the add
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: llc < %s | FileCheck %s
2+
target datalayout = "e-m:e-i64:64-n32:64"
3+
target triple = "powerpc64le-unknown-linux-gnu"
4+
5+
%struct.S0 = type <{ i32, [5 x i8] }>
6+
7+
; Function Attrs: norecurse nounwind readnone
8+
define signext i32 @foo([2 x i64] %a.coerce) local_unnamed_addr #0 {
9+
entry:
10+
%a = alloca %struct.S0, align 8
11+
%a.coerce.fca.0.extract = extractvalue [2 x i64] %a.coerce, 0
12+
%a.coerce.fca.1.extract = extractvalue [2 x i64] %a.coerce, 1
13+
%a.0.a.0..sroa_cast = bitcast %struct.S0* %a to i64*
14+
store i64 %a.coerce.fca.0.extract, i64* %a.0.a.0..sroa_cast, align 8
15+
%tmp.sroa.2.0.extract.trunc = trunc i64 %a.coerce.fca.1.extract to i8
16+
%a.8.a.8..sroa_idx = getelementptr inbounds %struct.S0, %struct.S0* %a, i64 0, i32 1, i64 4
17+
store i8 %tmp.sroa.2.0.extract.trunc, i8* %a.8.a.8..sroa_idx, align 8
18+
%a.4.a.4..sroa_idx = getelementptr inbounds %struct.S0, %struct.S0* %a, i64 0, i32 1
19+
%a.4.a.4..sroa_cast = bitcast [5 x i8]* %a.4.a.4..sroa_idx to i40*
20+
%a.4.a.4.bf.load = load i40, i40* %a.4.a.4..sroa_cast, align 4
21+
%bf.lshr = lshr i40 %a.4.a.4.bf.load, 31
22+
%bf.lshr.tr = trunc i40 %bf.lshr to i32
23+
%bf.cast = and i32 %bf.lshr.tr, 127
24+
ret i32 %bf.cast
25+
26+
; CHECK-LABEL: @foo
27+
; FIXME: We don't need to do these stores/loads at all.
28+
; CHECK-DAG: std 3, -24(1)
29+
; CHECK-DAG: stb 4, -16(1)
30+
; CHECK: ori 2, 2, 0
31+
; CHECK-DAG: lbz [[REG1:[0-9]+]], -16(1)
32+
; CHECK-DAG: lwz [[REG2:[0-9]+]], -20(1)
33+
; CHECK-DAG: sldi [[REG3:[0-9]+]], [[REG1]], 32
34+
; CHECK-DAG: or [[REG4:[0-9]+]], [[REG2]], [[REG3]]
35+
; CHECK: rldicl 3, [[REG4]], 33, 57
36+
; CHECK: blr
37+
}
38+
39+
attributes #0 = { nounwind "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "target-cpu"="ppc64le" }
40+

0 commit comments

Comments
 (0)