Skip to content

Commit e22b07e

Browse files
authored
[SandboxIR][NFC] Move Function class to a separate file (#110526)
1 parent fcb5905 commit e22b07e

File tree

21 files changed

+145
-96
lines changed

21 files changed

+145
-96
lines changed

llvm/benchmarks/SandboxIRBench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/IR/Function.h"
1919
#include "llvm/IR/Instruction.h"
2020
#include "llvm/IR/Module.h"
21+
#include "llvm/SandboxIR/Function.h"
2122
#include "llvm/SandboxIR/Instruction.h"
2223
#include "llvm/SandboxIR/Module.h"
2324
#include "llvm/Support/SourceMgr.h"

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,59 +1227,6 @@ class ConstantTokenNone final : public Constant {
12271227
#endif
12281228
};
12291229

1230-
class Function : public GlobalWithNodeAPI<Function, llvm::Function,
1231-
GlobalObject, llvm::GlobalObject> {
1232-
/// Helper for mapped_iterator.
1233-
struct LLVMBBToBB {
1234-
Context &Ctx;
1235-
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
1236-
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
1237-
return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
1238-
}
1239-
};
1240-
/// Use Context::createFunction() instead.
1241-
Function(llvm::Function *F, sandboxir::Context &Ctx)
1242-
: GlobalWithNodeAPI(ClassID::Function, F, Ctx) {}
1243-
friend class Context; // For constructor.
1244-
1245-
public:
1246-
/// For isa/dyn_cast.
1247-
static bool classof(const sandboxir::Value *From) {
1248-
return From->getSubclassID() == ClassID::Function;
1249-
}
1250-
1251-
Module *getParent() {
1252-
return Ctx.getModule(cast<llvm::Function>(Val)->getParent());
1253-
}
1254-
1255-
Argument *getArg(unsigned Idx) const {
1256-
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
1257-
return cast<Argument>(Ctx.getValue(Arg));
1258-
}
1259-
1260-
size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
1261-
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
1262-
1263-
using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
1264-
iterator begin() const {
1265-
LLVMBBToBB BBGetter(Ctx);
1266-
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
1267-
}
1268-
iterator end() const {
1269-
LLVMBBToBB BBGetter(Ctx);
1270-
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
1271-
}
1272-
FunctionType *getFunctionType() const;
1273-
1274-
#ifndef NDEBUG
1275-
void verify() const final {
1276-
assert(isa<llvm::Function>(Val) && "Expected Function!");
1277-
}
1278-
void dumpNameAndArgs(raw_ostream &OS) const;
1279-
void dumpOS(raw_ostream &OS) const final;
1280-
#endif
1281-
};
1282-
12831230
} // namespace llvm::sandboxir
12841231

12851232
#endif // LLVM_SANDBOXIR_CONSTANT_H
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===- Function.h -----------------------------------------------*- C++ -*-===//
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+
#ifndef LLVM_SANDBOXIR_FUNCTION_H
10+
#define LLVM_SANDBOXIR_FUNCTION_H
11+
12+
#include "llvm/IR/Function.h"
13+
#include "llvm/SandboxIR/Constant.h"
14+
15+
namespace llvm::sandboxir {
16+
17+
class Function : public GlobalWithNodeAPI<Function, llvm::Function,
18+
GlobalObject, llvm::GlobalObject> {
19+
/// Helper for mapped_iterator.
20+
struct LLVMBBToBB {
21+
Context &Ctx;
22+
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
23+
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
24+
return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
25+
}
26+
};
27+
/// Use Context::createFunction() instead.
28+
Function(llvm::Function *F, sandboxir::Context &Ctx)
29+
: GlobalWithNodeAPI(ClassID::Function, F, Ctx) {}
30+
friend class Context; // For constructor.
31+
32+
public:
33+
/// For isa/dyn_cast.
34+
static bool classof(const sandboxir::Value *From) {
35+
return From->getSubclassID() == ClassID::Function;
36+
}
37+
38+
Module *getParent() {
39+
return Ctx.getModule(cast<llvm::Function>(Val)->getParent());
40+
}
41+
42+
Argument *getArg(unsigned Idx) const {
43+
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
44+
return cast<Argument>(Ctx.getValue(Arg));
45+
}
46+
47+
size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
48+
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
49+
50+
using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
51+
iterator begin() const {
52+
LLVMBBToBB BBGetter(Ctx);
53+
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
54+
}
55+
iterator end() const {
56+
LLVMBBToBB BBGetter(Ctx);
57+
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
58+
}
59+
FunctionType *getFunctionType() const;
60+
61+
#ifndef NDEBUG
62+
void verify() const final {
63+
assert(isa<llvm::Function>(Val) && "Expected Function!");
64+
}
65+
void dumpNameAndArgs(raw_ostream &OS) const;
66+
void dumpOS(raw_ostream &OS) const final;
67+
#endif
68+
};
69+
70+
} // namespace llvm::sandboxir
71+
72+
#endif // LLVM_SANDBOXIR_FUNCTION_H

llvm/lib/SandboxIR/BasicBlock.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/SandboxIR/BasicBlock.h"
1010
#include "llvm/SandboxIR/Context.h"
11+
#include "llvm/SandboxIR/Function.h"
1112
#include "llvm/SandboxIR/Instruction.h"
1213

1314
namespace llvm::sandboxir {

llvm/lib/SandboxIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMSandboxIR
33
BasicBlock.cpp
44
Constant.cpp
55
Context.cpp
6+
Function.cpp
67
Instruction.cpp
78
Module.cpp
89
Pass.cpp

llvm/lib/SandboxIR/Constant.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/SandboxIR/Argument.h"
1111
#include "llvm/SandboxIR/BasicBlock.h"
1212
#include "llvm/SandboxIR/Context.h"
13+
#include "llvm/SandboxIR/Function.h"
1314

1415
namespace llvm::sandboxir {
1516

@@ -467,44 +468,4 @@ GlobalValue *DSOLocalEquivalent::getGlobalValue() const {
467468
Ctx.getValue(cast<llvm::DSOLocalEquivalent>(Val)->getGlobalValue()));
468469
}
469470

470-
FunctionType *Function::getFunctionType() const {
471-
return cast<FunctionType>(
472-
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
473-
}
474-
475-
#ifndef NDEBUG
476-
void Function::dumpNameAndArgs(raw_ostream &OS) const {
477-
auto *F = cast<llvm::Function>(Val);
478-
OS << *F->getReturnType() << " @" << F->getName() << "(";
479-
interleave(
480-
F->args(),
481-
[this, &OS](const llvm::Argument &LLVMArg) {
482-
auto *SBArg = cast_or_null<Argument>(Ctx.getValue(&LLVMArg));
483-
if (SBArg == nullptr)
484-
OS << "NULL";
485-
else
486-
SBArg->printAsOperand(OS);
487-
},
488-
[&] { OS << ", "; });
489-
OS << ")";
490-
}
491-
492-
void Function::dumpOS(raw_ostream &OS) const {
493-
dumpNameAndArgs(OS);
494-
OS << " {\n";
495-
auto *LLVMF = cast<llvm::Function>(Val);
496-
interleave(
497-
*LLVMF,
498-
[this, &OS](const llvm::BasicBlock &LLVMBB) {
499-
auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
500-
if (BB == nullptr)
501-
OS << "NULL";
502-
else
503-
OS << *BB;
504-
},
505-
[&OS] { OS << "\n"; });
506-
OS << "}\n";
507-
}
508-
#endif // NDEBUG
509-
510471
} // namespace llvm::sandboxir

llvm/lib/SandboxIR/Context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/SandboxIR/Context.h"
10+
#include "llvm/SandboxIR/Function.h"
1011
#include "llvm/SandboxIR/Instruction.h"
1112
#include "llvm/SandboxIR/Module.h"
1213

llvm/lib/SandboxIR/Function.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===- Function.cpp - The Function class of Sandbox IR --------------------===//
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 "llvm/SandboxIR/Function.h"
10+
#include "llvm/IR/Value.h"
11+
#include "llvm/SandboxIR/Context.h"
12+
13+
namespace llvm::sandboxir {
14+
15+
FunctionType *Function::getFunctionType() const {
16+
return cast<FunctionType>(
17+
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
18+
}
19+
20+
#ifndef NDEBUG
21+
void Function::dumpNameAndArgs(raw_ostream &OS) const {
22+
auto *F = cast<llvm::Function>(Val);
23+
OS << *F->getReturnType() << " @" << F->getName() << "(";
24+
interleave(
25+
F->args(),
26+
[this, &OS](const llvm::Argument &LLVMArg) {
27+
auto *SBArg = cast_or_null<Argument>(Ctx.getValue(&LLVMArg));
28+
if (SBArg == nullptr)
29+
OS << "NULL";
30+
else
31+
SBArg->printAsOperand(OS);
32+
},
33+
[&] { OS << ", "; });
34+
OS << ")";
35+
}
36+
37+
void Function::dumpOS(raw_ostream &OS) const {
38+
dumpNameAndArgs(OS);
39+
OS << " {\n";
40+
auto *LLVMF = cast<llvm::Function>(Val);
41+
interleave(
42+
*LLVMF,
43+
[this, &OS](const llvm::BasicBlock &LLVMBB) {
44+
auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB));
45+
if (BB == nullptr)
46+
OS << "NULL";
47+
else
48+
OS << *BB;
49+
},
50+
[&OS] { OS << "\n"; });
51+
OS << "}\n";
52+
}
53+
#endif // NDEBUG
54+
55+
} // namespace llvm::sandboxir

llvm/lib/SandboxIR/Instruction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/SandboxIR/Instruction.h"
10+
#include "llvm/SandboxIR/Function.h"
1011

1112
namespace llvm::sandboxir {
1213

llvm/lib/SandboxIR/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/SandboxIR/Module.h"
1010
#include "llvm/SandboxIR/Constant.h"
1111
#include "llvm/SandboxIR/Context.h"
12+
#include "llvm/SandboxIR/Function.h"
1213
#include "llvm/SandboxIR/Value.h"
1314

1415
using namespace llvm::sandboxir;

llvm/lib/SandboxIR/Region.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/SandboxIR/Region.h"
10+
#include "llvm/SandboxIR/Function.h"
1011

1112
namespace llvm::sandboxir {
1213

llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Passes/BottomUpVec.h"
1010
#include "llvm/ADT/SmallVector.h"
11+
#include "llvm/SandboxIR/Function.h"
1112
#include "llvm/SandboxIR/Instruction.h"
1213

1314
using namespace llvm::sandboxir;

llvm/unittests/SandboxIR/PassTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/IR/Module.h"
1212
#include "llvm/SandboxIR/Constant.h"
1313
#include "llvm/SandboxIR/Context.h"
14+
#include "llvm/SandboxIR/Function.h"
1415
#include "llvm/SandboxIR/PassManager.h"
1516
#include "llvm/Support/SourceMgr.h"
1617
#include "gtest/gtest.h"

llvm/unittests/SandboxIR/RegionTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "llvm/SandboxIR/Region.h"
1010
#include "llvm/AsmParser/Parser.h"
11-
#include "llvm/SandboxIR/Constant.h"
1211
#include "llvm/SandboxIR/Context.h"
12+
#include "llvm/SandboxIR/Function.h"
1313
#include "llvm/SandboxIR/Instruction.h"
1414
#include "llvm/Support/SourceMgr.h"
1515
#include "gmock/gmock-matchers.h"

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/IR/Module.h"
1616
#include "llvm/SandboxIR/BasicBlock.h"
1717
#include "llvm/SandboxIR/Constant.h"
18+
#include "llvm/SandboxIR/Function.h"
1819
#include "llvm/SandboxIR/Instruction.h"
1920
#include "llvm/SandboxIR/Module.h"
2021
#include "llvm/SandboxIR/Utils.h"

llvm/unittests/SandboxIR/TrackerTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/IR/Function.h"
1212
#include "llvm/IR/Instruction.h"
1313
#include "llvm/IR/Module.h"
14+
#include "llvm/SandboxIR/Function.h"
1415
#include "llvm/SandboxIR/Instruction.h"
1516
#include "llvm/Support/SourceMgr.h"
1617
#include "gmock/gmock-matchers.h"

llvm/unittests/SandboxIR/TypesTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/IR/Module.h"
1717
#include "llvm/SandboxIR/Constant.h"
1818
#include "llvm/SandboxIR/Context.h"
19+
#include "llvm/SandboxIR/Function.h"
1920
#include "llvm/Support/SourceMgr.h"
2021
#include "gtest/gtest.h"
2122

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/IR/Module.h"
2121
#include "llvm/SandboxIR/Constant.h"
2222
#include "llvm/SandboxIR/Context.h"
23+
#include "llvm/SandboxIR/Function.h"
2324
#include "llvm/Support/SourceMgr.h"
2425
#include "gtest/gtest.h"
2526

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/DependencyGraphTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h"
1010
#include "llvm/AsmParser/Parser.h"
11-
#include "llvm/SandboxIR/Constant.h"
1211
#include "llvm/SandboxIR/Context.h"
12+
#include "llvm/SandboxIR/Function.h"
1313
#include "llvm/SandboxIR/Instruction.h"
1414
#include "llvm/Support/SourceMgr.h"
1515
#include "gmock/gmock-matchers.h"

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/IntervalTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"
1010
#include "llvm/AsmParser/Parser.h"
11-
#include "llvm/SandboxIR/Constant.h"
1211
#include "llvm/SandboxIR/Context.h"
12+
#include "llvm/SandboxIR/Function.h"
1313
#include "llvm/SandboxIR/Instruction.h"
1414
#include "llvm/Support/SourceMgr.h"
1515
#include "gtest/gtest.h"

llvm/unittests/Transforms/Vectorize/SandboxVectorizer/LegalityTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Legality.h"
1010
#include "llvm/AsmParser/Parser.h"
11+
#include "llvm/SandboxIR/Function.h"
1112
#include "llvm/SandboxIR/Instruction.h"
1213
#include "llvm/Support/SourceMgr.h"
1314
#include "gtest/gtest.h"

0 commit comments

Comments
 (0)