//===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Peephole optimize the CFG.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "simplifycfg"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Type.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/Debug.h"
#include <algorithm>
#include <functional>
#include <set>
#include <map>
using namespace llvm;
// PropagatePredecessorsForPHIs - This gets "Succ" ready to have the
// predecessors from "BB". This is a little tricky because "Succ" has PHI
// nodes, which need to have extra slots added to them to hold the merge edges
// from BB's predecessors, and BB itself might have had PHI nodes in it. This
// function returns true (failure) if the Succ BB already has a predecessor that
// is a predecessor of BB and incoming PHI arguments would not be discernible.
//
// Assumption: Succ is the single successor for BB.
//
static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
if (!isa<PHINode>(Succ->front()))
return false; // We can make the transformation, no problem.
// If there is more than one predecessor, and there are PHI nodes in
// the successor, then we need to add incoming edges for the PHI nodes
//
const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
// Check to see if one of the predecessors of BB is already a predecessor of
// Succ. If so, we cannot do the transformation if there are any PHI nodes
// with incompatible values coming in from the two edges!
//
for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI)
if (std::find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
// Loop over all of the PHI nodes checking to see if there are
// incompatible values coming in.
for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
<