Skip to content

Commit 3eaa25b

Browse files
committed
[sil] Complement ValueBase::getParentBB() with ValueBase::get{Function,Module}().
These APIs work just like getParentBB does, namely they attempt to cast self to either SILInstruction/SILArgument and if the instance is one of those classes, using the APIs on said classes to get the relevant Function or Module. If the dynamic casts fail, then nullptr is returned.
1 parent d010a09 commit 3eaa25b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

include/swift/SIL/SILValue.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace swift {
2929
class ValueBaseUseIterator;
3030
class ValueUseIterator;
3131
class SILBasicBlock;
32+
class SILFunction;
33+
class SILModule;
3234
class SILInstruction;
3335
class SILLocation;
3436
class DominanceInfo;
@@ -119,6 +121,14 @@ class alignas(8) ValueBase : public SILAllocated<ValueBase> {
119121
/// If this is a SILArgument or a SILInstruction get its parent basic block,
120122
/// otherwise return null.
121123
SILBasicBlock *getParentBB();
124+
125+
/// If this is a SILArgument or a SILInstruction get its parent function,
126+
/// otherwise return null.
127+
SILFunction *getFunction();
128+
129+
/// If this is a SILArgument or a SILInstruction get its parent module,
130+
/// otherwise return null.
131+
SILModule *getModule();
122132
};
123133

124134
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,

lib/SIL/SILValue.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ SILBasicBlock *ValueBase::getParentBB() {
3838
return Arg->getParent();
3939
return nullptr;
4040
}
41+
42+
SILFunction *ValueBase::getFunction() {
43+
if (auto Inst = dyn_cast<SILInstruction>(this))
44+
return Inst->getFunction();
45+
if (auto Arg = dyn_cast<SILArgument>(this))
46+
return Arg->getFunction();
47+
return nullptr;
48+
}
49+
50+
SILModule *ValueBase::getModule() {
51+
if (auto Inst = dyn_cast<SILInstruction>(this))
52+
return &Inst->getModule();
53+
if (auto Arg = dyn_cast<SILArgument>(this))
54+
return &Arg->getModule();
55+
return nullptr;
56+
}

0 commit comments

Comments
 (0)