llvm.org GIT mirror llvm / 73d2cc5
MIR Serialization: Serialize MBB successors. This commit implements serialization of the machine basic block successors. It uses a YAML flow sequence that contains strings that have the MBB references. The MBB references in those strings use the same syntax as the MBB machine operands in the machine instruction strings. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D10699 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241093 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Lorenz 5 years ago
8 changed file(s) with 189 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
5454 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
5555 };
5656
57 struct FlowStringValue : StringValue {
58 FlowStringValue() {}
59 FlowStringValue(std::string Value) : StringValue(Value) {}
60 };
61
62 template <> struct ScalarTraits {
63 static void output(const FlowStringValue &S, void *, llvm::raw_ostream &OS) {
64 return ScalarTraits::output(S, nullptr, OS);
65 }
66
67 static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
68 return ScalarTraits::input(Scalar, Ctx, S);
69 }
70
71 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
72 };
73
5774 } // end namespace yaml
5875 } // end namespace llvm
5976
6077 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
78 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
6179
6280 namespace llvm {
6381 namespace yaml {
6886 unsigned Alignment = 0;
6987 bool IsLandingPad = false;
7088 bool AddressTaken = false;
71 // TODO: Serialize the successors and liveins.
89 // TODO: Serialize the successor weights and liveins.
90 std::vector Successors;
7291
7392 std::vector Instructions;
7493 };
81100 YamlIO.mapOptional("alignment", MBB.Alignment);
82101 YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
83102 YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
103 YamlIO.mapOptional("successors", MBB.Successors);
84104 YamlIO.mapOptional("instructions", MBB.Instructions);
85105 }
86106 };
6363 bool error(StringRef::iterator Loc, const Twine &Msg);
6464
6565 bool parse(MachineInstr *&MI);
66 bool parseMBB(MachineBasicBlock *&MBB);
6667
6768 bool parseRegister(unsigned &Reg);
6869 bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
6970 bool parseImmediateOperand(MachineOperand &Dest);
71 bool parseMBBReference(MachineBasicBlock *&MBB);
7072 bool parseMBBOperand(MachineOperand &Dest);
7173 bool parseGlobalAddressOperand(MachineOperand &Dest);
7274 bool parseMachineOperand(MachineOperand &Dest);
185187 return false;
186188 }
187189
190 bool MIParser::parseMBB(MachineBasicBlock *&MBB) {
191 lex();
192 if (Token.isNot(MIToken::MachineBasicBlock))
193 return error("expected a machine basic block reference");
194 if (parseMBBReference(MBB))
195 return true;
196 lex();
197 if (Token.isNot(MIToken::Eof))
198 return error(
199 "expected end of string after the machine basic block reference");
200 return false;
201 }
202
188203 bool MIParser::parseInstruction(unsigned &OpCode) {
189204 if (Token.isNot(MIToken::Identifier))
190205 return error("expected a machine instruction");
245260 return false;
246261 }
247262
248 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
263 bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
249264 assert(Token.is(MIToken::MachineBasicBlock));
250265 unsigned Number;
251266 if (getUnsigned(Number))
254269 if (MBBInfo == MBBSlots.end())
255270 return error(Twine("use of undefined machine basic block #") +
256271 Twine(Number));
257 MachineBasicBlock *MBB = MBBInfo->second;
272 MBB = MBBInfo->second;
258273 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
259274 return error(Twine("the name of machine basic block #") + Twine(Number) +
260275 " isn't '" + Token.stringValue() + "'");
276 return false;
277 }
278
279 bool MIParser::parseMBBOperand(MachineOperand &Dest) {
280 MachineBasicBlock *MBB;
281 if (parseMBBReference(MBB))
282 return true;
261283 Dest = MachineOperand::CreateMBB(MBB);
262284 lex();
263285 return false;
391413 const SlotMapping &IRSlots, SMDiagnostic &Error) {
392414 return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parse(MI);
393415 }
416
417 bool llvm::parseMBBReference(
418 MachineBasicBlock *&MBB, SourceMgr &SM, MachineFunction &MF, StringRef Src,
419 const DenseMap &MBBSlots,
420 const SlotMapping &IRSlots, SMDiagnostic &Error) {
421 return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parseMBB(MBB);
422 }
3030 const DenseMap &MBBSlots,
3131 const SlotMapping &IRSlots, SMDiagnostic &Error);
3232
33 bool parseMBBReference(MachineBasicBlock *&MBB, SourceMgr &SM,
34 MachineFunction &MF, StringRef Src,
35 const DenseMap &MBBSlots,
36 const SlotMapping &IRSlots, SMDiagnostic &Error);
37
3338 } // end namespace llvm
3439
3540 #endif
273273 if (YamlMBB.AddressTaken)
274274 MBB.setHasAddressTaken();
275275 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
276 SMDiagnostic Error;
277 // Parse the successors.
278 for (const auto &MBBSource : YamlMBB.Successors) {
279 MachineBasicBlock *SuccMBB = nullptr;
280 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, MBBSlots, IRSlots,
281 Error))
282 return error(Error, MBBSource.SourceRange);
283 // TODO: Report an error when adding the same successor more than once.
284 MBB.addSuccessor(SuccMBB);
285 }
276286 // Parse the instructions.
277287 for (const auto &MISource : YamlMBB.Instructions) {
278 SMDiagnostic Error;
279288 MachineInstr *MI = nullptr;
280289 if (parseMachineInstr(MI, SM, MF, MISource.Value, MBBSlots, IRSlots, Error))
281290 return error(Error, MISource.SourceRange);
130130 YamlMBB.Alignment = MBB.getAlignment();
131131 YamlMBB.AddressTaken = MBB.hasAddressTaken();
132132 YamlMBB.IsLandingPad = MBB.isLandingPad();
133 for (const auto *MBB : MBB.successors()) {
134 std::string Str;
135 raw_string_ostream StrOS(Str);
136 MIPrinter(M, StrOS, RegisterMaskIds).printMBBReference(*MBB);
137 YamlMBB.Successors.push_back(StrOS.str());
138 }
133139
134140 // Print the machine instructions.
135141 YamlMBB.Instructions.reserve(MBB.size());
0 # RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1
2 --- |
3
4 define i32 @foo(i32 %a) {
5 entry:
6 %0 = icmp sle i32 %a, 10
7 br i1 %0, label %less, label %exit
8
9 less:
10 ret i32 0
11
12 exit:
13 ret i32 %a
14 }
15
16 ...
17 ---
18 name: foo
19 body:
20 - id: 0
21 name: entry
22 # CHECK: [[@LINE+1]]:46: expected end of string after the machine basic block reference
23 successors: [ '%bb.1.less', '%bb.2.exit 2' ]
24 - id: 1
25 name: less
26 - id: 2
27 name: exit
28 ...
0 # RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1
2 --- |
3
4 define i32 @foo(i32 %a) {
5 entry:
6 %0 = icmp sle i32 %a, 10
7 br i1 %0, label %less, label %exit
8
9 less:
10 ret i32 0
11
12 exit:
13 ret i32 %a
14 }
15
16 ...
17 ---
18 name: foo
19 body:
20 - id: 0
21 name: entry
22 # CHECK: [[@LINE+1]]:35: expected a machine basic block reference
23 successors: [ '%bb.1.less', '2' ]
24 - id: 1
25 name: less
26 - id: 2
27 name: exit
28 ...
0 # RUN: llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s
1 # This test ensures that the MIR parser parses basic block successors correctly.
2
3 --- |
4
5 define i32 @foo(i32 %a) {
6 entry:
7 %0 = icmp sle i32 %a, 10
8 br i1 %0, label %less, label %exit
9
10 less:
11 ret i32 0
12
13 exit:
14 ret i32 %a
15 }
16
17 define i32 @bar(i32 %a) {
18 entry:
19 %b = icmp sle i32 %a, 10
20 br i1 %b, label %0, label %1
21
22 ;
23 ret i32 0
24
25 ;
26 ret i32 %a
27 }
28
29 ...
30 ---
31 name: foo
32 body:
33 # CHECK: name: entry
34 # CHECK: successors: [ '%bb.1.less', '%bb.2.exit' ]
35 # CHECK: name: less
36 - id: 0
37 name: entry
38 successors: [ '%bb.1.less', '%bb.2.exit' ]
39 - id: 1
40 name: less
41 - id: 2
42 name: exit
43 ...
44 ---
45 name: bar
46 body:
47 # CHECK: name: bar
48 # CHECK: name: entry
49 # CHECK: successors: [ '%bb.1', '%bb.2' ]
50 # CHECK: id: 1
51 # CHECK: id: 2
52 - id: 0
53 name: entry
54 successors: [ '%bb.1', '%bb.2' ]
55 - id: 1
56 - id: 2
57 ...