2010-05-07 15:40:13 +00:00
|
|
|
//===-- Sink.cpp - Code Sinking -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass moves instructions into successor blocks, when possible, so that
|
|
|
|
// they aren't executed on paths where their results aren't needed.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-05-07 15:40:13 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2011-12-14 23:49:11 +00:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-07-10 16:07:11 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2015-03-04 18:43:29 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2010-05-07 15:40:13 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "sink"
|
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
STATISTIC(NumSunk, "Number of instructions sunk");
|
2012-05-31 08:09:49 +00:00
|
|
|
STATISTIC(NumSinkIter, "Number of sinking iterations");
|
2010-05-07 15:40:13 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class Sinking : public FunctionPass {
|
|
|
|
DominatorTree *DT;
|
|
|
|
LoopInfo *LI;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification
|
2010-10-19 17:21:58 +00:00
|
|
|
Sinking() : FunctionPass(ID) {
|
|
|
|
initializeSinkingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-05-07 15:40:13 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2015-01-17 14:16:18 +00:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2015-01-17 14:16:18 +00:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2010-05-07 15:40:13 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool ProcessBlock(BasicBlock &BB);
|
2014-08-21 05:55:13 +00:00
|
|
|
bool SinkInstruction(Instruction *I, SmallPtrSetImpl<Instruction*> &Stores);
|
2010-05-07 15:40:13 +00:00
|
|
|
bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB) const;
|
2012-05-31 08:09:49 +00:00
|
|
|
bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo) const;
|
2010-05-07 15:40:13 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
char Sinking::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(Sinking, "sink", "Code sinking", false, false)
|
2015-01-17 14:16:18 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2014-01-13 13:07:17 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
|
|
|
INITIALIZE_PASS_END(Sinking, "sink", "Code sinking", false, false)
|
2010-05-07 15:40:13 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createSinkingPass() { return new Sinking(); }
|
|
|
|
|
|
|
|
/// AllUsesDominatedByBlock - Return true if all uses of the specified value
|
|
|
|
/// occur in blocks dominated by the specified block.
|
2012-07-24 10:51:42 +00:00
|
|
|
bool Sinking::AllUsesDominatedByBlock(Instruction *Inst,
|
2010-05-07 15:40:13 +00:00
|
|
|
BasicBlock *BB) const {
|
|
|
|
// Ignoring debug uses is necessary so debug info doesn't affect the code.
|
|
|
|
// This may leave a referencing dbg_value in the original block, before
|
|
|
|
// the definition of the vreg. Dwarf generator handles this although the
|
|
|
|
// user might not get the right info at runtime.
|
2014-03-09 03:16:01 +00:00
|
|
|
for (Use &U : Inst->uses()) {
|
2010-05-07 15:40:13 +00:00
|
|
|
// Determine the block of the use.
|
2014-03-09 03:16:01 +00:00
|
|
|
Instruction *UseInst = cast<Instruction>(U.getUser());
|
2010-05-07 15:40:13 +00:00
|
|
|
BasicBlock *UseBlock = UseInst->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(UseInst)) {
|
|
|
|
// PHI nodes use the operand in the predecessor block, not the block with
|
|
|
|
// the PHI.
|
2014-03-09 03:16:01 +00:00
|
|
|
unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
|
2010-05-07 15:40:13 +00:00
|
|
|
UseBlock = PN->getIncomingBlock(Num);
|
|
|
|
}
|
|
|
|
// Check that it dominates.
|
|
|
|
if (!DT->dominates(BB, UseBlock))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sinking::runOnFunction(Function &F) {
|
2014-01-13 13:07:17 +00:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2015-01-17 14:16:18 +00:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2010-05-07 15:40:13 +00:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
bool MadeChange, EverMadeChange = false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
do {
|
|
|
|
MadeChange = false;
|
|
|
|
DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n");
|
2010-05-07 15:40:13 +00:00
|
|
|
// Process all basic blocks.
|
2012-07-24 10:51:42 +00:00
|
|
|
for (Function::iterator I = F.begin(), E = F.end();
|
2010-05-07 15:40:13 +00:00
|
|
|
I != E; ++I)
|
|
|
|
MadeChange |= ProcessBlock(*I);
|
2012-05-31 08:09:49 +00:00
|
|
|
EverMadeChange |= MadeChange;
|
|
|
|
NumSinkIter++;
|
|
|
|
} while (MadeChange);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
return EverMadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sinking::ProcessBlock(BasicBlock &BB) {
|
|
|
|
// Can't sink anything out of a block that has less than two successors.
|
|
|
|
if (BB.getTerminator()->getNumSuccessors() <= 1 || BB.empty()) return false;
|
|
|
|
|
|
|
|
// Don't bother sinking code out of unreachable blocks. In addition to being
|
2012-07-24 10:51:42 +00:00
|
|
|
// unprofitable, it can also lead to infinite looping, because in an
|
|
|
|
// unreachable loop there may be nowhere to stop.
|
2010-05-07 15:40:13 +00:00
|
|
|
if (!DT->isReachableFromEntry(&BB)) return false;
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Walk the basic block bottom-up. Remember if we saw a store.
|
|
|
|
BasicBlock::iterator I = BB.end();
|
|
|
|
--I;
|
|
|
|
bool ProcessedBegin = false;
|
|
|
|
SmallPtrSet<Instruction *, 8> Stores;
|
|
|
|
do {
|
|
|
|
Instruction *Inst = I; // The instruction to sink.
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// Predecrement I (if it's not begin) so that it isn't invalidated by
|
|
|
|
// sinking.
|
|
|
|
ProcessedBegin = I == BB.begin();
|
|
|
|
if (!ProcessedBegin)
|
|
|
|
--I;
|
|
|
|
|
|
|
|
if (isa<DbgInfoIntrinsic>(Inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (SinkInstruction(Inst, Stores))
|
|
|
|
++NumSunk, MadeChange = true;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// If we just processed the first instruction in the block, we're done.
|
|
|
|
} while (!ProcessedBegin);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA,
|
2014-08-21 05:55:13 +00:00
|
|
|
SmallPtrSetImpl<Instruction *> &Stores) {
|
2010-05-07 15:40:13 +00:00
|
|
|
|
2011-09-01 21:21:24 +00:00
|
|
|
if (Inst->mayWriteToMemory()) {
|
|
|
|
Stores.insert(Inst);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
|
2010-11-11 21:50:19 +00:00
|
|
|
AliasAnalysis::Location Loc = AA->getLocation(L);
|
2014-08-24 23:23:06 +00:00
|
|
|
for (Instruction *S : Stores)
|
|
|
|
if (AA->getModRefInfo(S, Loc) & AliasAnalysis::Mod)
|
2010-05-07 15:40:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-11 16:20:28 +00:00
|
|
|
if (isa<TerminatorInst>(Inst) || isa<PHINode>(Inst))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2010-05-07 15:40:13 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
/// IsAcceptableTarget - Return true if it is possible to sink the instruction
|
|
|
|
/// in the specified basic block.
|
2012-07-24 10:51:42 +00:00
|
|
|
bool Sinking::IsAcceptableTarget(Instruction *Inst,
|
|
|
|
BasicBlock *SuccToSinkTo) const {
|
2012-05-31 08:09:49 +00:00
|
|
|
assert(Inst && "Instruction to be sunk is null");
|
|
|
|
assert(SuccToSinkTo && "Candidate sink target is null");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
// It is not possible to sink an instruction into its own block. This can
|
|
|
|
// happen with loops.
|
|
|
|
if (Inst->getParent() == SuccToSinkTo)
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
|
|
|
// If the block has multiple predecessors, this would introduce computation
|
2012-05-31 08:09:49 +00:00
|
|
|
// on different code paths. We could split the critical edge, but for now we
|
|
|
|
// just punt.
|
|
|
|
// FIXME: Split critical edges if not backedges.
|
|
|
|
if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) {
|
|
|
|
// We cannot sink a load across a critical edge - there may be stores in
|
|
|
|
// other code paths.
|
2015-03-10 02:37:25 +00:00
|
|
|
if (!isSafeToSpeculativelyExecute(Inst))
|
2012-05-31 08:09:49 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
// We don't want to sink across a critical edge if we don't dominate the
|
|
|
|
// successor. We could be introducing calculations to new code paths.
|
|
|
|
if (!DT->dominates(Inst->getParent(), SuccToSinkTo))
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
// Don't sink instructions into a loop.
|
2012-07-24 10:51:42 +00:00
|
|
|
Loop *succ = LI->getLoopFor(SuccToSinkTo);
|
|
|
|
Loop *cur = LI->getLoopFor(Inst->getParent());
|
2014-04-25 05:29:35 +00:00
|
|
|
if (succ != nullptr && succ != cur)
|
2012-05-31 08:09:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
// Finally, check that all the uses of the instruction are actually
|
|
|
|
// dominated by the candidate
|
|
|
|
return AllUsesDominatedByBlock(Inst, SuccToSinkTo);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
/// SinkInstruction - Determine whether it is safe to sink the specified machine
|
|
|
|
/// instruction out of its current block into a successor.
|
|
|
|
bool Sinking::SinkInstruction(Instruction *Inst,
|
2014-08-21 05:55:13 +00:00
|
|
|
SmallPtrSetImpl<Instruction *> &Stores) {
|
2014-03-21 15:51:51 +00:00
|
|
|
|
|
|
|
// Don't sink static alloca instructions. CodeGen assumes allocas outside the
|
|
|
|
// entry block are dynamically sized stack objects.
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
|
|
|
|
if (AI->isStaticAlloca())
|
|
|
|
return false;
|
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// Check if it's safe to move the instruction.
|
|
|
|
if (!isSafeToMove(Inst, AA, Stores))
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// FIXME: This should include support for sinking instructions within the
|
|
|
|
// block they are currently in to shorten the live ranges. We often get
|
|
|
|
// instructions sunk into the top of a large block, but it would be better to
|
|
|
|
// also sink them down before their first use in the block. This xform has to
|
|
|
|
// be careful not to *increase* register pressure though, e.g. sinking
|
|
|
|
// "x = y + z" down if it kills y and z would increase the live ranges of y
|
|
|
|
// and z and only shrink the live range of x.
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// SuccToSinkTo - This is the successor to sink this instruction to, once we
|
|
|
|
// decide.
|
2014-04-25 05:29:35 +00:00
|
|
|
BasicBlock *SuccToSinkTo = nullptr;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// Instructions can only be sunk if all their uses are in blocks
|
|
|
|
// dominated by one of the successors.
|
2012-05-31 08:09:49 +00:00
|
|
|
// Look at all the postdominators and see if we can sink it in one.
|
|
|
|
DomTreeNode *DTN = DT->getNode(Inst->getParent());
|
2012-07-24 10:51:42 +00:00
|
|
|
for (DomTreeNode::iterator I = DTN->begin(), E = DTN->end();
|
2014-04-25 05:29:35 +00:00
|
|
|
I != E && SuccToSinkTo == nullptr; ++I) {
|
2012-05-31 08:09:49 +00:00
|
|
|
BasicBlock *Candidate = (*I)->getBlock();
|
2012-07-24 10:51:42 +00:00
|
|
|
if ((*I)->getIDom()->getBlock() == Inst->getParent() &&
|
2012-05-31 08:09:49 +00:00
|
|
|
IsAcceptableTarget(Inst, Candidate))
|
|
|
|
SuccToSinkTo = Candidate;
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:51:42 +00:00
|
|
|
// If no suitable postdominator was found, look at all the successors and
|
2012-05-31 08:09:49 +00:00
|
|
|
// decide which one we should sink to, if any.
|
2014-07-21 17:06:51 +00:00
|
|
|
for (succ_iterator I = succ_begin(Inst->getParent()),
|
|
|
|
E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) {
|
|
|
|
if (IsAcceptableTarget(Inst, *I))
|
|
|
|
SuccToSinkTo = *I;
|
2010-05-07 15:40:13 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// If we couldn't find a block to sink to, ignore this instruction.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!SuccToSinkTo)
|
2010-05-07 15:40:13 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-05-31 08:09:49 +00:00
|
|
|
DEBUG(dbgs() << "Sink" << *Inst << " (";
|
2014-01-09 02:29:41 +00:00
|
|
|
Inst->getParent()->printAsOperand(dbgs(), false);
|
2012-05-31 08:09:49 +00:00
|
|
|
dbgs() << " -> ";
|
2014-01-09 02:29:41 +00:00
|
|
|
SuccToSinkTo->printAsOperand(dbgs(), false);
|
2012-05-31 08:09:49 +00:00
|
|
|
dbgs() << ")\n");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-05-07 15:40:13 +00:00
|
|
|
// Move the instruction.
|
2012-05-31 08:09:49 +00:00
|
|
|
Inst->moveBefore(SuccToSinkTo->getFirstInsertionPt());
|
2010-05-07 15:40:13 +00:00
|
|
|
return true;
|
|
|
|
}
|