Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c249168

Browse files
committed
MIR Serialization: Serialize the sub register indices.
This commit serializes the sub register indices from the register machine operands. Reviewers: Duncan P. N. Exon Smith git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242084 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 195dc69 commit c249168

File tree

7 files changed

+146
-3
lines changed

7 files changed

+146
-3
lines changed

lib/CodeGen/MIRParser/MILexer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ static MIToken::TokenKind symbolToken(char C) {
179179
return MIToken::comma;
180180
case '=':
181181
return MIToken::equal;
182+
case ':':
183+
return MIToken::colon;
182184
default:
183185
return MIToken::Error;
184186
}

lib/CodeGen/MIRParser/MILexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct MIToken {
3535
comma,
3636
equal,
3737
underscore,
38+
colon,
3839

3940
// Keywords
4041
kw_implicit,

lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class MIParser {
5656
StringMap<unsigned> Names2Regs;
5757
/// Maps from register mask names to register masks.
5858
StringMap<const uint32_t *> Names2RegMasks;
59+
/// Maps from subregister names to subregister indices.
60+
StringMap<unsigned> Names2SubRegIndices;
5961

6062
public:
6163
MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error,
@@ -79,6 +81,7 @@ class MIParser {
7981

8082
bool parseRegister(unsigned &Reg);
8183
bool parseRegisterFlag(unsigned &Flags);
84+
bool parseSubRegisterIndex(unsigned &SubReg);
8285
bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
8386
bool parseImmediateOperand(MachineOperand &Dest);
8487
bool parseMBBReference(MachineBasicBlock *&MBB);
@@ -115,6 +118,13 @@ class MIParser {
115118
///
116119
/// Return null if the identifier isn't a register mask.
117120
const uint32_t *getRegMask(StringRef Identifier);
121+
122+
void initNames2SubRegIndices();
123+
124+
/// Check if the given identifier is a name of a subregister index.
125+
///
126+
/// Return 0 if the name isn't a subregister index class.
127+
unsigned getSubRegIndex(StringRef Name);
118128
};
119129

120130
} // end anonymous namespace
@@ -332,6 +342,19 @@ bool MIParser::parseRegisterFlag(unsigned &Flags) {
332342
return false;
333343
}
334344

345+
bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
346+
assert(Token.is(MIToken::colon));
347+
lex();
348+
if (Token.isNot(MIToken::Identifier))
349+
return error("expected a subregister index after ':'");
350+
auto Name = Token.stringValue();
351+
SubReg = getSubRegIndex(Name);
352+
if (!SubReg)
353+
return error(Twine("use of unknown subregister index '") + Name + "'");
354+
lex();
355+
return false;
356+
}
357+
335358
bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
336359
unsigned Reg;
337360
unsigned Flags = IsDef ? RegState::Define : 0;
@@ -344,10 +367,15 @@ bool MIParser::parseRegisterOperand(MachineOperand &Dest, bool IsDef) {
344367
if (parseRegister(Reg))
345368
return true;
346369
lex();
347-
// TODO: Parse subregister.
370+
unsigned SubReg = 0;
371+
if (Token.is(MIToken::colon)) {
372+
if (parseSubRegisterIndex(SubReg))
373+
return true;
374+
}
348375
Dest = MachineOperand::CreateReg(
349376
Reg, Flags & RegState::Define, Flags & RegState::Implicit,
350-
Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef);
377+
Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
378+
/*isEarlyClobber=*/false, SubReg);
351379
return false;
352380
}
353381

@@ -525,6 +553,23 @@ const uint32_t *MIParser::getRegMask(StringRef Identifier) {
525553
return RegMaskInfo->getValue();
526554
}
527555

556+
void MIParser::initNames2SubRegIndices() {
557+
if (!Names2SubRegIndices.empty())
558+
return;
559+
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
560+
for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
561+
Names2SubRegIndices.insert(
562+
std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
563+
}
564+
565+
unsigned MIParser::getSubRegIndex(StringRef Name) {
566+
initNames2SubRegIndices();
567+
auto SubRegInfo = Names2SubRegIndices.find(Name);
568+
if (SubRegInfo == Names2SubRegIndices.end())
569+
return 0;
570+
return SubRegInfo->getValue();
571+
}
572+
528573
bool llvm::parseMachineInstr(MachineInstr *&MI, SourceMgr &SM,
529574
MachineFunction &MF, StringRef Src,
530575
const PerFunctionMIParsingState &PFS,

lib/CodeGen/MIRPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) {
303303
if (Op.isUndef())
304304
OS << "undef ";
305305
printReg(Op.getReg(), OS, TRI);
306-
// TODO: Print sub register.
306+
// Print the sub register.
307+
if (Op.getSubReg() != 0)
308+
OS << ':' << TRI->getSubRegIndexName(Op.getSubReg());
307309
break;
308310
case MachineOperand::MO_Immediate:
309311
OS << Op.getImm();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# RUN: not llc -march=x86-64 -start-after machine-sink -stop-after machine-sink -o /dev/null %s 2>&1 | FileCheck %s
2+
3+
--- |
4+
5+
define zeroext i1 @t(i1 %c) {
6+
entry:
7+
ret i1 %c
8+
}
9+
10+
...
11+
---
12+
name: t
13+
isSSA: true
14+
tracksRegLiveness: true
15+
registers:
16+
- { id: 0, class: gr32 }
17+
- { id: 1, class: gr8 }
18+
- { id: 2, class: gr8 }
19+
body:
20+
- name: entry
21+
id: 0
22+
instructions:
23+
- '%0 = COPY %edi'
24+
# CHECK: [[@LINE+1]]:25: expected a subregister index after ':'
25+
- '%1 = COPY %0 : 42'
26+
- '%2 = AND8ri %1, 1, implicit-def %eflags'
27+
- '%al = COPY %2'
28+
- 'RETQ %al'
29+
...
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# RUN: llc -march=x86-64 -start-after machine-sink -stop-after machine-sink -o /dev/null %s | FileCheck %s
2+
# This test ensures that the MIR parser parses subregisters in register operands
3+
# correctly.
4+
5+
--- |
6+
7+
define zeroext i1 @t(i1 %c) {
8+
entry:
9+
ret i1 %c
10+
}
11+
12+
...
13+
---
14+
name: t
15+
isSSA: true
16+
tracksRegLiveness: true
17+
registers:
18+
- { id: 0, class: gr32 }
19+
- { id: 1, class: gr8 }
20+
- { id: 2, class: gr8 }
21+
body:
22+
- name: entry
23+
id: 0
24+
instructions:
25+
# CHECK: %0 = COPY %edi
26+
# CHECK-NEXT: %1 = COPY %0:sub_8bit
27+
- '%0 = COPY %edi'
28+
- '%1 = COPY %0:sub_8bit'
29+
- '%2 = AND8ri %1, 1, implicit-def %eflags'
30+
- '%al = COPY %2'
31+
- 'RETQ %al'
32+
...
33+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# RUN: not llc -march=x86-64 -start-after machine-sink -stop-after machine-sink -o /dev/null %s 2>&1 | FileCheck %s
2+
# This test ensures that an error is reported when an unknown subregister index
3+
# is encountered.
4+
5+
--- |
6+
7+
define zeroext i1 @t(i1 %c) {
8+
entry:
9+
ret i1 %c
10+
}
11+
12+
...
13+
---
14+
name: t
15+
isSSA: true
16+
tracksRegLiveness: true
17+
registers:
18+
- { id: 0, class: gr32 }
19+
- { id: 1, class: gr8 }
20+
- { id: 2, class: gr8 }
21+
body:
22+
- name: entry
23+
id: 0
24+
instructions:
25+
- '%0 = COPY %edi'
26+
# CHECK: [[@LINE+1]]:23: use of unknown subregister index 'bit8'
27+
- '%1 = COPY %0:bit8'
28+
- '%2 = AND8ri %1, 1, implicit-def %eflags'
29+
- '%al = COPY %2'
30+
- 'RETQ %al'
31+
...

0 commit comments

Comments
 (0)