Skip to content

Commit 5213381

Browse files
committed
GlobalISel: Make buildConstant handle vectors
Produce a splat build_vector similar to how SelectionDAG::getConstant does. llvm-svn: 351880
1 parent fea3731 commit 5213381

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

llvm/include/llvm/Support/LowLevelTypeImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class LLT {
103103
return getScalarSizeInBits() * getNumElements();
104104
}
105105

106+
LLT getScalarType() const {
107+
return isVector() ? getElementType() : *this;
108+
}
109+
106110
unsigned getScalarSizeInBits() const {
107111
assert(RawData != 0 && "Invalid Type");
108112
if (!IsVector) {

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,28 @@ MachineInstrBuilder MachineIRBuilder::buildCopy(const DstOp &Res,
242242
MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
243243
const ConstantInt &Val) {
244244
LLT Ty = Res.getLLTTy(*getMRI());
245+
LLT EltTy = Ty.getScalarType();
245246

246247
const ConstantInt *NewVal = &Val;
247-
if (Ty.getScalarSizeInBits() != Val.getBitWidth())
248-
NewVal = ConstantInt::get(getMF().getFunction().getContext(),
249-
Val.getValue().sextOrTrunc(Ty.getSizeInBits()));
248+
if (EltTy.getSizeInBits() != Val.getBitWidth()) {
249+
NewVal = ConstantInt::get(
250+
getMF().getFunction().getContext(),
251+
Val.getValue().sextOrTrunc(EltTy.getSizeInBits()));
252+
}
253+
254+
if (Ty.isVector()) {
255+
unsigned EltReg = getMRI()->createGenericVirtualRegister(EltTy);
256+
buildInstr(TargetOpcode::G_CONSTANT)
257+
.addDef(EltReg)
258+
.addCImm(NewVal);
259+
260+
auto MIB = buildInstr(TargetOpcode::G_BUILD_VECTOR);
261+
Res.addDefToMIB(*getMRI(), MIB);
262+
263+
for (unsigned I = 0, E = Ty.getNumElements(); I != E; ++I)
264+
MIB.addUse(EltReg);
265+
return MIB;
266+
}
250267

251268
auto MIB = buildInstr(TargetOpcode::G_CONSTANT);
252269
Res.addDefToMIB(*getMRI(), MIB);
@@ -264,7 +281,24 @@ MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
264281

265282
MachineInstrBuilder MachineIRBuilder::buildFConstant(const DstOp &Res,
266283
const ConstantFP &Val) {
267-
assert(!Res.getLLTTy(*getMRI()).isPointer() && "invalid operand type");
284+
LLT Ty = Res.getLLTTy(*getMRI());
285+
286+
assert(!Ty.isPointer() && "invalid operand type");
287+
288+
if (Ty.isVector()) {
289+
unsigned EltReg
290+
= getMRI()->createGenericVirtualRegister(Ty.getElementType());
291+
buildInstr(TargetOpcode::G_FCONSTANT)
292+
.addDef(EltReg)
293+
.addFPImm(&Val);
294+
295+
auto MIB = buildInstr(TargetOpcode::G_BUILD_VECTOR);
296+
Res.addDefToMIB(*getMRI(), MIB);
297+
298+
for (unsigned I = 0, E = Ty.getNumElements(); I != E; ++I)
299+
MIB.addUse(EltReg);
300+
return MIB;
301+
}
268302

269303
auto MIB = buildInstr(TargetOpcode::G_FCONSTANT);
270304
Res.addDefToMIB(*getMRI(), MIB);

llvm/unittests/CodeGen/GlobalISel/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ set(LLVM_LINK_COMPONENTS
1010
)
1111

1212
add_llvm_unittest(GlobalISelTests
13-
LegalizerInfoTest.cpp
14-
PatternMatchTest.cpp
15-
LegalizerHelperTest.cpp
16-
CSETest.cpp
17-
)
13+
CSETest.cpp
14+
LegalizerHelperTest.cpp
15+
LegalizerInfoTest.cpp
16+
MachineIRBuilderTest.cpp
17+
PatternMatchTest.cpp
18+
)

llvm/unittests/CodeGen/GlobalISel/GISelMITest.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//===- GISelMITest.h
2-
//-----------------------------------------------===//
1+
//===- GISelMITest.h --------------------------------------------*- C++ -*-===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===- MachineIRBuilderTest.cpp -------------------------------------------===//
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 "GISelMITest.h"
10+
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
11+
12+
TEST_F(GISelMITest, TestBuildConstantFConstant) {
13+
if (!TM)
14+
return;
15+
16+
MachineIRBuilder B(*MF);
17+
B.setInsertPt(*EntryMBB, EntryMBB->begin());
18+
19+
B.buildConstant(LLT::scalar(32), 42);
20+
B.buildFConstant(LLT::scalar(32), 1.0);
21+
22+
B.buildConstant(LLT::vector(2, 32), 99);
23+
B.buildFConstant(LLT::vector(2, 32), 2.0);
24+
25+
auto CheckStr = R"(
26+
CHECK: [[CONST0:%[0-9]+]]:_(s32) = G_CONSTANT i32 42
27+
CHECK: [[FCONST0:%[0-9]+]]:_(s32) = G_FCONSTANT float 1.000000e+00
28+
CHECK: [[CONST1:%[0-9]+]]:_(s32) = G_CONSTANT i32 99
29+
CHECK: [[VEC0:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[CONST1]]:_(s32), [[CONST1]]:_(s32)
30+
CHECK: [[FCONST1:%[0-9]+]]:_(s32) = G_FCONSTANT double 2.000000e+00
31+
CHECK: [[VEC1:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[FCONST1]]:_(s32), [[FCONST1]]:_(s32)
32+
33+
)";
34+
35+
ASSERT_TRUE(CheckMachineFunction(*MF, CheckStr));
36+
}

0 commit comments

Comments
 (0)