Skip to content

Commit 239a174

Browse files
committed
[RISCV] Prevent constant hoisting for or/and/xor that can use bseti/bclri/binvi.
Reviewed By: reames Differential Revision: https://reviews.llvm.org/D140928
1 parent b58927e commit 239a174

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,22 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
124124
// zext.w
125125
if (Imm == UINT64_C(0xffffffff) && ST->hasStdExtZba())
126126
return TTI::TCC_Free;
127+
// bclri
128+
if (ST->hasStdExtZbs() && (~Imm).isPowerOf2())
129+
return TTI::TCC_Free;
127130
if (Inst && Idx == 1 && Imm.getBitWidth() <= ST->getXLen() &&
128131
canUseShiftPair(Inst, Imm))
129132
return TTI::TCC_Free;
130-
[[fallthrough]];
133+
Takes12BitImm = true;
134+
break;
131135
case Instruction::Add:
136+
Takes12BitImm = true;
137+
break;
132138
case Instruction::Or:
133139
case Instruction::Xor:
140+
// bseti/binvi
141+
if (ST->hasStdExtZbs() && Imm.isPowerOf2())
142+
return TTI::TCC_Free;
134143
Takes12BitImm = true;
135144
break;
136145
case Instruction::Mul:

llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,30 @@ define i32 @test10(i32 %a, i32 %b) nounwind {
9595
%5 = mul i32 %2, %4
9696
ret i32 %5
9797
}
98+
99+
; bseti
100+
define i64 @test11(i64 %a) nounwind "target-features"="+zbs" {
101+
; CHECK-LABEL: test11
102+
; CHECK: or i64 %a, 8589934592
103+
%1 = or i64 %a, 8589934592 ; 1 << 33
104+
%2 = or i64 %1, 8589934592 ; 1 << 33
105+
ret i64 %2
106+
}
107+
108+
; binvi
109+
define i64 @test12(i64 %a) nounwind "target-features"="+zbs" {
110+
; CHECK-LABEL: test12
111+
; CHECK: xor i64 %a, -9223372036854775808
112+
%1 = xor i64 %a, -9223372036854775808 ; 1 << 63
113+
%2 = xor i64 %1, -9223372036854775808 ; 1 << 63
114+
ret i64 %2
115+
}
116+
117+
; bclri
118+
define i64 @test13(i64 %a) nounwind "target-features"="+zbs" {
119+
; CHECK-LABEL: test13
120+
; CHECK: and i64 %a, -281474976710657
121+
%1 = and i64 %a, -281474976710657 ; ~(1 << 48)
122+
%2 = and i64 %1, -281474976710657 ; ~(1 << 48)
123+
ret i64 %2
124+
}

0 commit comments

Comments
 (0)