29 #define DEBUG_TYPE "loop-unswitch"
56 STATISTIC(NumBranches,
"Number of branches unswitched");
57 STATISTIC(NumSwitches,
"Number of switches unswitched");
58 STATISTIC(NumSelects ,
"Number of selects unswitched");
59 STATISTIC(NumTrivial ,
"Number of unswitches that are trivial");
60 STATISTIC(NumSimplify,
"Number of simplifications of unswitched code");
61 STATISTIC(TotalInsts,
"Total number of instructions analyzed");
71 class LUAnalysisCache {
76 typedef UnswitchedValsMap::iterator UnswitchedValsIt;
78 struct LoopProperties {
79 unsigned CanBeUnswitchedCount;
80 unsigned SizeEstimation;
81 UnswitchedValsMap UnswitchedVals;
86 typedef std::map<const Loop*, LoopProperties> LoopPropsMap;
87 typedef LoopPropsMap::iterator LoopPropsMapIt;
89 LoopPropsMap LoopsProperties;
90 UnswitchedValsMap *CurLoopInstructions;
91 LoopProperties *CurrentLoopProperties;
99 CurLoopInstructions(0), CurrentLoopProperties(0),
108 void forgetLoop(
const Loop *L);
121 void cloneData(
const Loop *NewLoop,
const Loop *OldLoop,
125 class LoopUnswitch :
public LoopPass {
131 std::vector<Loop*> LoopProcessWorklist;
133 LUAnalysisCache BranchesInfo;
135 bool OptimizeForSize;
146 std::vector<BasicBlock*> LoopBlocks;
148 std::vector<BasicBlock*> NewBlocks;
152 explicit LoopUnswitch(
bool Os =
false) :
154 currentLoop(0), DT(0), loopHeader(0),
160 bool processCurrentLoop();
179 virtual void releaseMemory() {
180 BranchesInfo.forgetLoop(currentLoop);
185 void RemoveLoopFromWorklist(
Loop *L) {
186 std::vector<Loop*>::iterator
I = std::find(LoopProcessWorklist.begin(),
187 LoopProcessWorklist.end(), L);
188 if (I != LoopProcessWorklist.end())
189 LoopProcessWorklist.erase(I);
192 void initLoopData() {
193 loopHeader = currentLoop->getHeader();
194 loopPreheader = currentLoop->getLoopPreheader();
206 void RewriteLoopBodyWithConditionConstant(
Loop *L,
Value *LIC,
209 void EmitPreheaderBranchOnCondition(
Value *LIC,
Constant *Val,
214 void SimplifyCode(std::vector<Instruction*> &Worklist,
Loop *L);
215 void RemoveLoopFromHierarchy(
Loop *L);
216 bool IsTrivialUnswitchCondition(
Value *Cond,
Constant **Val = 0,
226 LoopPropsMapIt PropsIt;
229 LoopsProperties.insert(std::make_pair(L, LoopProperties()));
231 LoopProperties &Props = PropsIt->second;
250 Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation);
251 MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount;
255 << L->
getHeader()->getName() <<
", contents cannot be "
261 if (!Props.CanBeUnswitchedCount) {
263 << L->
getHeader()->getName() <<
", cost too high: "
269 CurrentLoopProperties = &Props;
270 CurLoopInstructions = &Props.UnswitchedVals;
276 void LUAnalysisCache::forgetLoop(
const Loop *L) {
278 LoopPropsMapIt LIt = LoopsProperties.find(L);
280 if (LIt != LoopsProperties.end()) {
281 LoopProperties &Props = LIt->second;
282 MaxSize += Props.CanBeUnswitchedCount * Props.SizeEstimation;
283 LoopsProperties.erase(LIt);
286 CurrentLoopProperties = 0;
287 CurLoopInstructions = 0;
293 void LUAnalysisCache::setUnswitched(
const SwitchInst *SI,
const Value *V) {
294 (*CurLoopInstructions)[SI].insert(V);
298 bool LUAnalysisCache::isUnswitched(
const SwitchInst *SI,
const Value *V) {
299 return (*CurLoopInstructions)[SI].count(V);
305 void LUAnalysisCache::cloneData(
const Loop *NewLoop,
const Loop *OldLoop,
308 LoopProperties &NewLoopProps = LoopsProperties[NewLoop];
309 LoopProperties &OldLoopProps = *CurrentLoopProperties;
310 UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals;
314 --OldLoopProps.CanBeUnswitchedCount;
315 unsigned Quota = OldLoopProps.CanBeUnswitchedCount;
316 NewLoopProps.CanBeUnswitchedCount = Quota / 2;
317 OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2;
319 NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation;
324 for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++
I) {
327 const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI);
328 assert(NewInst &&
"All instructions that are in SrcBB must be in VMap.");
330 NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst];
345 return new LoopUnswitch(Os);
361 if (isa<Constant>(Cond))
return 0;
385 LI = &getAnalysis<LoopInfo>();
387 DT = getAnalysisIfAvailable<DominatorTree>();
389 Function *
F = currentLoop->getHeader()->getParent();
390 bool Changed =
false;
392 assert(currentLoop->isLCSSAForm(*DT));
394 Changed |= processCurrentLoop();
400 DT->runOnFunction(*F);
407 bool LoopUnswitch::processCurrentLoop() {
408 bool Changed =
false;
417 if (!currentLoop->isSafeToClone())
421 if (!currentLoop->hasDedicatedExits())
428 if (!BranchesInfo.countLoop(currentLoop, getAnalysis<TargetTransformInfo>()))
435 E = currentLoop->block_end(); I != E; ++
I) {
437 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
440 if (BI->isConditional()) {
444 currentLoop, Changed);
445 if (LoopCond && UnswitchIfProfitable(LoopCond,
451 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
453 currentLoop, Changed);
455 if (LoopCond && NumCases) {
466 Constant *UnswitchValCandidate = i.getCaseValue();
467 if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
468 UnswitchVal = UnswitchValCandidate;
476 if (UnswitchIfProfitable(LoopCond, UnswitchVal)) {
486 if (
SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
488 currentLoop, Changed);
489 if (LoopCond && UnswitchIfProfitable(LoopCond,
507 std::set<BasicBlock*> &Visited) {
508 if (!Visited.insert(BB).second) {
516 if (ExitBB != 0)
return false;
531 if (I->mayHaveSideEffects())
541 std::set<BasicBlock*> Visited;
561 bool LoopUnswitch::IsTrivialUnswitchCondition(
Value *Cond,
Constant **Val,
563 BasicBlock *Header = currentLoop->getHeader();
568 if (
BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
571 if (!BI->isConditional() || BI->getCondition() != Cond)
579 BI->getSuccessor(0)))) {
582 BI->getSuccessor(1)))) {
585 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
599 i.getCaseSuccessor()))) {
605 if (BranchesInfo.isUnswitched(SI, CaseVal))
607 LoopExitBB = LoopExitCandidate;
608 if (Val) *Val = CaseVal;
616 if (!LoopExitBB || isa<PHINode>(LoopExitBB->
begin()))
619 if (LoopExit) *LoopExit = LoopExitBB;
627 if (I->mayHaveSideEffects())
635 bool LoopUnswitch::UnswitchIfProfitable(
Value *LoopCond,
Constant *Val) {
640 if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) {
643 UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock);
650 if (OptimizeForSize ||
655 UnswitchNontrivialCondition(LoopCond, Val, currentLoop);
682 void LoopUnswitch::EmitPreheaderBranchOnCondition(
Value *LIC,
Constant *Val,
688 Value *BranchVal = LIC;
689 if (!isa<ConstantInt>(Val) ||
710 void LoopUnswitch::UnswitchTrivialCondition(
Loop *L,
Value *Cond,
713 DEBUG(
dbgs() <<
"loop-unswitch: Trivial-Unswitch loop %"
714 << loopHeader->getName() <<
" [" << L->
getBlocks().size()
715 <<
" blocks] in Function " << L->
getHeader()->getParent()->getName()
716 <<
" on cond: " << *Val <<
" == " << *Cond <<
"\n");
731 assert(!L->
contains(ExitBlock) &&
"Exit block is in the loop?");
736 EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
737 loopPreheader->getTerminator());
738 LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
747 RewriteLoopBodyWithConditionConstant(L, Cond, Val,
false);
753 void LoopUnswitch::SplitExitEdges(
Loop *L,
756 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i) {
776 void LoopUnswitch::UnswitchNontrivialCondition(
Value *LIC,
Constant *Val,
779 DEBUG(
dbgs() <<
"loop-unswitch: Unswitching loop %"
780 << loopHeader->getName() <<
" [" << L->
getBlocks().size()
781 <<
" blocks] in Function " << F->
getName()
782 <<
" when '" << *Val <<
"' == " << *LIC <<
"\n");
793 LoopBlocks.push_back(NewPreheader);
803 SplitExitEdges(L, ExitBlocks);
810 LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.
begin(), ExitBlocks.
end());
815 NewBlocks.reserve(LoopBlocks.size());
817 for (
unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
820 NewBlocks.push_back(NewBB);
821 VMap[LoopBlocks[i]] = NewBB;
822 LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L);
828 NewBlocks[0], F->
end());
835 BranchesInfo.cloneData(NewLoop, L, VMap);
844 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i) {
845 BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]);
847 if (
Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i]))
848 ExitBBLoop->addBasicBlockToLoop(NewExit, LI->getBase());
851 "Exit block should have been split to have one successor!");
866 ExitSucc->getFirstInsertionPt());
879 for (
unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
881 E = NewBlocks[i]->
end(); I != E; ++
I)
885 BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());
887 "Preheader splitting did not work correctly!");
890 EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR);
891 LPM->deleteSimpleAnalysisValue(OldBR, L);
894 LoopProcessWorklist.push_back(NewLoop);
905 RewriteLoopBodyWithConditionConstant(L, LIC, Val,
false);
910 if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop &&
911 LICHandle && !isa<Constant>(LICHandle))
912 RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val,
true);
918 std::vector<Instruction*> &Worklist) {
920 Worklist.erase(
std::remove(Worklist.begin(), Worklist.end(),
I),
927 std::vector<Instruction*> &Worklist,
929 DEBUG(
dbgs() <<
"Replace with '" << *V <<
"': " << *I);
934 Worklist.push_back(
Use);
939 Worklist.push_back(cast<Instruction>(*UI));
953 void LoopUnswitch::RemoveLoopFromHierarchy(
Loop *L) {
954 LPM->deleteLoopFromQueue(L);
955 RemoveLoopFromWorklist(L);
961 void LoopUnswitch::RewriteLoopBodyWithConditionConstant(
Loop *L,
Value *LIC,
964 assert(!isa<Constant>(LIC) &&
"Why are we unswitching on a constant?");
975 std::vector<Instruction*> Worklist;
980 if (IsEqual || (isa<ConstantInt>(Val) &&
987 !cast<ConstantInt>(Val)->getZExtValue());
994 Worklist.push_back(U);
997 for (std::vector<Instruction*>::iterator UI = Worklist.begin(),
998 UE = Worklist.end(); UI != UE; ++UI)
1001 SimplifyCode(Worklist, L);
1014 Worklist.push_back(U);
1021 if (SI == 0 || !isa<ConstantInt>(Val))
continue;
1035 BranchesInfo.setUnswitched(SI, Val);
1041 if (Latch && DT->dominates(SISucc, Latch))
1074 DT->addNewBlock(Abort, NewSISucc);
1077 SimplifyCode(Worklist, L);
1089 void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist,
Loop *L) {
1090 while (!Worklist.empty()) {
1092 Worklist.pop_back();
1096 DEBUG(
dbgs() <<
"Remove dead instruction '" << *I);
1101 Worklist.push_back(
Use);
1102 LPM->deleteSimpleAnalysisValue(I, L);
1113 if (LI->replacementPreservesLCSSAForm(I, V)) {
1119 if (
BranchInst *BI = dyn_cast<BranchInst>(I)) {
1120 if (BI->isUnconditional()) {
1126 if (!SinglePred)
continue;
1127 assert(SinglePred == Pred &&
"CFG broken");
1143 LPM->deleteSimpleAnalysisValue(BI, L);
1144 BI->eraseFromParent();
1148 LI->removeBlock(Succ);
1149 LPM->deleteSimpleAnalysisValue(Succ, L);
void initializeLoopUnswitchPass(PassRegistry &)
static ConstantInt * getFalse(LLVMContext &Context)
AnalysisUsage & addPreserved()
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P)
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
static PassRegistry * getPassRegistry()
int remove(const char *path);
INITIALIZE_PASS_BEGIN(LoopUnswitch,"loop-unswitch","Unswitch loops", false, false) INITIALIZE_PASS_END(LoopUnswitch
ValueT lookup(const KeyT &Val) const
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P=0, bool MergeIdenticalEdges=false, bool DontDeleteUselessPHIs=false, bool SplitLandingPads=false)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getNumOperands() const
const_iterator begin(StringRef path)
Get begin iterator over path.
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
bool notDuplicatable
True if this function cannot be duplicated.
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
const std::vector< BlockT * > & getBlocks() const
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
BlockT * getHeader() const
ConstantInt * findCaseDest(BasicBlock *BB)
LoopInfoBase< BlockT, LoopT > * LI
StringRef getName() const
BlockT * getLoopLatch() const
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isUnconditional() const
static Loop * CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
unsigned NumBlocks
Number of analyzed blocks.
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI)
Add information about a block to the current state.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
ID
LLVM Calling Convention Representation.
Interval::succ_iterator succ_begin(Interval *I)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, Pass *P=0)
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
static Value * FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed)
Pass * createLoopUnswitchPass(bool OptimizeForSize=false)
BasicBlock * getSuccessor(unsigned i) const
iterator find(const KeyT &Val)
AnalysisUsage & addPreservedID(const void *ID)
Loop * getLoopFor(const BasicBlock *BB) const
void replaceAllUsesWith(Value *V)
Interval::succ_iterator succ_end(Interval *I)
void replaceUsesOfWith(Value *From, Value *To)
unsigned getNumSuccessors() const
static void RemoveFromWorklist(Instruction *I, std::vector< Instruction * > &Worklist)
initializer< Ty > init(const Ty &Val)
void insertLoop(Loop *L, Loop *ParentLoop)
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
LLVM Constant Representation.
Instr is a loop (backwards branch).
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=0)
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Interval::pred_iterator pred_begin(Interval *I)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
bool contains(const LoopT *L) const
const InstListType & getInstList() const
Return the underlying instruction list container.
Represent an integer comparison operator.
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=0) const
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
#define INITIALIZE_AG_DEPENDENCY(depName)
static UndefValue * get(Type *T)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
LLVMContext & getContext() const
All values hold a context through their type.
void deleteSimpleAnalysisValue(Value *V, Loop *L)
deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
machine trace Machine Trace Metrics
const BasicBlockListType & getBasicBlockList() const
Class for constant integers.
Value * getIncomingValue(unsigned i) const
AnalysisUsage & addRequiredID(const void *ID)
void eraseFromParent()
Unlink 'this' from the containing function and delete it.
STATISTIC(NumBranches,"Number of branches unswitched")
Utility to calculate the size and a few similar metrics for a set of basic blocks.
BasicBlockTy * getCaseSuccessor()
Resolves successor for current case.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
CaseIt findCaseValue(const ConstantInt *C)
static ConstantInt * getTrue(LLVMContext &Context)
void splice(iterator where, iplist &L2)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
AttributeSet getAttributes() const
Return the attribute list for this Function.
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getSinglePredecessor()
Return this block if it has a single predecessor block. Otherwise return a null pointer.
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
std::vector< BlockT * >::const_iterator block_iterator
Value * getCondition() const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, BasicBlock *&ExitBB, std::set< BasicBlock * > &Visited)
block_iterator block_end() const
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=0, ClonedCodeInfo *CodeInfo=0)
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isLandingPad() const
Return true if this basic block is a landing pad.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
unsigned getNumCases() const
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
static void ReplaceUsesOfWith(Instruction *I, Value *V, std::vector< Instruction * > &Worklist, Loop *L, LPPassManager *LPM)
block_iterator block_begin() const
std::vector< LoopT * >::const_iterator iterator
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, Pass *P, SmallVectorImpl< BasicBlock * > &NewBBs)
void setIncomingValue(unsigned i, Value *V)
unsigned NumInsts
Number of instructions in the analyzed blocks.
static BasicBlock * isTrivialLoopExitBlock(Loop *L, BasicBlock *BB)
tier< T1, T2 > tie(T1 &f, T2 &s)
int getBasicBlockIndex(const BasicBlock *BB) const
LoopInfoBase< BasicBlock, Loop > & getBase()
const BasicBlock * getParent() const