2004-02-28 11:26:20 +08:00
|
|
|
//===- CodeExtractor.cpp - Pull code region into a new function -----------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-02-28 11:26:20 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-02-28 11:26:20 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the interface to tear out a code region, such as an
|
|
|
|
// individual loop or a parallel section, into a new function, replacing it with
|
|
|
|
// a call to the new function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
2013-02-09 09:04:28 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-08-02 10:15:45 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
|
|
|
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
|
|
|
|
#include "llvm/Analysis/BranchProbabilityInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-08-02 10:15:45 +08:00
|
|
|
#include "llvm/IR/MDBuilder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include "llvm/Pass.h"
|
2016-08-02 10:15:45 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 12:37:46 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include <algorithm>
|
2004-03-15 06:34:55 +08:00
|
|
|
#include <set>
|
2004-02-28 11:26:20 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "code-extractor"
|
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Provide a command-line option to aggregate function arguments into a struct
|
2008-12-13 13:21:37 +08:00
|
|
|
// for functions produced by the code extractor. This is useful when converting
|
2004-04-24 07:54:17 +08:00
|
|
|
// extracted functions to pthread-based code, as only one argument (void*) can
|
|
|
|
// be passed in to pthread_create().
|
|
|
|
static cl::opt<bool>
|
|
|
|
AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
|
|
|
|
cl::desc("Aggregate arguments to code-extracted functions"));
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
/// \brief Test whether a block is valid for extraction.
|
2016-07-27 16:02:46 +08:00
|
|
|
bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB) {
|
2012-05-04 18:18:49 +08:00
|
|
|
// Landing pads must be in the function where they were inserted for cleanup.
|
2015-08-04 16:21:40 +08:00
|
|
|
if (BB.isEHPad())
|
2012-05-04 18:18:49 +08:00
|
|
|
return false;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
// Don't hoist code containing allocas, invokes, or vastarts.
|
|
|
|
for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
|
|
|
|
if (isa<AllocaInst>(I) || isa<InvokeInst>(I))
|
2004-05-12 14:01:40 +08:00
|
|
|
return false;
|
2012-05-04 18:18:49 +08:00
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
if (const Function *F = CI->getCalledFunction())
|
|
|
|
if (F->getIntrinsicID() == Intrinsic::vastart)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Build a set of blocks to extract if the input blocks are viable.
|
2017-04-21 08:21:09 +08:00
|
|
|
static SetVector<BasicBlock *>
|
2017-04-21 12:25:00 +08:00
|
|
|
buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT) {
|
|
|
|
assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
|
2017-04-21 08:21:09 +08:00
|
|
|
SetVector<BasicBlock *> Result;
|
2012-05-04 18:26:45 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
// Loop over the blocks, adding them to our set-vector, and aborting with an
|
|
|
|
// empty set if we encounter invalid blocks.
|
2017-04-21 12:25:00 +08:00
|
|
|
for (BasicBlock *BB : BBs) {
|
|
|
|
|
|
|
|
// If this block is dead, don't process it.
|
|
|
|
if (DT && !DT->isReachableFromEntry(BB))
|
|
|
|
continue;
|
2012-05-04 18:18:49 +08:00
|
|
|
|
2017-04-21 12:25:00 +08:00
|
|
|
if (!Result.insert(BB))
|
|
|
|
llvm_unreachable("Repeated basic blocks in extraction input");
|
|
|
|
if (!CodeExtractor::isBlockValidForExtraction(*BB)) {
|
2012-05-04 18:18:49 +08:00
|
|
|
Result.clear();
|
2012-05-04 19:14:19 +08:00
|
|
|
return Result;
|
2004-05-12 14:01:40 +08:00
|
|
|
}
|
2017-04-21 12:25:00 +08:00
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2012-05-04 18:26:45 +08:00
|
|
|
#ifndef NDEBUG
|
2014-03-02 20:27:27 +08:00
|
|
|
for (SetVector<BasicBlock *>::iterator I = std::next(Result.begin()),
|
2012-05-05 05:33:30 +08:00
|
|
|
E = Result.end();
|
2012-05-04 18:26:45 +08:00
|
|
|
I != E; ++I)
|
2014-07-22 01:06:51 +08:00
|
|
|
for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
|
|
|
|
PI != PE; ++PI)
|
|
|
|
assert(Result.count(*PI) &&
|
2012-05-04 18:26:45 +08:00
|
|
|
"No blocks in this region may have entries from outside the region"
|
|
|
|
" except for the first block!");
|
|
|
|
#endif
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
|
2016-08-02 10:15:45 +08:00
|
|
|
bool AggregateArgs, BlockFrequencyInfo *BFI,
|
|
|
|
BranchProbabilityInfo *BPI)
|
|
|
|
: DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
|
2017-04-21 12:25:00 +08:00
|
|
|
BPI(BPI), Blocks(buildExtractionBlockSet(BBs, DT)), NumExitBlocks(~0U) {}
|
2016-08-02 10:15:45 +08:00
|
|
|
|
|
|
|
CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs,
|
|
|
|
BlockFrequencyInfo *BFI,
|
|
|
|
BranchProbabilityInfo *BPI)
|
|
|
|
: DT(&DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
|
2017-04-21 12:25:00 +08:00
|
|
|
BPI(BPI), Blocks(buildExtractionBlockSet(L.getBlocks(), &DT)),
|
2016-08-02 10:15:45 +08:00
|
|
|
NumExitBlocks(~0U) {}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
/// definedInRegion - Return true if the specified value is defined in the
|
|
|
|
/// extracted region.
|
|
|
|
static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (Blocks.count(I->getParent()))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// definedInCaller - Return true if the specified value is defined in the
|
|
|
|
/// function being code extracted, but not in the region being extracted.
|
|
|
|
/// These values must be passed in as live-ins to the function.
|
|
|
|
static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
|
|
|
|
if (isa<Argument>(V)) return true;
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (!Blocks.count(I->getParent()))
|
|
|
|
return true;
|
|
|
|
return false;
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2012-05-04 19:20:27 +08:00
|
|
|
void CodeExtractor::findInputsOutputs(ValueSet &Inputs,
|
|
|
|
ValueSet &Outputs) const {
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock *BB : Blocks) {
|
2012-05-04 19:20:27 +08:00
|
|
|
// If a used value is defined outside the region, it's an input. If an
|
|
|
|
// instruction is used outside the region, it's an output.
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Instruction &II : *BB) {
|
|
|
|
for (User::op_iterator OI = II.op_begin(), OE = II.op_end(); OI != OE;
|
|
|
|
++OI)
|
2012-05-04 19:20:27 +08:00
|
|
|
if (definedInCaller(Blocks, *OI))
|
|
|
|
Inputs.insert(*OI);
|
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
for (User *U : II.users())
|
2014-03-09 11:16:01 +08:00
|
|
|
if (!definedInRegion(Blocks, U)) {
|
2016-06-26 20:28:59 +08:00
|
|
|
Outputs.insert(&II);
|
2012-05-04 19:20:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
/// severSplitPHINodes - If a PHI node has multiple inputs from outside of the
|
|
|
|
/// region, we need to split the entry block of the region so that the PHI node
|
|
|
|
/// is easier to deal with.
|
|
|
|
void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) {
|
2011-03-30 19:19:20 +08:00
|
|
|
unsigned NumPredsFromRegion = 0;
|
2004-05-12 23:29:13 +08:00
|
|
|
unsigned NumPredsOutsideRegion = 0;
|
|
|
|
|
2007-03-23 00:38:57 +08:00
|
|
|
if (Header != &Header->getParent()->getEntryBlock()) {
|
2004-05-12 23:29:13 +08:00
|
|
|
PHINode *PN = dyn_cast<PHINode>(Header->begin());
|
|
|
|
if (!PN) return; // No PHI nodes.
|
|
|
|
|
|
|
|
// If the header node contains any PHI nodes, check to see if there is more
|
|
|
|
// than one entry from outside the region. If so, we need to sever the
|
|
|
|
// header block into two.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i)))
|
2011-03-30 19:19:20 +08:00
|
|
|
++NumPredsFromRegion;
|
2004-05-12 23:29:13 +08:00
|
|
|
else
|
|
|
|
++NumPredsOutsideRegion;
|
|
|
|
|
|
|
|
// If there is one (or fewer) predecessor from outside the region, we don't
|
|
|
|
// need to do anything special.
|
|
|
|
if (NumPredsOutsideRegion <= 1) return;
|
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2004-05-12 23:29:13 +08:00
|
|
|
// Otherwise, we need to split the header block into two pieces: one
|
|
|
|
// containing PHI nodes merging values from outside of the region, and a
|
|
|
|
// second that contains all of the code for the block and merges back any
|
|
|
|
// incoming values from inside of the region.
|
2017-04-21 05:40:22 +08:00
|
|
|
BasicBlock *NewBB = llvm::SplitBlock(Header, Header->getFirstNonPHI(), DT);
|
2004-05-12 23:29:13 +08:00
|
|
|
|
|
|
|
// We only want to code extract the second block now, and it becomes the new
|
|
|
|
// header of the region.
|
|
|
|
BasicBlock *OldPred = Header;
|
2012-05-04 18:18:49 +08:00
|
|
|
Blocks.remove(OldPred);
|
|
|
|
Blocks.insert(NewBB);
|
2004-05-12 23:29:13 +08:00
|
|
|
Header = NewBB;
|
|
|
|
|
|
|
|
// Okay, now we need to adjust the PHI nodes and any branches from within the
|
|
|
|
// region to go to the new header block instead of the old header block.
|
2011-03-30 19:19:20 +08:00
|
|
|
if (NumPredsFromRegion) {
|
2004-05-12 23:29:13 +08:00
|
|
|
PHINode *PN = cast<PHINode>(OldPred->begin());
|
|
|
|
// Loop over all of the predecessors of OldPred that are in the region,
|
|
|
|
// changing them to branch to NewBB instead.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-05-12 23:29:13 +08:00
|
|
|
TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator();
|
|
|
|
TI->replaceUsesOfWith(OldPred, NewBB);
|
|
|
|
}
|
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
// Okay, everything within the region is now branching to the right block, we
|
2004-05-12 23:29:13 +08:00
|
|
|
// just have to update the PHI nodes now, inserting PHI nodes into NewBB.
|
2017-04-21 05:40:22 +08:00
|
|
|
BasicBlock::iterator AfterPHIs;
|
2004-09-16 01:06:42 +08:00
|
|
|
for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
|
|
|
|
PHINode *PN = cast<PHINode>(AfterPHIs);
|
2004-05-12 23:29:13 +08:00
|
|
|
// Create a new PHI node in the new region, which has an incoming value
|
|
|
|
// from OldPred of PN.
|
2011-03-30 19:28:46 +08:00
|
|
|
PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
|
2015-10-13 10:39:05 +08:00
|
|
|
PN->getName() + ".ce", &NewBB->front());
|
2017-04-25 12:51:19 +08:00
|
|
|
PN->replaceAllUsesWith(NewPN);
|
2004-05-12 23:29:13 +08:00
|
|
|
NewPN->addIncoming(PN, OldPred);
|
|
|
|
|
|
|
|
// Loop over all of the incoming value in PN, moving them to NewPN if they
|
|
|
|
// are from the extracted region.
|
|
|
|
for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-05-12 23:29:13 +08:00
|
|
|
NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
|
|
|
|
PN->removeIncomingValue(i);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 00:07:41 +08:00
|
|
|
}
|
2004-05-12 23:29:13 +08:00
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
void CodeExtractor::splitReturnBlocks() {
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock *Block : Blocks)
|
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) {
|
2015-10-13 10:39:05 +08:00
|
|
|
BasicBlock *New =
|
2016-06-26 20:28:59 +08:00
|
|
|
Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
|
2009-08-25 07:32:14 +08:00
|
|
|
if (DT) {
|
2010-09-11 06:25:58 +08:00
|
|
|
// Old dominates New. New node dominates all other nodes dominated
|
|
|
|
// by Old.
|
2016-06-26 20:28:59 +08:00
|
|
|
DomTreeNode *OldNode = DT->getNode(Block);
|
|
|
|
SmallVector<DomTreeNode *, 8> Children(OldNode->begin(),
|
|
|
|
OldNode->end());
|
2009-08-25 07:32:14 +08:00
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
DomTreeNode *NewNode = DT->addNewBlock(New, Block);
|
2009-08-25 07:32:14 +08:00
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
for (DomTreeNode *I : Children)
|
|
|
|
DT->changeImmediateDominator(I, NewNode);
|
2009-08-25 07:32:14 +08:00
|
|
|
}
|
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
}
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
/// constructFunction - make a function based on inputs and outputs, as follows:
|
|
|
|
/// f(in0, ..., inN, out0, ..., outN)
|
|
|
|
///
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *CodeExtractor::constructFunction(const ValueSet &inputs,
|
|
|
|
const ValueSet &outputs,
|
2004-03-18 13:28:49 +08:00
|
|
|
BasicBlock *header,
|
2004-02-28 11:26:20 +08:00
|
|
|
BasicBlock *newRootNode,
|
|
|
|
BasicBlock *newHeader,
|
2004-03-18 13:28:49 +08:00
|
|
|
Function *oldFunction,
|
|
|
|
Module *M) {
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
|
|
|
|
DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// This function returns unsigned, outputs will go back by reference.
|
2004-05-12 12:14:24 +08:00
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
2009-08-14 05:58:54 +08:00
|
|
|
case 1: RetTy = Type::getVoidTy(header->getContext()); break;
|
|
|
|
case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
|
|
|
|
default: RetTy = Type::getInt16Ty(header->getContext()); break;
|
2004-05-12 12:14:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> paramTy;
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Add the types of the input values to the function's argument list
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Value *value : inputs) {
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "value used in func: " << *value << "\n");
|
2004-02-28 11:26:20 +08:00
|
|
|
paramTy.push_back(value->getType());
|
|
|
|
}
|
|
|
|
|
2004-03-18 11:49:40 +08:00
|
|
|
// Add the types of the output values to the function's argument list.
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Value *output : outputs) {
|
|
|
|
DEBUG(dbgs() << "instr used in func: " << *output << "\n");
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs)
|
2016-06-26 20:28:59 +08:00
|
|
|
paramTy.push_back(output->getType());
|
2004-04-24 07:54:17 +08:00
|
|
|
else
|
2016-06-26 20:28:59 +08:00
|
|
|
paramTy.push_back(PointerType::getUnqual(output->getType()));
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-26 21:39:33 +08:00
|
|
|
DEBUG({
|
|
|
|
dbgs() << "Function type: " << *RetTy << " f(";
|
|
|
|
for (Type *i : paramTy)
|
|
|
|
dbgs() << *i << ", ";
|
|
|
|
dbgs() << ")\n";
|
|
|
|
});
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2015-03-14 09:53:18 +08:00
|
|
|
StructType *StructTy;
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
|
2015-03-14 09:53:18 +08:00
|
|
|
StructTy = StructType::get(M->getContext(), paramTy);
|
2004-04-24 07:54:17 +08:00
|
|
|
paramTy.clear();
|
2015-03-14 09:53:18 +08:00
|
|
|
paramTy.push_back(PointerType::getUnqual(StructTy));
|
2004-04-24 07:54:17 +08:00
|
|
|
}
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *funcType =
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType::get(RetTy, paramTy, false);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Create the new function
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *newFunction = Function::Create(funcType,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
oldFunction->getName() + "_" +
|
|
|
|
header->getName(), M);
|
2008-12-18 13:52:56 +08:00
|
|
|
// If the old function is no-throw, so is the new one.
|
|
|
|
if (oldFunction->doesNotThrow())
|
2012-10-10 11:12:49 +08:00
|
|
|
newFunction->setDoesNotThrow();
|
2016-08-01 11:15:32 +08:00
|
|
|
|
|
|
|
// Inherit the uwtable attribute if we need to.
|
|
|
|
if (oldFunction->hasUWTable())
|
|
|
|
newFunction->setHasUWTable();
|
|
|
|
|
|
|
|
// Inherit all of the target dependent attributes.
|
|
|
|
// (e.g. If the extracted region contains a call to an x86.sse
|
|
|
|
// instruction we need to make sure that the extracted region has the
|
|
|
|
// "target-features" attribute allowing it to be lowered.
|
|
|
|
// FIXME: This should be changed to check to see if a specific
|
|
|
|
// attribute can not be inherited.
|
2017-04-11 07:31:05 +08:00
|
|
|
AttrBuilder AB(oldFunction->getAttributes().getFnAttributes());
|
2017-02-22 14:34:04 +08:00
|
|
|
for (const auto &Attr : AB.td_attrs())
|
2016-08-01 11:15:32 +08:00
|
|
|
newFunction->addFnAttr(Attr.first, Attr.second);
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
newFunction->getBasicBlockList().push_back(newRootNode);
|
|
|
|
|
2004-03-18 11:49:40 +08:00
|
|
|
// Create an iterator to name all of the arguments we inserted.
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator AI = newFunction->arg_begin();
|
2004-03-18 11:49:40 +08:00
|
|
|
|
|
|
|
// Rewrite all users of the inputs in the extracted region to use the
|
2004-04-24 07:54:17 +08:00
|
|
|
// arguments (or appropriate addressing into struct) instead.
|
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
|
|
|
|
Value *RewriteVal;
|
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
|
2004-04-24 07:54:17 +08:00
|
|
|
TerminatorInst *TI = newFunction->begin()->getTerminator();
|
2015-03-14 09:53:18 +08:00
|
|
|
GetElementPtrInst *GEP = GetElementPtrInst::Create(
|
2015-10-13 10:39:05 +08:00
|
|
|
StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI);
|
2009-07-24 16:24:36 +08:00
|
|
|
RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
|
2004-04-24 07:54:17 +08:00
|
|
|
} else
|
2015-10-13 10:39:05 +08:00
|
|
|
RewriteVal = &*AI++;
|
2004-04-24 07:54:17 +08:00
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
std::vector<User*> Users(inputs[i]->user_begin(), inputs[i]->user_end());
|
2016-06-26 20:28:59 +08:00
|
|
|
for (User *use : Users)
|
|
|
|
if (Instruction *inst = dyn_cast<Instruction>(use))
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(inst->getParent()))
|
2004-04-24 07:54:17 +08:00
|
|
|
inst->replaceUsesOfWith(inputs[i], RewriteVal);
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Set names for input and output arguments.
|
|
|
|
if (!AggregateArgs) {
|
2005-03-15 12:54:21 +08:00
|
|
|
AI = newFunction->arg_begin();
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
|
2008-04-15 01:38:21 +08:00
|
|
|
AI->setName(inputs[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
|
2005-04-22 07:48:37 +08:00
|
|
|
AI->setName(outputs[i]->getName()+".out");
|
2004-04-24 07:54:17 +08:00
|
|
|
}
|
2004-03-18 11:49:40 +08:00
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
// Rewrite branches to basic blocks outside of the loop to new dummy blocks
|
|
|
|
// within the new function. This must be done before we lose track of which
|
|
|
|
// blocks were originally in the code region.
|
2014-03-09 11:16:01 +08:00
|
|
|
std::vector<User*> Users(header->user_begin(), header->user_end());
|
2004-03-18 13:28:49 +08:00
|
|
|
for (unsigned i = 0, e = Users.size(); i != e; ++i)
|
|
|
|
// The BasicBlock which contains the branch is not in the region
|
|
|
|
// modify the branch target to a new block
|
|
|
|
if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i]))
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(TI->getParent()) &&
|
2004-03-18 13:28:49 +08:00
|
|
|
TI->getParent()->getParent() == oldFunction)
|
|
|
|
TI->replaceUsesOfWith(header, newHeader);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
return newFunction;
|
|
|
|
}
|
|
|
|
|
2009-08-26 01:42:07 +08:00
|
|
|
/// FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI
|
|
|
|
/// that uses the value within the basic block, and return the predecessor
|
|
|
|
/// block associated with that use, or return 0 if none is found.
|
2009-08-26 01:26:32 +08:00
|
|
|
static BasicBlock* FindPhiPredForUseInBlock(Value* Used, BasicBlock* BB) {
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Use &U : Used->uses()) {
|
|
|
|
PHINode *P = dyn_cast<PHINode>(U.getUser());
|
2009-08-26 01:26:32 +08:00
|
|
|
if (P && P->getParent() == BB)
|
2014-03-09 11:16:01 +08:00
|
|
|
return P->getIncomingBlock(U);
|
2009-08-26 01:26:32 +08:00
|
|
|
}
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2009-08-26 01:26:32 +08:00
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
/// emitCallAndSwitchStatement - This method sets up the caller side by adding
|
|
|
|
/// the call instruction, splitting any PHI nodes in the header block as
|
|
|
|
/// necessary.
|
|
|
|
void CodeExtractor::
|
|
|
|
emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
|
2012-05-04 18:18:49 +08:00
|
|
|
ValueSet &inputs, ValueSet &outputs) {
|
2004-05-12 14:01:40 +08:00
|
|
|
// Emit a call to the new function, passing in: *pointer to struct (if
|
|
|
|
// aggregating parameters), or plan inputs and allocated memory for outputs
|
2009-08-25 08:54:39 +08:00
|
|
|
std::vector<Value*> params, StructValues, ReloadOutputs, Reloads;
|
2017-04-11 06:27:50 +08:00
|
|
|
|
|
|
|
Module *M = newFunction->getParent();
|
|
|
|
LLVMContext &Context = M->getContext();
|
|
|
|
const DataLayout &DL = M->getDataLayout();
|
2004-04-24 07:54:17 +08:00
|
|
|
|
|
|
|
// Add inputs as params, or to be filled into the struct
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Value *input : inputs)
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs)
|
2016-06-26 20:28:59 +08:00
|
|
|
StructValues.push_back(input);
|
2004-04-24 07:54:17 +08:00
|
|
|
else
|
2016-06-26 20:28:59 +08:00
|
|
|
params.push_back(input);
|
2004-04-24 07:54:17 +08:00
|
|
|
|
|
|
|
// Create allocas for the outputs
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Value *output : outputs) {
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs) {
|
2016-06-26 20:28:59 +08:00
|
|
|
StructValues.push_back(output);
|
2004-04-24 07:54:17 +08:00
|
|
|
} else {
|
|
|
|
AllocaInst *alloca =
|
2017-04-11 06:27:50 +08:00
|
|
|
new AllocaInst(output->getType(), DL.getAllocaAddrSpace(),
|
|
|
|
nullptr, output->getName() + ".loc",
|
|
|
|
&codeReplacer->getParent()->front().front());
|
2004-04-24 07:54:17 +08:00
|
|
|
ReloadOutputs.push_back(alloca);
|
|
|
|
params.push_back(alloca);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 09:53:18 +08:00
|
|
|
StructType *StructArgTy = nullptr;
|
2014-04-25 13:29:35 +08:00
|
|
|
AllocaInst *Struct = nullptr;
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> ArgTypes;
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::iterator v = StructValues.begin(),
|
2004-04-24 07:54:17 +08:00
|
|
|
ve = StructValues.end(); v != ve; ++v)
|
|
|
|
ArgTypes.push_back((*v)->getType());
|
|
|
|
|
|
|
|
// Allocate a struct at the beginning of this function
|
2015-03-14 09:53:18 +08:00
|
|
|
StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
|
2017-04-11 06:27:50 +08:00
|
|
|
Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
|
|
|
|
"structArg",
|
2015-10-13 10:39:05 +08:00
|
|
|
&codeReplacer->getParent()->front().front());
|
2004-04-24 07:54:17 +08:00
|
|
|
params.push_back(Struct);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
|
2015-03-14 09:53:18 +08:00
|
|
|
GetElementPtrInst *GEP = GetElementPtrInst::Create(
|
|
|
|
StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(GEP);
|
|
|
|
StoreInst *SI = new StoreInst(StructValues[i], GEP);
|
|
|
|
codeReplacer->getInstList().push_back(SI);
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
|
|
|
|
// Emit the call to the function
|
2011-07-15 16:37:34 +08:00
|
|
|
CallInst *call = CallInst::Create(newFunction, params,
|
2008-04-07 04:25:17 +08:00
|
|
|
NumExitBlocks > 1 ? "targetBlock" : "");
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(call);
|
|
|
|
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator OutputArgBegin = newFunction->arg_begin();
|
2004-04-24 07:54:17 +08:00
|
|
|
unsigned FirstOut = inputs.size();
|
|
|
|
if (!AggregateArgs)
|
|
|
|
std::advance(OutputArgBegin, inputs.size());
|
2004-03-18 12:12:05 +08:00
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Reload the outputs passed in by reference
|
2004-03-18 11:49:40 +08:00
|
|
|
for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *Output = nullptr;
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
|
2015-03-14 09:53:18 +08:00
|
|
|
GetElementPtrInst *GEP = GetElementPtrInst::Create(
|
|
|
|
StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(GEP);
|
|
|
|
Output = GEP;
|
|
|
|
} else {
|
|
|
|
Output = ReloadOutputs[i];
|
|
|
|
}
|
|
|
|
LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload");
|
2009-08-25 08:54:39 +08:00
|
|
|
Reloads.push_back(load);
|
2004-03-18 11:49:40 +08:00
|
|
|
codeReplacer->getInstList().push_back(load);
|
2014-03-09 11:16:01 +08:00
|
|
|
std::vector<User*> Users(outputs[i]->user_begin(), outputs[i]->user_end());
|
2004-03-18 11:49:40 +08:00
|
|
|
for (unsigned u = 0, e = Users.size(); u != e; ++u) {
|
|
|
|
Instruction *inst = cast<Instruction>(Users[u]);
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(inst->getParent()))
|
2004-03-18 11:49:40 +08:00
|
|
|
inst->replaceUsesOfWith(outputs[i], load);
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2004-03-15 07:05:49 +08:00
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
// Now we can emit a switch statement using the call as a value.
|
2004-05-12 12:14:24 +08:00
|
|
|
SwitchInst *TheSwitch =
|
2009-08-14 05:58:54 +08:00
|
|
|
SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
|
2008-04-07 04:25:17 +08:00
|
|
|
codeReplacer, 0, codeReplacer);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Since there may be multiple exits from the original region, make the new
|
2004-03-15 07:05:49 +08:00
|
|
|
// function return an unsigned, switch on that number. This loop iterates
|
|
|
|
// over all of the blocks in the extracted region, updating any terminator
|
|
|
|
// instructions in the to-be-extracted region that branch to blocks that are
|
|
|
|
// not in the region to be extracted.
|
|
|
|
std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
unsigned switchVal = 0;
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock *Block : Blocks) {
|
|
|
|
TerminatorInst *TI = Block->getTerminator();
|
2004-03-15 07:05:49 +08:00
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(TI->getSuccessor(i))) {
|
2004-03-15 07:05:49 +08:00
|
|
|
BasicBlock *OldTarget = TI->getSuccessor(i);
|
|
|
|
// add a new basic block which returns the appropriate value
|
|
|
|
BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
|
|
|
|
if (!NewTarget) {
|
|
|
|
// If we don't already have an exit stub for this non-extracted
|
|
|
|
// destination, create one now!
|
2009-08-14 05:58:54 +08:00
|
|
|
NewTarget = BasicBlock::Create(Context,
|
|
|
|
OldTarget->getName() + ".exitStub",
|
2008-04-07 04:25:17 +08:00
|
|
|
newFunction);
|
2004-05-12 12:14:24 +08:00
|
|
|
unsigned SuccNum = switchVal++;
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *brVal = nullptr;
|
2004-05-12 12:14:24 +08:00
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
|
|
|
case 1: break; // No value needed.
|
|
|
|
case 2: // Conditional branch, return a bool
|
2009-08-14 05:58:54 +08:00
|
|
|
brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
2009-08-14 05:58:54 +08:00
|
|
|
brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-03-15 07:05:49 +08:00
|
|
|
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst *NTRet = ReturnInst::Create(Context, brVal, NewTarget);
|
2004-03-15 07:05:49 +08:00
|
|
|
|
|
|
|
// Update the switch instruction.
|
2009-08-14 05:58:54 +08:00
|
|
|
TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
|
|
|
|
SuccNum),
|
2004-05-12 12:14:24 +08:00
|
|
|
OldTarget);
|
2004-03-15 07:05:49 +08:00
|
|
|
|
|
|
|
// Restore values just before we exit
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator OAI = OutputArgBegin;
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned out = 0, e = outputs.size(); out != e; ++out) {
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
// For an invoke, the normal destination is the only one that is
|
|
|
|
// dominated by the result of the invocation
|
2004-04-24 07:54:17 +08:00
|
|
|
BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent();
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
bool DominatesDef = true;
|
|
|
|
|
2015-08-15 10:46:08 +08:00
|
|
|
BasicBlock *NormalDest = nullptr;
|
|
|
|
if (auto *Invoke = dyn_cast<InvokeInst>(outputs[out]))
|
|
|
|
NormalDest = Invoke->getNormalDest();
|
|
|
|
|
|
|
|
if (NormalDest) {
|
|
|
|
DefBlock = NormalDest;
|
2004-11-13 07:50:44 +08:00
|
|
|
|
|
|
|
// Make sure we are looking at the original successor block, not
|
|
|
|
// at a newly inserted exit block, which won't be in the dominator
|
|
|
|
// info.
|
2016-06-26 20:28:59 +08:00
|
|
|
for (const auto &I : ExitBlockMap)
|
|
|
|
if (DefBlock == I.second) {
|
|
|
|
DefBlock = I.first;
|
2004-11-13 07:50:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
// In the extract block case, if the block we are extracting ends
|
|
|
|
// with an invoke instruction, make sure that we don't emit a
|
|
|
|
// store of the invoke value for the unwind block.
|
2007-06-08 06:17:16 +08:00
|
|
|
if (!DT && DefBlock != OldTarget)
|
2004-11-13 08:06:45 +08:00
|
|
|
DominatesDef = false;
|
2004-11-13 07:50:44 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 08:54:39 +08:00
|
|
|
if (DT) {
|
2007-06-08 06:17:16 +08:00
|
|
|
DominatesDef = DT->dominates(DefBlock, OldTarget);
|
2009-08-25 08:54:39 +08:00
|
|
|
|
|
|
|
// If the output value is used by a phi in the target block,
|
|
|
|
// then we need to test for dominance of the phi's predecessor
|
|
|
|
// instead. Unfortunately, this a little complicated since we
|
|
|
|
// have already rewritten uses of the value to uses of the reload.
|
2009-08-26 01:26:32 +08:00
|
|
|
BasicBlock* pred = FindPhiPredForUseInBlock(Reloads[out],
|
|
|
|
OldTarget);
|
|
|
|
if (pred && DT && DT->dominates(DefBlock, pred))
|
|
|
|
DominatesDef = true;
|
2009-08-25 08:54:39 +08:00
|
|
|
}
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
if (DominatesDef) {
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context),
|
|
|
|
FirstOut+out);
|
2015-03-14 09:53:18 +08:00
|
|
|
GetElementPtrInst *GEP = GetElementPtrInst::Create(
|
2015-10-13 10:39:05 +08:00
|
|
|
StructArgTy, &*OAI, Idx, "gep_" + outputs[out]->getName(),
|
2015-03-14 09:53:18 +08:00
|
|
|
NTRet);
|
2004-04-24 07:54:17 +08:00
|
|
|
new StoreInst(outputs[out], GEP, NTRet);
|
2004-11-13 08:06:45 +08:00
|
|
|
} else {
|
2015-10-13 10:39:05 +08:00
|
|
|
new StoreInst(outputs[out], &*OAI, NTRet);
|
2004-11-13 08:06:45 +08:00
|
|
|
}
|
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
// Advance output iterator even if we don't emit a store
|
|
|
|
if (!AggregateArgs) ++OAI;
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
2004-03-15 06:34:55 +08:00
|
|
|
|
2004-03-15 07:05:49 +08:00
|
|
|
// rewrite the original branch instruction with this new target
|
|
|
|
TI->setSuccessor(i, NewTarget);
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
2004-03-15 07:43:24 +08:00
|
|
|
|
2004-05-12 11:22:33 +08:00
|
|
|
// Now that we've done the deed, simplify the switch instruction.
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
|
2004-05-12 12:14:24 +08:00
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
2004-08-12 11:17:02 +08:00
|
|
|
// There are no successors (the block containing the switch itself), which
|
2004-04-24 07:54:17 +08:00
|
|
|
// means that previously this was the last part of the function, and hence
|
|
|
|
// this should be rewritten as a `ret'
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Check if the function should return a value
|
2010-01-05 21:12:22 +08:00
|
|
|
if (OldFnRetTy->isVoidTy()) {
|
2014-04-25 13:29:35 +08:00
|
|
|
ReturnInst::Create(Context, nullptr, TheSwitch); // Return void
|
2004-08-12 11:17:02 +08:00
|
|
|
} else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
|
2004-04-24 07:54:17 +08:00
|
|
|
// return what we have
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch);
|
2004-08-12 11:17:02 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise we must have code extracted an unwind or something, just
|
|
|
|
// return whatever we want.
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(Context,
|
|
|
|
Constant::getNullValue(OldFnRetTy), TheSwitch);
|
2004-08-12 11:17:02 +08:00
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Only a single destination, change the switch into an unconditional
|
|
|
|
// branch.
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch);
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
|
|
|
|
call, TheSwitch);
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Otherwise, make the default destination of the switch instruction be one
|
|
|
|
// of the other successors.
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
llvm-svn: 149481
2012-02-01 15:49:51 +08:00
|
|
|
TheSwitch->setCondition(call);
|
|
|
|
TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
|
2012-03-08 15:06:20 +08:00
|
|
|
// Remove redundant case
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
llvm-svn: 190328
2013-09-10 03:14:35 +08:00
|
|
|
TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1));
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
2004-03-15 07:43:24 +08:00
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
void CodeExtractor::moveCodeToFunction(Function *newFunction) {
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *oldFunc = (*Blocks.begin())->getParent();
|
2004-05-12 14:01:40 +08:00
|
|
|
Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
|
|
|
|
Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
|
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock *Block : Blocks) {
|
2004-05-12 14:01:40 +08:00
|
|
|
// Delete the basic block from the old function, and the list of blocks
|
2016-06-26 20:28:59 +08:00
|
|
|
oldBlocks.remove(Block);
|
2004-05-12 14:01:40 +08:00
|
|
|
|
|
|
|
// Insert this basic block into the new function
|
2016-06-26 20:28:59 +08:00
|
|
|
newBlocks.push_back(Block);
|
2004-05-12 14:01:40 +08:00
|
|
|
}
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
void CodeExtractor::calculateNewCallTerminatorWeights(
|
|
|
|
BasicBlock *CodeReplacer,
|
|
|
|
DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
|
|
|
|
BranchProbabilityInfo *BPI) {
|
|
|
|
typedef BlockFrequencyInfoImplBase::Distribution Distribution;
|
|
|
|
typedef BlockFrequencyInfoImplBase::BlockNode BlockNode;
|
|
|
|
|
|
|
|
// Update the branch weights for the exit block.
|
|
|
|
TerminatorInst *TI = CodeReplacer->getTerminator();
|
|
|
|
SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
|
|
|
|
|
|
|
|
// Block Frequency distribution with dummy node.
|
|
|
|
Distribution BranchDist;
|
|
|
|
|
|
|
|
// Add each of the frequencies of the successors.
|
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
|
|
|
|
BlockNode ExitNode(i);
|
|
|
|
uint64_t ExitFreq = ExitWeights[TI->getSuccessor(i)].getFrequency();
|
|
|
|
if (ExitFreq != 0)
|
|
|
|
BranchDist.addExit(ExitNode, ExitFreq);
|
|
|
|
else
|
|
|
|
BPI->setEdgeProbability(CodeReplacer, i, BranchProbability::getZero());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for no total weight.
|
|
|
|
if (BranchDist.Total == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Normalize the distribution so that they can fit in unsigned.
|
|
|
|
BranchDist.normalize();
|
|
|
|
|
|
|
|
// Create normalized branch weights and set the metadata.
|
|
|
|
for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
|
|
|
|
const auto &Weight = BranchDist.Weights[I];
|
|
|
|
|
|
|
|
// Get the weight and update the current BFI.
|
|
|
|
BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
|
|
|
|
BranchProbability BP(Weight.Amount, BranchDist.Total);
|
|
|
|
BPI->setEdgeProbability(CodeReplacer, Weight.TargetNode.Index, BP);
|
|
|
|
}
|
|
|
|
TI->setMetadata(
|
|
|
|
LLVMContext::MD_prof,
|
|
|
|
MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
|
|
|
|
}
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *CodeExtractor::extractCodeRegion() {
|
|
|
|
if (!isEligible())
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2004-04-24 07:54:17 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
ValueSet inputs, outputs;
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Assumption: this is a single-entry code region, and the header is the first
|
2004-03-15 09:18:23 +08:00
|
|
|
// block in the region.
|
2012-05-04 18:18:49 +08:00
|
|
|
BasicBlock *header = *Blocks.begin();
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
// Calculate the entry frequency of the new function before we change the root
|
|
|
|
// block.
|
|
|
|
BlockFrequency EntryFreq;
|
|
|
|
if (BFI) {
|
|
|
|
assert(BPI && "Both BPI and BFI are required to preserve profile info");
|
|
|
|
for (BasicBlock *Pred : predecessors(header)) {
|
|
|
|
if (Blocks.count(Pred))
|
|
|
|
continue;
|
|
|
|
EntryFreq +=
|
|
|
|
BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
// If we have to split PHI nodes or the entry block, do so now.
|
2004-05-12 23:29:13 +08:00
|
|
|
severSplitPHINodes(header);
|
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
// If we have any return instructions in the region, split those blocks so
|
|
|
|
// that the return is not in the region.
|
|
|
|
splitReturnBlocks();
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
Function *oldFunction = header->getParent();
|
|
|
|
|
|
|
|
// This takes place of the original loop
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
|
|
|
|
"codeRepl", oldFunction,
|
2008-05-15 18:04:30 +08:00
|
|
|
header);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// The new function needs a root node because other nodes can branch to the
|
2004-05-12 14:01:40 +08:00
|
|
|
// head of the region, but the entry node of a function cannot have preds.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
|
|
|
|
"newFuncRoot");
|
2008-04-07 04:25:17 +08:00
|
|
|
newFuncRoot->getInstList().push_back(BranchInst::Create(header));
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
// Find inputs to, outputs from the code region.
|
|
|
|
findInputsOutputs(inputs, outputs);
|
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
// Calculate the exit blocks for the extracted region and the total exit
|
|
|
|
// weights for each of those blocks.
|
|
|
|
DenseMap<BasicBlock *, BlockFrequency> ExitWeights;
|
2012-05-04 19:20:27 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 1> ExitBlocks;
|
2016-08-02 10:15:45 +08:00
|
|
|
for (BasicBlock *Block : Blocks) {
|
2016-06-26 20:28:59 +08:00
|
|
|
for (succ_iterator SI = succ_begin(Block), SE = succ_end(Block); SI != SE;
|
2016-08-02 10:15:45 +08:00
|
|
|
++SI) {
|
|
|
|
if (!Blocks.count(*SI)) {
|
|
|
|
// Update the branch weight for this successor.
|
|
|
|
if (BFI) {
|
|
|
|
BlockFrequency &BF = ExitWeights[*SI];
|
|
|
|
BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, *SI);
|
|
|
|
}
|
2014-07-22 01:06:51 +08:00
|
|
|
ExitBlocks.insert(*SI);
|
2016-08-02 10:15:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-04 19:20:27 +08:00
|
|
|
NumExitBlocks = ExitBlocks.size();
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
// Construct new function based on inputs/outputs & add allocas for all defs.
|
2004-05-12 23:29:13 +08:00
|
|
|
Function *newFunction = constructFunction(inputs, outputs, header,
|
2005-04-22 07:48:37 +08:00
|
|
|
newFuncRoot,
|
2004-03-15 09:18:23 +08:00
|
|
|
codeReplacer, oldFunction,
|
|
|
|
oldFunction->getParent());
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
// Update the entry count of the function.
|
|
|
|
if (BFI) {
|
|
|
|
Optional<uint64_t> EntryCount =
|
|
|
|
BFI->getProfileCountFromFreq(EntryFreq.getFrequency());
|
|
|
|
if (EntryCount.hasValue())
|
|
|
|
newFunction->setEntryCount(EntryCount.getValue());
|
|
|
|
BFI->setBlockFreq(codeReplacer, EntryFreq.getFrequency());
|
|
|
|
}
|
|
|
|
|
2004-03-15 06:34:55 +08:00
|
|
|
emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-03-15 06:34:55 +08:00
|
|
|
moveCodeToFunction(newFunction);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2016-08-02 10:15:45 +08:00
|
|
|
// Update the branch weights for the exit block.
|
|
|
|
if (BFI && NumExitBlocks > 1)
|
|
|
|
calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
|
|
|
|
|
2004-05-12 23:29:13 +08:00
|
|
|
// Loop over all of the PHI nodes in the header block, and change any
|
2004-03-18 13:28:49 +08:00
|
|
|
// references to the old incoming edge to be the new incoming edge.
|
2004-09-16 01:06:42 +08:00
|
|
|
for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2004-03-18 13:28:49 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(PN->getIncomingBlock(i)))
|
2004-03-18 13:28:49 +08:00
|
|
|
PN->setIncomingBlock(i, newFuncRoot);
|
2004-09-16 01:06:42 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-03-18 13:38:31 +08:00
|
|
|
// Look at all successors of the codeReplacer block. If any of these blocks
|
|
|
|
// had PHI nodes in them, we need to update the "from" block to be the code
|
|
|
|
// replacer, not the original block in the extracted region.
|
|
|
|
std::vector<BasicBlock*> Succs(succ_begin(codeReplacer),
|
|
|
|
succ_end(codeReplacer));
|
|
|
|
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
|
2004-09-16 01:06:42 +08:00
|
|
|
for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2004-08-13 11:27:07 +08:00
|
|
|
std::set<BasicBlock*> ProcessedPreds;
|
2004-03-18 13:38:31 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-08-13 11:27:07 +08:00
|
|
|
if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second)
|
|
|
|
PN->setIncomingBlock(i, codeReplacer);
|
|
|
|
else {
|
|
|
|
// There were multiple entries in the PHI for this block, now there
|
|
|
|
// is only one, so remove the duplicated entries.
|
|
|
|
PN->removeIncomingValue(i, false);
|
|
|
|
--i; --e;
|
|
|
|
}
|
2008-02-20 19:26:25 +08:00
|
|
|
}
|
2004-08-13 11:27:07 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2006-12-07 09:30:32 +08:00
|
|
|
//cerr << "NEW FUNCTION: " << *newFunction;
|
2004-05-12 23:29:13 +08:00
|
|
|
// verifyFunction(*newFunction);
|
|
|
|
|
2006-12-07 09:30:32 +08:00
|
|
|
// cerr << "OLD FUNCTION: " << *oldFunction;
|
2004-05-12 23:29:13 +08:00
|
|
|
// verifyFunction(*oldFunction);
|
2004-03-18 13:38:31 +08:00
|
|
|
|
2009-07-11 21:10:19 +08:00
|
|
|
DEBUG(if (verifyFunction(*newFunction))
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("verifyFunction failed!"));
|
2004-02-28 11:26:20 +08:00
|
|
|
return newFunction;
|
|
|
|
}
|