36
36
#include " llvm/ADT/APInt.h"
37
37
#include " llvm/ADT/ArrayRef.h"
38
38
#include " llvm/ADT/DenseMap.h"
39
+ #include " llvm/ADT/PostOrderIterator.h"
39
40
#include " llvm/ADT/SmallPtrSet.h"
40
41
#include " llvm/ADT/SmallVector.h"
41
42
#include " llvm/ADT/Statistic.h"
@@ -4108,31 +4109,29 @@ class AliasScopeTracker {
4108
4109
}
4109
4110
};
4110
4111
4111
- // / Populate the IC worklist from a function, by walking it in depth-first
4112
- // / order and adding all reachable code to the worklist.
4112
+ // / Populate the IC worklist from a function, by walking it in reverse
4113
+ // / post- order and adding all reachable code to the worklist.
4113
4114
// /
4114
4115
// / This has a couple of tricks to make the code faster and more powerful. In
4115
4116
// / particular, we constant fold and DCE instructions as we go, to avoid adding
4116
4117
// / them to the worklist (this significantly speeds up instcombine on code where
4117
4118
// / many instructions are dead or constant). Additionally, if we find a branch
4118
4119
// / whose condition is a known constant, we only visit the reachable successors.
4119
- static bool prepareICWorklistFromFunction (Function &F, const DataLayout &DL,
4120
- const TargetLibraryInfo *TLI,
4121
- InstructionWorklist &ICWorklist) {
4120
+ static bool
4121
+ prepareICWorklistFromFunction (Function &F, const DataLayout &DL,
4122
+ const TargetLibraryInfo *TLI,
4123
+ InstructionWorklist &ICWorklist,
4124
+ ReversePostOrderTraversal<BasicBlock *> &RPOT) {
4122
4125
bool MadeIRChange = false ;
4123
- SmallPtrSet<BasicBlock *, 32 > Visited;
4124
- SmallVector<BasicBlock*, 256 > Worklist;
4125
- Worklist.push_back (&F.front ());
4126
+ SmallPtrSet<BasicBlock *, 32 > LiveBlocks;
4127
+ LiveBlocks.insert (&F.front ());
4126
4128
4127
4129
SmallVector<Instruction *, 128 > InstrsForInstructionWorklist;
4128
4130
DenseMap<Constant *, Constant *> FoldedConstants;
4129
4131
AliasScopeTracker SeenAliasScopes;
4130
4132
4131
- do {
4132
- BasicBlock *BB = Worklist.pop_back_val ();
4133
-
4134
- // We have now visited this block! If we've already been here, ignore it.
4135
- if (!Visited.insert (BB).second )
4133
+ for (BasicBlock *BB : RPOT) {
4134
+ if (!LiveBlocks.count (BB))
4136
4135
continue ;
4137
4136
4138
4137
for (Instruction &Inst : llvm::make_early_inc_range (*BB)) {
@@ -4178,8 +4177,8 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
4178
4177
}
4179
4178
}
4180
4179
4181
- // Recursively visit successors. If this is a branch or switch on a
4182
- // constant, only visit the reachable successor .
4180
+ // If this is a branch or switch on a constant, mark only the single
4181
+ // live successor. Otherwise assume all successors are live .
4183
4182
Instruction *TI = BB->getTerminator ();
4184
4183
if (BranchInst *BI = dyn_cast<BranchInst>(TI); BI && BI->isConditional ()) {
4185
4184
if (isa<UndefValue>(BI->getCondition ()))
@@ -4188,27 +4187,28 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
4188
4187
if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition ())) {
4189
4188
bool CondVal = Cond->getZExtValue ();
4190
4189
BasicBlock *ReachableBB = BI->getSuccessor (!CondVal);
4191
- Worklist. push_back (ReachableBB);
4190
+ LiveBlocks. insert (ReachableBB);
4192
4191
continue ;
4193
4192
}
4194
4193
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
4195
4194
if (isa<UndefValue>(SI->getCondition ()))
4196
4195
// Switch on undef is UB.
4197
4196
continue ;
4198
4197
if (auto *Cond = dyn_cast<ConstantInt>(SI->getCondition ())) {
4199
- Worklist. push_back (SI->findCaseValue (Cond)->getCaseSuccessor ());
4198
+ LiveBlocks. insert (SI->findCaseValue (Cond)->getCaseSuccessor ());
4200
4199
continue ;
4201
4200
}
4202
4201
}
4203
4202
4204
- append_range (Worklist, successors (TI));
4205
- } while (!Worklist.empty ());
4203
+ for (BasicBlock *SuccBB : successors (TI))
4204
+ LiveBlocks.insert (SuccBB);
4205
+ }
4206
4206
4207
4207
// Remove instructions inside unreachable blocks. This prevents the
4208
4208
// instcombine code from having to deal with some bad special cases, and
4209
4209
// reduces use counts of instructions.
4210
4210
for (BasicBlock &BB : F) {
4211
- if (Visited .count (&BB))
4211
+ if (LiveBlocks .count (&BB))
4212
4212
continue ;
4213
4213
4214
4214
unsigned NumDeadInstInBB;
@@ -4262,6 +4262,8 @@ static bool combineInstructionsOverFunction(
4262
4262
AC.registerAssumption (Assume);
4263
4263
}));
4264
4264
4265
+ ReversePostOrderTraversal<BasicBlock *> RPOT (&F.front ());
4266
+
4265
4267
// Lower dbg.declare intrinsics otherwise their value may be clobbered
4266
4268
// by instcombiner.
4267
4269
bool MadeIRChange = false ;
@@ -4290,7 +4292,7 @@ static bool combineInstructionsOverFunction(
4290
4292
LLVM_DEBUG (dbgs () << " \n\n INSTCOMBINE ITERATION #" << Iteration << " on "
4291
4293
<< F.getName () << " \n " );
4292
4294
4293
- MadeIRChange |= prepareICWorklistFromFunction (F, DL, &TLI, Worklist);
4295
+ MadeIRChange |= prepareICWorklistFromFunction (F, DL, &TLI, Worklist, RPOT );
4294
4296
4295
4297
InstCombinerImpl IC (Worklist, Builder, F.hasMinSize (), AA, AC, TLI, TTI, DT,
4296
4298
ORE, BFI, PSI, DL, LI);
0 commit comments