[llvm-commits] [llvm] r158392 - in /llvm/trunk: lib/Transforms/Utils/SimplifyCFG.cpp test/Transforms/SimplifyCFG/branch-fold.ll

Nuno Lopes nunoplopes at sapo.pt
Wed Jun 13 06:51:43 PDT 2012


Hi,

> Author: mren
> Date: Wed Jun 13 00:43:29 2012
> New Revision: 158392
>
> URL: http://llvm.org/viewvc/llvm-project?rev=158392&view=rev
> Log:
> SimplifyCFG: fold unconditional branch to its predecessor if profitable.
>
> This patch extends FoldBranchToCommonDest to fold unconditional branches.
> For unconditional branches, we fold them if it is easy to update the phi 
> nodes
> in the common successors.
>
> rdar://10554090

So, if I understand correctly the code, the profitability heuristic is that 
if all instructions in a basic block are also computed in it's immediate 
predecessor, then the unconditional branch is folded.
I have two comments:
- When not optimizing for size, I think it makes sense to look at the branch 
probability metadata and be more aggressive in the folding for highly 
probably-taken branches. The performance impact for the not taken branch 
will be negligible (since the branch won't be taken very often).
- Not really related with your patch, but rather a more broaden question: 
Now that the register allocator can rematerialize instructions when the live 
intervals are too long (Jakob: please correct if I'm wrong), doesn't it make 
sense to actually reuse all computations done in dominating basic blocks? 
This patch is looking for opportunities to reuse redundant computations in 
adjacent blocks, but I believe we could do it for arbitrary BBs, as long as 
one of them dominates the other.



> +/// checkCSEInPredecessor - Return true if the given instruction is 
> available
> +/// in its predecessor block. If yes, the instruction will be removed.
> +///
> +bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB) {
> +  if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
> +    return false;

Here you can use Inst->mayHaveSideEffects(), since all instructions without 
side-effects are safe to be removed (and not just binary operators and 
compare instructions).

Nuno 




More information about the llvm-commits mailing list