Skip to content

Commit 8170ccf

Browse files
[CodeExtractor][NFC]: Refactor SwitchType
This is one of the patches split-off from llvm#114419 for ease of review. Co-authored-by: Michael Kruse <[email protected]>
1 parent c1cec8c commit 8170ccf

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

llvm/include/llvm/Transforms/Utils/CodeExtractor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class CodeExtractorAnalysisCache {
102102
// Bits of intermediate state computed at various phases of extraction.
103103
SetVector<BasicBlock *> Blocks;
104104
unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
105-
Type *RetTy;
106105

107106
// Mapping from the original exit blocks, to the new blocks inside
108107
// the function.
@@ -238,6 +237,10 @@ class CodeExtractorAnalysisCache {
238237
getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
239238
Instruction *Addr, BasicBlock *ExitBlock) const;
240239

240+
/// Return the type used for the return code of the extracted function to
241+
/// indicate which exit block to jump to.
242+
Type *getSwitchType();
243+
241244
void severSplitPHINodesOfEntry(BasicBlock *&Header);
242245
void severSplitPHINodesOfExits(const SetVector<BasicBlock *> &Exits);
243246
void splitReturnBlocks();

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,6 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
813813
LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
814814
LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
815815

816-
// This function returns unsigned, outputs will go back by reference.
817-
switch (NumExitBlocks) {
818-
case 0:
819-
case 1: RetTy = Type::getVoidTy(header->getContext()); break;
820-
case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
821-
default: RetTy = Type::getInt16Ty(header->getContext()); break;
822-
}
823-
824816
std::vector<Type *> ParamTy;
825817
std::vector<Type *> AggParamTy;
826818
std::vector<std::tuple<unsigned, Value *>> NumberedInputs;
@@ -870,6 +862,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
870862
StructTy, ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
871863
}
872864

865+
Type *RetTy = getSwitchType();
873866
LLVM_DEBUG({
874867
dbgs() << "Function type: " << *RetTy << " f(";
875868
for (Type *i : ParamTy)
@@ -1080,6 +1073,22 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
10801073
return newFunction;
10811074
}
10821075

1076+
Type *CodeExtractor::getSwitchType() {
1077+
LLVMContext &Context = Blocks.front()->getContext();
1078+
1079+
assert(NumExitBlocks < 0xffff && "too many exit blocks for switch");
1080+
switch (NumExitBlocks) {
1081+
case 0:
1082+
case 1:
1083+
return Type::getVoidTy(Context);
1084+
case 2:
1085+
// Conditional branch, return a bool
1086+
return Type::getInt1Ty(Context);
1087+
default:
1088+
return Type::getInt16Ty(Context);
1089+
}
1090+
}
1091+
10831092
/// Erase lifetime.start markers which reference inputs to the extraction
10841093
/// region, and insert the referenced memory into \p LifetimesStart.
10851094
///

0 commit comments

Comments
 (0)