Skip to content

Commit 39472f2

Browse files
committed
[CodeExtractor] NFC: Refactor sanity checks into isEligible
Reviewers: fhahn Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68331 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373479 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9a5d29b commit 39472f2

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

include/llvm/Transforms/Utils/CodeExtractor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ class Value;
110110
///
111111
/// Based on the blocks used when constructing the code extractor,
112112
/// determine whether it is eligible for extraction.
113-
bool isEligible() const { return !Blocks.empty(); }
113+
///
114+
/// Checks that varargs handling (with vastart and vaend) is only done in
115+
/// the outlined blocks.
116+
bool isEligible() const;
114117

115118
/// Compute the set of input values and output values for the code.
116119
///

lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,41 @@ void CodeExtractor::findAllocas(ValueSet &SinkCands, ValueSet &HoistCands,
535535
}
536536
}
537537

538+
bool CodeExtractor::isEligible() const {
539+
if (Blocks.empty())
540+
return false;
541+
BasicBlock *Header = *Blocks.begin();
542+
Function *F = Header->getParent();
543+
544+
// For functions with varargs, check that varargs handling is only done in the
545+
// outlined function, i.e vastart and vaend are only used in outlined blocks.
546+
if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
547+
auto containsVarArgIntrinsic = [](const Instruction &I) {
548+
if (const CallInst *CI = dyn_cast<CallInst>(&I))
549+
if (const Function *Callee = CI->getCalledFunction())
550+
return Callee->getIntrinsicID() == Intrinsic::vastart ||
551+
Callee->getIntrinsicID() == Intrinsic::vaend;
552+
return false;
553+
};
554+
555+
for (auto &BB : *F) {
556+
if (Blocks.count(&BB))
557+
continue;
558+
if (llvm::any_of(BB, containsVarArgIntrinsic))
559+
return false;
560+
}
561+
}
562+
return true;
563+
}
564+
538565
void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
539566
const ValueSet &SinkCands) const {
540567
for (BasicBlock *BB : Blocks) {
541568
// If a used value is defined outside the region, it's an input. If an
542569
// instruction is used outside the region, it's an output.
543570
for (Instruction &II : *BB) {
544-
for (User::op_iterator OI = II.op_begin(), OE = II.op_end(); OI != OE;
545-
++OI) {
546-
Value *V = *OI;
571+
for (auto &OI : II.operands()) {
572+
Value *V = OI;
547573
if (!SinkCands.count(V) && definedInCaller(Blocks, V))
548574
Inputs.insert(V);
549575
}
@@ -1339,27 +1365,6 @@ Function *CodeExtractor::extractCodeRegion() {
13391365
BasicBlock *header = *Blocks.begin();
13401366
Function *oldFunction = header->getParent();
13411367

1342-
// For functions with varargs, check that varargs handling is only done in the
1343-
// outlined function, i.e vastart and vaend are only used in outlined blocks.
1344-
if (AllowVarArgs && oldFunction->getFunctionType()->isVarArg()) {
1345-
auto containsVarArgIntrinsic = [](Instruction &I) {
1346-
if (const CallInst *CI = dyn_cast<CallInst>(&I))
1347-
if (const Function *F = CI->getCalledFunction())
1348-
return F->getIntrinsicID() == Intrinsic::vastart ||
1349-
F->getIntrinsicID() == Intrinsic::vaend;
1350-
return false;
1351-
};
1352-
1353-
for (auto &BB : *oldFunction) {
1354-
if (Blocks.count(&BB))
1355-
continue;
1356-
if (llvm::any_of(BB, containsVarArgIntrinsic))
1357-
return nullptr;
1358-
}
1359-
}
1360-
ValueSet inputs, outputs, SinkingCands, HoistingCands;
1361-
BasicBlock *CommonExit = nullptr;
1362-
13631368
// Calculate the entry frequency of the new function before we change the root
13641369
// block.
13651370
BlockFrequency EntryFreq;
@@ -1426,6 +1431,8 @@ Function *CodeExtractor::extractCodeRegion() {
14261431
}
14271432
newFuncRoot->getInstList().push_back(BranchI);
14281433

1434+
ValueSet inputs, outputs, SinkingCands, HoistingCands;
1435+
BasicBlock *CommonExit = nullptr;
14291436
findAllocas(SinkingCands, HoistingCands, CommonExit);
14301437
assert(HoistingCands.empty() || CommonExit);
14311438

0 commit comments

Comments
 (0)