Skip to content

Commit 84986b2

Browse files
author
Cameron Zwarich
committed
Make more passes preserve dominators (or state that they preserve dominators if
they all ready do). This removes two dominator recomputations prior to isel, which is a 1% improvement in total llc time for 403.gcc. The only potentially suspect thing is making GCStrategy recompute dominators if it used a custom lowering strategy. llvm-svn: 123064
1 parent 45e6c19 commit 84986b2

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

llvm/lib/CodeGen/GCStrategy.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/CodeGen/Passes.h"
2020
#include "llvm/IntrinsicInst.h"
2121
#include "llvm/Module.h"
22+
#include "llvm/Analysis/Dominators.h"
2223
#include "llvm/CodeGen/MachineFrameInfo.h"
2324
#include "llvm/CodeGen/MachineFunctionPass.h"
2425
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -146,6 +147,7 @@ const char *LowerIntrinsics::getPassName() const {
146147
void LowerIntrinsics::getAnalysisUsage(AnalysisUsage &AU) const {
147148
FunctionPass::getAnalysisUsage(AU);
148149
AU.addRequired<GCModuleInfo>();
150+
AU.addPreserved<DominatorTree>();
149151
}
150152

151153
/// doInitialization - If this module uses the GC intrinsics, find them now.
@@ -256,9 +258,16 @@ bool LowerIntrinsics::runOnFunction(Function &F) {
256258
if (NeedsDefaultLoweringPass(S))
257259
MadeChange |= PerformDefaultLowering(F, S);
258260

259-
if (NeedsCustomLoweringPass(S))
261+
bool UseCustomLoweringPass = NeedsCustomLoweringPass(S);
262+
if (UseCustomLoweringPass)
260263
MadeChange |= S.performCustomLowering(F);
261-
264+
265+
// Custom lowering may modify the CFG, so dominators must be recomputed.
266+
if (UseCustomLoweringPass) {
267+
if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>())
268+
DT->DT->recalculate(F);
269+
}
270+
262271
return MadeChange;
263272
}
264273

llvm/lib/CodeGen/StackProtector.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#define DEBUG_TYPE "stack-protector"
1818
#include "llvm/CodeGen/Passes.h"
19+
#include "llvm/Analysis/Dominators.h"
1920
#include "llvm/Attributes.h"
2021
#include "llvm/Constants.h"
2122
#include "llvm/DerivedTypes.h"
@@ -45,6 +46,8 @@ namespace {
4546
Function *F;
4647
Module *M;
4748

49+
DominatorTree* DT;
50+
4851
/// InsertStackProtectors - Insert code into the prologue and epilogue of
4952
/// the function.
5053
///
@@ -70,6 +73,10 @@ namespace {
7073
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
7174
}
7275

76+
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77+
AU.addPreserved<DominatorTree>();
78+
}
79+
7380
virtual bool runOnFunction(Function &Fn);
7481
};
7582
} // end anonymous namespace
@@ -85,6 +92,7 @@ FunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) {
8592
bool StackProtector::runOnFunction(Function &Fn) {
8693
F = &Fn;
8794
M = F->getParent();
95+
DT = getAnalysisIfAvailable<DominatorTree>();
8896

8997
if (!RequiresStackProtector()) return false;
9098

@@ -139,6 +147,7 @@ bool StackProtector::RequiresStackProtector() const {
139147
/// value. It calls __stack_chk_fail if they differ.
140148
bool StackProtector::InsertStackProtectors() {
141149
BasicBlock *FailBB = 0; // The basic block to jump to if check fails.
150+
BasicBlock *FailBBDom = 0; // FailBB's dominator.
142151
AllocaInst *AI = 0; // Place on stack that stores the stack guard.
143152
Value *StackGuardVar = 0; // The stack guard variable.
144153

@@ -182,6 +191,8 @@ bool StackProtector::InsertStackProtectors() {
182191

183192
// Create the basic block to jump to when the guard check fails.
184193
FailBB = CreateFailBB();
194+
if (DT)
195+
FailBBDom = DT->isReachableFromEntry(BB) ? BB : 0;
185196
}
186197

187198
// For each block with a return instruction, convert this:
@@ -208,6 +219,10 @@ bool StackProtector::InsertStackProtectors() {
208219

209220
// Split the basic block before the return instruction.
210221
BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
222+
if (DT) {
223+
DT->addNewBlock(NewBB, DT->isReachableFromEntry(BB) ? BB : 0);
224+
FailBBDom = DT->findNearestCommonDominator(FailBBDom, BB);
225+
}
211226

212227
// Remove default branch instruction to the new BB.
213228
BB->getTerminator()->eraseFromParent();
@@ -227,6 +242,9 @@ bool StackProtector::InsertStackProtectors() {
227242
// statements in the function.
228243
if (!FailBB) return false;
229244

245+
if (DT)
246+
DT->addNewBlock(FailBB, FailBBDom);
247+
230248
return true;
231249
}
232250

llvm/lib/CodeGen/UnreachableBlockElim.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Function.h"
2727
#include "llvm/Pass.h"
2828
#include "llvm/Type.h"
29+
#include "llvm/Analysis/Dominators.h"
2930
#include "llvm/Analysis/ProfileInfo.h"
3031
#include "llvm/CodeGen/MachineDominators.h"
3132
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -48,6 +49,7 @@ namespace {
4849
}
4950

5051
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52+
AU.addPreserved<DominatorTree>();
5153
AU.addPreserved<ProfileInfo>();
5254
}
5355
};

llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Instructions.h"
2323
#include "llvm/IntrinsicInst.h"
2424
#include "llvm/Pass.h"
25+
#include "llvm/Analysis/Dominators.h"
2526
#include "llvm/Analysis/InstructionSimplify.h"
2627
#include "llvm/Analysis/ProfileInfo.h"
2728
#include "llvm/Target/TargetData.h"
@@ -66,6 +67,7 @@ namespace {
6667
/// TLI - Keep a pointer of a TargetLowering to consult for determining
6768
/// transformation profitability.
6869
const TargetLowering *TLI;
70+
DominatorTree *DT;
6971
ProfileInfo *PFI;
7072

7173
/// BackEdges - Keep a set of all the loop back edges.
@@ -86,6 +88,7 @@ namespace {
8688
bool runOnFunction(Function &F);
8789

8890
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
91+
AU.addPreserved<DominatorTree>();
8992
AU.addPreserved<ProfileInfo>();
9093
}
9194

@@ -131,6 +134,7 @@ void CodeGenPrepare::findLoopBackEdges(const Function &F) {
131134
bool CodeGenPrepare::runOnFunction(Function &F) {
132135
bool EverMadeChange = false;
133136

137+
DT = getAnalysisIfAvailable<DominatorTree>();
134138
PFI = getAnalysisIfAvailable<ProfileInfo>();
135139
// First pass, eliminate blocks that contain only PHI nodes and an
136140
// unconditional branch.
@@ -325,6 +329,13 @@ void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
325329
// The PHIs are now updated, change everything that refers to BB to use
326330
// DestBB and remove BB.
327331
BB->replaceAllUsesWith(DestBB);
332+
if (DT) {
333+
BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock();
334+
BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock();
335+
BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom);
336+
DT->changeImmediateDominator(DestBB, NewIDom);
337+
DT->eraseNode(BB);
338+
}
328339
if (PFI) {
329340
PFI->replaceAllUses(BB, DestBB);
330341
PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IntrinsicInst.h"
2323
#include "llvm/ADT/DenseMap.h"
2424
#include "llvm/ADT/SmallPtrSet.h"
25+
#include "llvm/Analysis/Dominators.h"
2526
#include "llvm/Analysis/ConstantFolding.h"
2627
#include "llvm/Analysis/InstructionSimplify.h"
2728
#include "llvm/Analysis/ProfileInfo.h"
@@ -401,6 +402,12 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
401402
PredBB->replaceAllUsesWith(DestBB);
402403

403404
if (P) {
405+
DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
406+
if (DT) {
407+
BasicBlock *PredBBIDom = DT->getNode(PredBB)->getIDom()->getBlock();
408+
DT->changeImmediateDominator(DestBB, PredBBIDom);
409+
DT->eraseNode(PredBB);
410+
}
404411
ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
405412
if (PI) {
406413
PI->replaceAllUses(PredBB, DestBB);

0 commit comments

Comments
 (0)