Skip to content

[CodeExtractor][NFC]: Refactor SwitchType #115078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion llvm/include/llvm/Transforms/Utils/CodeExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class CodeExtractorAnalysisCache {
// Bits of intermediate state computed at various phases of extraction.
SetVector<BasicBlock *> Blocks;
unsigned NumExitBlocks = std::numeric_limits<unsigned>::max();
Type *RetTy;

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

/// Return the type used for the return code of the extracted function to
/// indicate which exit block to jump to.
Type *getSwitchType();

void severSplitPHINodesOfEntry(BasicBlock *&Header);
void severSplitPHINodesOfExits(const SetVector<BasicBlock *> &Exits);
void splitReturnBlocks();
Expand Down
25 changes: 17 additions & 8 deletions llvm/lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,6 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");

// This function returns unsigned, outputs will go back by reference.
switch (NumExitBlocks) {
case 0:
case 1: RetTy = Type::getVoidTy(header->getContext()); break;
case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
default: RetTy = Type::getInt16Ty(header->getContext()); break;
}

std::vector<Type *> ParamTy;
std::vector<Type *> AggParamTy;
std::vector<std::tuple<unsigned, Value *>> NumberedInputs;
Expand Down Expand Up @@ -870,6 +862,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
StructTy, ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
}

Type *RetTy = getSwitchType();
LLVM_DEBUG({
dbgs() << "Function type: " << *RetTy << " f(";
for (Type *i : ParamTy)
Expand Down Expand Up @@ -1080,6 +1073,22 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
return newFunction;
}

Type *CodeExtractor::getSwitchType() {
LLVMContext &Context = Blocks.front()->getContext();

assert(NumExitBlocks < 0xffff && "too many exit blocks for switch");
switch (NumExitBlocks) {
case 0:
case 1:
return Type::getVoidTy(Context);
case 2:
// Conditional branch, return a bool
return Type::getInt1Ty(Context);
default:
return Type::getInt16Ty(Context);
}
}

/// Erase lifetime.start markers which reference inputs to the extraction
/// region, and insert the referenced memory into \p LifetimesStart.
///
Expand Down
Loading