2007-08-22 05:42:03 +08:00
|
|
|
//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-22 05:42:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the CFG and CFGBuilder classes for representing and
|
|
|
|
// building Control-Flow Graphs (CFGs) from ASTs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/CFG.h"
|
2007-08-22 06:06:14 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-09-01 05:30:12 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
2007-08-22 07:26:17 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-08-29 03:26:49 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2007-08-30 05:56:09 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2007-12-18 03:35:20 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-01-09 02:15:10 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-04-29 02:00:46 +08:00
|
|
|
#include <llvm/Support/Allocator.h>
|
2008-09-13 13:16:45 +08:00
|
|
|
#include <llvm/Support/Format.h>
|
2007-08-22 05:42:03 +08:00
|
|
|
#include <iomanip>
|
|
|
|
#include <algorithm>
|
2007-08-30 05:56:09 +08:00
|
|
|
#include <sstream>
|
2008-01-11 08:40:29 +08:00
|
|
|
|
2007-08-22 05:42:03 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2007-08-24 05:26:19 +08:00
|
|
|
// SaveAndRestore - A utility class that uses RIIA to save and restore
|
|
|
|
// the value of a variable.
|
|
|
|
template<typename T>
|
2008-01-09 02:15:10 +08:00
|
|
|
struct VISIBILITY_HIDDEN SaveAndRestore {
|
2007-08-24 05:26:19 +08:00
|
|
|
SaveAndRestore(T& x) : X(x), old_value(x) {}
|
|
|
|
~SaveAndRestore() { X = old_value; }
|
2007-08-31 02:13:31 +08:00
|
|
|
T get() { return old_value; }
|
|
|
|
|
2007-08-24 05:26:19 +08:00
|
|
|
T& X;
|
|
|
|
T old_value;
|
|
|
|
};
|
2007-08-22 05:42:03 +08:00
|
|
|
|
2008-08-07 07:20:50 +08:00
|
|
|
static SourceLocation GetEndLoc(ScopedDecl* D) {
|
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(D))
|
|
|
|
if (Expr* Ex = VD->getInit())
|
|
|
|
return Ex->getSourceRange().getEnd();
|
|
|
|
|
|
|
|
return D->getLocation();
|
|
|
|
}
|
|
|
|
|
2008-08-05 06:51:42 +08:00
|
|
|
/// CFGBuilder - This class implements CFG construction from an AST.
|
2007-08-22 05:42:03 +08:00
|
|
|
/// The builder is stateful: an instance of the builder should be used to only
|
|
|
|
/// construct a single CFG.
|
|
|
|
///
|
|
|
|
/// Example usage:
|
|
|
|
///
|
|
|
|
/// CFGBuilder builder;
|
|
|
|
/// CFG* cfg = builder.BuildAST(stmt1);
|
|
|
|
///
|
2007-08-22 06:06:14 +08:00
|
|
|
/// CFG construction is done via a recursive walk of an AST.
|
|
|
|
/// We actually parse the AST in reverse order so that the successor
|
|
|
|
/// of a basic block is constructed prior to its predecessor. This
|
|
|
|
/// allows us to nicely capture implicit fall-throughs without extra
|
|
|
|
/// basic blocks.
|
|
|
|
///
|
2008-01-09 02:15:10 +08:00
|
|
|
class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
|
2007-08-22 05:42:03 +08:00
|
|
|
CFG* cfg;
|
|
|
|
CFGBlock* Block;
|
|
|
|
CFGBlock* Succ;
|
2007-08-23 05:36:54 +08:00
|
|
|
CFGBlock* ContinueTargetBlock;
|
2007-08-23 05:51:58 +08:00
|
|
|
CFGBlock* BreakTargetBlock;
|
2007-08-24 02:43:24 +08:00
|
|
|
CFGBlock* SwitchTerminatedBlock;
|
2008-02-14 06:05:39 +08:00
|
|
|
CFGBlock* DefaultCaseBlock;
|
2007-08-22 05:42:03 +08:00
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
// LabelMap records the mapping from Label expressions to their blocks.
|
2007-08-22 07:26:17 +08:00
|
|
|
typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
|
|
|
|
LabelMapTy LabelMap;
|
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
// A list of blocks that end with a "goto" that must be backpatched to
|
|
|
|
// their resolved targets upon completion of CFG construction.
|
2007-08-22 23:40:58 +08:00
|
|
|
typedef std::vector<CFGBlock*> BackpatchBlocksTy;
|
2007-08-22 07:26:17 +08:00
|
|
|
BackpatchBlocksTy BackpatchBlocks;
|
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
// A list of labels whose address has been taken (for indirect gotos).
|
|
|
|
typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
|
|
|
|
LabelSetTy AddressTakenLabels;
|
|
|
|
|
2007-08-22 05:42:03 +08:00
|
|
|
public:
|
2007-08-24 00:51:22 +08:00
|
|
|
explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
|
2007-08-23 05:51:58 +08:00
|
|
|
ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
|
2008-02-14 06:05:39 +08:00
|
|
|
SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
|
2007-08-22 05:42:03 +08:00
|
|
|
// Create an empty CFG.
|
|
|
|
cfg = new CFG();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CFGBuilder() { delete cfg; }
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// buildCFG - Used by external clients to construct the CFG.
|
|
|
|
CFG* buildCFG(Stmt* Statement);
|
|
|
|
|
|
|
|
// Visitors to walk an AST and construct the CFG. Called by
|
|
|
|
// buildCFG. Do not call directly!
|
|
|
|
|
|
|
|
CFGBlock* VisitBreakStmt(BreakStmt* B);
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
|
2008-11-12 01:10:00 +08:00
|
|
|
CFGBlock* VisitCompoundStmt(CompoundStmt* C);
|
|
|
|
CFGBlock* VisitContinueStmt(ContinueStmt* C);
|
2008-02-14 05:46:34 +08:00
|
|
|
CFGBlock* VisitDefaultStmt(DefaultStmt* D);
|
2008-11-12 01:10:00 +08:00
|
|
|
CFGBlock* VisitDoStmt(DoStmt* D);
|
|
|
|
CFGBlock* VisitForStmt(ForStmt* F);
|
|
|
|
CFGBlock* VisitGotoStmt(GotoStmt* G);
|
|
|
|
CFGBlock* VisitIfStmt(IfStmt* I);
|
2007-08-29 03:26:49 +08:00
|
|
|
CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
|
2008-11-12 01:10:00 +08:00
|
|
|
CFGBlock* VisitLabelStmt(LabelStmt* L);
|
|
|
|
CFGBlock* VisitNullStmt(NullStmt* Statement);
|
|
|
|
CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
|
|
|
|
CFGBlock* VisitReturnStmt(ReturnStmt* R);
|
|
|
|
CFGBlock* VisitStmt(Stmt* Statement);
|
|
|
|
CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
|
|
|
|
CFGBlock* VisitWhileStmt(WhileStmt* W);
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2008-03-13 11:04:22 +08:00
|
|
|
// FIXME: Add support for ObjC-specific control-flow structures.
|
|
|
|
|
2008-04-29 02:00:46 +08:00
|
|
|
// NYS == Not Yet Supported
|
|
|
|
CFGBlock* NYS() {
|
2008-03-13 11:04:22 +08:00
|
|
|
badCFG = true;
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2008-04-29 02:00:46 +08:00
|
|
|
CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); }
|
|
|
|
CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); }
|
|
|
|
CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); }
|
|
|
|
CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { return NYS(); }
|
|
|
|
|
|
|
|
CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
|
|
|
|
return NYS();
|
2008-03-13 11:04:22 +08:00
|
|
|
}
|
|
|
|
|
2008-09-27 02:17:07 +08:00
|
|
|
// Blocks.
|
|
|
|
CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
|
|
|
|
CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
private:
|
|
|
|
CFGBlock* createBlock(bool add_successor = true);
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* addStmt(Stmt* Terminator);
|
|
|
|
CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
|
|
|
|
CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
|
2008-08-07 07:20:50 +08:00
|
|
|
CFGBlock* WalkAST_VisitDeclSubExpr(ScopedDecl* D);
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
|
2007-08-24 05:42:29 +08:00
|
|
|
void FinishBlock(CFGBlock* B);
|
|
|
|
|
2008-03-13 11:04:22 +08:00
|
|
|
bool badCFG;
|
2007-08-24 05:42:29 +08:00
|
|
|
};
|
2008-09-27 06:58:57 +08:00
|
|
|
|
|
|
|
static VariableArrayType* FindVA(Type* t) {
|
|
|
|
while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
|
|
|
|
if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
|
|
|
|
if (vat->getSizeExpr())
|
|
|
|
return vat;
|
|
|
|
|
|
|
|
t = vt->getElementType().getTypePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-23 02:22:34 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
|
|
|
|
/// represent an arbitrary statement. Examples include a single expression
|
|
|
|
/// or a function body (compound statement). The ownership of the returned
|
|
|
|
/// CFG is transferred to the caller. If CFG construction fails, this method
|
|
|
|
/// returns NULL.
|
|
|
|
CFG* CFGBuilder::buildCFG(Stmt* Statement) {
|
2007-08-29 03:26:49 +08:00
|
|
|
assert (cfg);
|
2007-08-24 05:42:29 +08:00
|
|
|
if (!Statement) return NULL;
|
|
|
|
|
2008-03-13 11:04:22 +08:00
|
|
|
badCFG = false;
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Create an empty block that will serve as the exit block for the CFG.
|
|
|
|
// Since this is the first block added to the CFG, it will be implicitly
|
|
|
|
// registered as the exit block.
|
2007-08-28 03:46:09 +08:00
|
|
|
Succ = createBlock();
|
|
|
|
assert (Succ == &cfg->getExit());
|
|
|
|
Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// Visit the statements and create the CFG.
|
2008-02-28 01:33:02 +08:00
|
|
|
CFGBlock* B = Visit(Statement);
|
|
|
|
if (!B) B = Succ;
|
|
|
|
|
|
|
|
if (B) {
|
2007-08-24 05:42:29 +08:00
|
|
|
// Finalize the last constructed block. This usually involves
|
|
|
|
// reversing the order of the statements in the block.
|
2007-08-28 03:46:09 +08:00
|
|
|
if (Block) FinishBlock(B);
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// Backpatch the gotos whose label -> block mappings we didn't know
|
|
|
|
// when we encountered them.
|
|
|
|
for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
|
|
|
|
E = BackpatchBlocks.end(); I != E; ++I ) {
|
|
|
|
|
|
|
|
CFGBlock* B = *I;
|
|
|
|
GotoStmt* G = cast<GotoStmt>(B->getTerminator());
|
|
|
|
LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
|
|
|
|
|
|
|
|
// If there is no target for the goto, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (LI == LabelMap.end()) continue;
|
2007-08-22 07:26:17 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
B->addSuccessor(LI->second);
|
2007-08-29 03:26:49 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
// Add successors to the Indirect Goto Dispatch block (if we have one).
|
|
|
|
if (CFGBlock* B = cfg->getIndirectGotoBlock())
|
|
|
|
for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
|
|
|
|
E = AddressTakenLabels.end(); I != E; ++I ) {
|
|
|
|
|
|
|
|
// Lookup the target block.
|
|
|
|
LabelMapTy::iterator LI = LabelMap.find(*I);
|
|
|
|
|
|
|
|
// If there is no target block that contains label, then we are looking
|
|
|
|
// at an incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (LI == LabelMap.end()) continue;
|
|
|
|
|
|
|
|
B->addSuccessor(LI->second);
|
|
|
|
}
|
2007-09-27 05:23:31 +08:00
|
|
|
|
2007-09-18 00:18:02 +08:00
|
|
|
Succ = B;
|
|
|
|
}
|
2007-09-27 05:23:31 +08:00
|
|
|
|
|
|
|
// Create an empty entry block that has no predecessors.
|
|
|
|
cfg->setEntry(createBlock());
|
|
|
|
|
2008-03-13 11:04:22 +08:00
|
|
|
if (badCFG) {
|
|
|
|
delete cfg;
|
|
|
|
cfg = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-27 05:23:31 +08:00
|
|
|
// NULL out cfg so that repeated calls to the builder will fail and that
|
|
|
|
// the ownership of the constructed CFG is passed to the caller.
|
|
|
|
CFG* t = cfg;
|
|
|
|
cfg = NULL;
|
|
|
|
return t;
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2007-08-22 06:06:14 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
/// createBlock - Used to lazily create blocks that are connected
|
|
|
|
/// to the current (global) succcessor.
|
|
|
|
CFGBlock* CFGBuilder::createBlock(bool add_successor) {
|
2007-09-06 04:02:05 +08:00
|
|
|
CFGBlock* B = cfg->createBlock();
|
2007-08-24 05:42:29 +08:00
|
|
|
if (add_successor && Succ) B->addSuccessor(Succ);
|
|
|
|
return B;
|
|
|
|
}
|
2007-08-23 02:22:34 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
/// FinishBlock - When the last statement has been added to the block,
|
2007-08-28 03:46:09 +08:00
|
|
|
/// we must reverse the statements because they have been inserted
|
|
|
|
/// in reverse order.
|
2007-08-24 05:42:29 +08:00
|
|
|
void CFGBuilder::FinishBlock(CFGBlock* B) {
|
|
|
|
assert (B);
|
2007-08-28 03:46:09 +08:00
|
|
|
B->reverseStmts();
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
2007-08-28 05:27:44 +08:00
|
|
|
/// addStmt - Used to add statements/expressions to the current CFGBlock
|
|
|
|
/// "Block". This method calls WalkAST on the passed statement to see if it
|
|
|
|
/// contains any short-circuit expressions. If so, it recursively creates
|
|
|
|
/// the necessary blocks for such expressions. It returns the "topmost" block
|
|
|
|
/// of the created blocks, or the original value of "Block" when this method
|
|
|
|
/// was called if no additional blocks are created.
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
|
2007-08-31 02:39:40 +08:00
|
|
|
if (!Block) Block = createBlock();
|
2008-04-17 05:10:48 +08:00
|
|
|
return WalkAST(Terminator,true);
|
2007-08-28 05:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// WalkAST - Used by addStmt to walk the subtree of a statement and
|
2007-08-29 02:14:37 +08:00
|
|
|
/// add extra blocks for ternary operators, &&, and ||. We also
|
|
|
|
/// process "," and DeclStmts (which may contain nested control-flow).
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
|
|
|
|
switch (Terminator->getStmtClass()) {
|
2007-08-28 05:27:44 +08:00
|
|
|
case Stmt::ConditionalOperatorClass: {
|
2008-04-17 05:10:48 +08:00
|
|
|
ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
|
2007-11-27 02:20:26 +08:00
|
|
|
|
|
|
|
// Create the confluence block that will "merge" the results
|
|
|
|
// of the ternary expression.
|
2007-08-28 05:27:44 +08:00
|
|
|
CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
|
|
|
|
ConfluenceBlock->appendStmt(C);
|
2007-12-14 06:44:18 +08:00
|
|
|
FinishBlock(ConfluenceBlock);
|
2007-11-27 02:20:26 +08:00
|
|
|
|
|
|
|
// Create a block for the LHS expression if there is an LHS expression.
|
|
|
|
// A GCC extension allows LHS to be NULL, causing the condition to
|
|
|
|
// be the value that is returned instead.
|
|
|
|
// e.g: x ?: y is shorthand for: x ? x : y;
|
2007-08-28 05:27:44 +08:00
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
2007-11-27 02:20:26 +08:00
|
|
|
CFGBlock* LHSBlock = NULL;
|
|
|
|
if (C->getLHS()) {
|
|
|
|
LHSBlock = Visit(C->getLHS());
|
|
|
|
FinishBlock(LHSBlock);
|
|
|
|
Block = NULL;
|
|
|
|
}
|
2007-08-28 05:27:44 +08:00
|
|
|
|
2007-11-27 02:20:26 +08:00
|
|
|
// Create the block for the RHS expression.
|
2007-08-28 05:27:44 +08:00
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
CFGBlock* RHSBlock = Visit(C->getRHS());
|
2007-09-12 05:29:43 +08:00
|
|
|
FinishBlock(RHSBlock);
|
2007-08-28 05:27:44 +08:00
|
|
|
|
2007-11-27 02:20:26 +08:00
|
|
|
// Create the block that will contain the condition.
|
2007-08-28 05:27:44 +08:00
|
|
|
Block = createBlock(false);
|
2007-11-27 02:20:26 +08:00
|
|
|
|
|
|
|
if (LHSBlock)
|
|
|
|
Block->addSuccessor(LHSBlock);
|
|
|
|
else {
|
|
|
|
// If we have no LHS expression, add the ConfluenceBlock as a direct
|
|
|
|
// successor for the block containing the condition. Moreover,
|
|
|
|
// we need to reverse the order of the predecessors in the
|
|
|
|
// ConfluenceBlock because the RHSBlock will have been added to
|
|
|
|
// the succcessors already, and we want the first predecessor to the
|
|
|
|
// the block containing the expression for the case when the ternary
|
|
|
|
// expression evaluates to true.
|
|
|
|
Block->addSuccessor(ConfluenceBlock);
|
|
|
|
assert (ConfluenceBlock->pred_size() == 2);
|
|
|
|
std::reverse(ConfluenceBlock->pred_begin(),
|
|
|
|
ConfluenceBlock->pred_end());
|
|
|
|
}
|
|
|
|
|
2007-09-01 01:03:41 +08:00
|
|
|
Block->addSuccessor(RHSBlock);
|
2007-11-27 02:20:26 +08:00
|
|
|
|
2007-09-01 01:03:41 +08:00
|
|
|
Block->setTerminator(C);
|
|
|
|
return addStmt(C->getCond());
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stmt::ChooseExprClass: {
|
2008-04-17 05:10:48 +08:00
|
|
|
ChooseExpr* C = cast<ChooseExpr>(Terminator);
|
2007-09-01 01:03:41 +08:00
|
|
|
|
|
|
|
CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
|
|
|
|
ConfluenceBlock->appendStmt(C);
|
|
|
|
FinishBlock(ConfluenceBlock);
|
|
|
|
|
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock* LHSBlock = Visit(C->getLHS());
|
2007-09-12 05:29:43 +08:00
|
|
|
FinishBlock(LHSBlock);
|
|
|
|
|
2007-09-01 01:03:41 +08:00
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock* RHSBlock = Visit(C->getRHS());
|
2007-09-12 05:29:43 +08:00
|
|
|
FinishBlock(RHSBlock);
|
2007-09-01 01:03:41 +08:00
|
|
|
|
|
|
|
Block = createBlock(false);
|
2007-08-28 05:27:44 +08:00
|
|
|
Block->addSuccessor(LHSBlock);
|
|
|
|
Block->addSuccessor(RHSBlock);
|
|
|
|
Block->setTerminator(C);
|
|
|
|
return addStmt(C->getCond());
|
|
|
|
}
|
2007-08-29 00:18:58 +08:00
|
|
|
|
2007-10-31 05:48:34 +08:00
|
|
|
case Stmt::DeclStmtClass: {
|
2008-10-07 04:56:19 +08:00
|
|
|
DeclStmt *DS = cast<DeclStmt>(Terminator);
|
|
|
|
if (DS->hasSolitaryDecl()) {
|
2008-08-07 07:20:50 +08:00
|
|
|
Block->appendStmt(Terminator);
|
2008-10-07 04:56:19 +08:00
|
|
|
return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
|
2008-08-07 07:20:50 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
|
|
|
|
BufTy Buf;
|
|
|
|
CFGBlock* B = 0;
|
2008-10-07 04:56:19 +08:00
|
|
|
|
|
|
|
// FIXME: Add a reverse iterator for DeclStmt to avoid this
|
|
|
|
// extra copy.
|
|
|
|
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
|
|
|
|
DI != DE; ++DI)
|
|
|
|
Buf.push_back(*DI);
|
|
|
|
|
2008-08-07 07:20:50 +08:00
|
|
|
for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
|
2008-10-08 07:09:49 +08:00
|
|
|
// Get the alignment of the new DeclStmt, padding out to >=8 bytes.
|
|
|
|
unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
|
|
|
|
? 8 : llvm::AlignOf<DeclStmt>::Alignment;
|
|
|
|
|
|
|
|
// Allocate the DeclStmt using the BumpPtrAllocator. It will
|
|
|
|
// get automatically freed with the CFG. Note that even though
|
|
|
|
// we are using a DeclGroupOwningRef that wraps a singe Decl*,
|
|
|
|
// that Decl* will not get deallocated because the destroy method
|
|
|
|
// of DG is never called.
|
|
|
|
DeclGroupOwningRef DG(*I);
|
|
|
|
ScopedDecl* D = *I;
|
|
|
|
void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
|
|
|
|
|
|
|
|
DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
|
|
|
|
GetEndLoc(D));
|
2008-08-07 07:20:50 +08:00
|
|
|
|
|
|
|
// Append the fake DeclStmt to block.
|
2008-10-08 07:09:49 +08:00
|
|
|
Block->appendStmt(DS);
|
|
|
|
B = WalkAST_VisitDeclSubExpr(D);
|
2008-08-07 07:20:50 +08:00
|
|
|
}
|
|
|
|
return B;
|
|
|
|
}
|
2007-10-31 05:48:34 +08:00
|
|
|
}
|
2008-08-07 07:20:50 +08:00
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
case Stmt::AddrLabelExprClass: {
|
2008-04-17 05:10:48 +08:00
|
|
|
AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
|
2007-08-29 03:26:49 +08:00
|
|
|
AddressTakenLabels.insert(A->getLabel());
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
if (AlwaysAddStmt) Block->appendStmt(Terminator);
|
2007-08-29 03:26:49 +08:00
|
|
|
return Block;
|
|
|
|
}
|
2007-09-12 05:29:43 +08:00
|
|
|
|
2007-08-29 02:30:10 +08:00
|
|
|
case Stmt::StmtExprClass:
|
2008-04-17 05:10:48 +08:00
|
|
|
return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
|
2007-08-29 02:14:37 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
case Stmt::SizeOfAlignOfExprClass: {
|
|
|
|
SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
|
2008-09-27 06:58:57 +08:00
|
|
|
|
|
|
|
// VLA types have expressions that must be evaluated.
|
2008-11-12 01:56:53 +08:00
|
|
|
if (E->isArgumentType()) {
|
|
|
|
for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
|
|
|
|
VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
|
|
|
|
addStmt(VA->getSizeExpr());
|
|
|
|
}
|
|
|
|
// Expressions in sizeof/alignof are not evaluated and thus have no
|
|
|
|
// control flow.
|
|
|
|
else
|
|
|
|
Block->appendStmt(Terminator);
|
2008-09-27 06:58:57 +08:00
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2007-08-28 05:54:41 +08:00
|
|
|
case Stmt::BinaryOperatorClass: {
|
2008-04-17 05:10:48 +08:00
|
|
|
BinaryOperator* B = cast<BinaryOperator>(Terminator);
|
2007-08-28 05:54:41 +08:00
|
|
|
|
|
|
|
if (B->isLogicalOp()) { // && or ||
|
|
|
|
CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
|
|
|
|
ConfluenceBlock->appendStmt(B);
|
|
|
|
FinishBlock(ConfluenceBlock);
|
|
|
|
|
|
|
|
// create the block evaluating the LHS
|
|
|
|
CFGBlock* LHSBlock = createBlock(false);
|
2007-12-22 03:49:00 +08:00
|
|
|
LHSBlock->setTerminator(B);
|
2007-08-28 05:54:41 +08:00
|
|
|
|
|
|
|
// create the block evaluating the RHS
|
|
|
|
Succ = ConfluenceBlock;
|
|
|
|
Block = NULL;
|
|
|
|
CFGBlock* RHSBlock = Visit(B->getRHS());
|
2008-10-04 13:48:38 +08:00
|
|
|
FinishBlock(RHSBlock);
|
2007-12-22 03:49:00 +08:00
|
|
|
|
|
|
|
// Now link the LHSBlock with RHSBlock.
|
|
|
|
if (B->getOpcode() == BinaryOperator::LOr) {
|
|
|
|
LHSBlock->addSuccessor(ConfluenceBlock);
|
|
|
|
LHSBlock->addSuccessor(RHSBlock);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (B->getOpcode() == BinaryOperator::LAnd);
|
|
|
|
LHSBlock->addSuccessor(RHSBlock);
|
|
|
|
LHSBlock->addSuccessor(ConfluenceBlock);
|
|
|
|
}
|
2007-08-28 05:54:41 +08:00
|
|
|
|
|
|
|
// Generate the blocks for evaluating the LHS.
|
|
|
|
Block = LHSBlock;
|
|
|
|
return addStmt(B->getLHS());
|
2007-08-29 02:14:37 +08:00
|
|
|
}
|
|
|
|
else if (B->getOpcode() == BinaryOperator::Comma) { // ,
|
|
|
|
Block->appendStmt(B);
|
|
|
|
addStmt(B->getRHS());
|
|
|
|
return addStmt(B->getLHS());
|
2007-10-02 03:33:33 +08:00
|
|
|
}
|
2007-12-14 06:44:18 +08:00
|
|
|
|
|
|
|
break;
|
2007-08-28 05:54:41 +08:00
|
|
|
}
|
2008-09-27 02:17:07 +08:00
|
|
|
|
|
|
|
// Blocks: No support for blocks ... yet
|
|
|
|
case Stmt::BlockExprClass:
|
|
|
|
case Stmt::BlockDeclRefExprClass:
|
|
|
|
return NYS();
|
2008-02-26 10:37:08 +08:00
|
|
|
|
|
|
|
case Stmt::ParenExprClass:
|
2008-04-17 05:10:48 +08:00
|
|
|
return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
|
2007-08-28 05:54:41 +08:00
|
|
|
|
2007-08-28 05:27:44 +08:00
|
|
|
default:
|
2007-12-14 06:44:18 +08:00
|
|
|
break;
|
2007-08-28 05:27:44 +08:00
|
|
|
};
|
2007-12-14 06:44:18 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
if (AlwaysAddStmt) Block->appendStmt(Terminator);
|
|
|
|
return WalkAST_VisitChildren(Terminator);
|
2007-08-28 05:27:44 +08:00
|
|
|
}
|
2008-09-27 00:26:36 +08:00
|
|
|
|
2008-08-07 07:20:50 +08:00
|
|
|
/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
|
|
|
|
/// for initializers in Decls.
|
|
|
|
CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
|
|
|
|
VarDecl* VD = dyn_cast<VarDecl>(D);
|
|
|
|
|
|
|
|
if (!VD)
|
2007-11-19 04:06:01 +08:00
|
|
|
return Block;
|
|
|
|
|
2008-08-07 07:20:50 +08:00
|
|
|
Expr* Init = VD->getInit();
|
2008-03-01 06:32:24 +08:00
|
|
|
|
2008-09-27 00:26:36 +08:00
|
|
|
if (Init) {
|
|
|
|
// Optimization: Don't create separate block-level statements for literals.
|
|
|
|
switch (Init->getStmtClass()) {
|
|
|
|
case Stmt::IntegerLiteralClass:
|
|
|
|
case Stmt::CharacterLiteralClass:
|
|
|
|
case Stmt::StringLiteralClass:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Block = addStmt(Init);
|
|
|
|
}
|
2008-03-01 06:32:24 +08:00
|
|
|
}
|
2008-09-27 00:26:36 +08:00
|
|
|
|
|
|
|
// If the type of VD is a VLA, then we must process its size expressions.
|
|
|
|
for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
|
|
|
|
VA = FindVA(VA->getElementType().getTypePtr()))
|
|
|
|
Block = addStmt(VA->getSizeExpr());
|
2007-10-31 05:48:34 +08:00
|
|
|
|
2007-08-29 02:14:37 +08:00
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2007-08-28 05:27:44 +08:00
|
|
|
/// WalkAST_VisitChildren - Utility method to call WalkAST on the
|
|
|
|
/// children of a Stmt.
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
|
2007-08-28 05:27:44 +08:00
|
|
|
CFGBlock* B = Block;
|
2008-04-17 05:10:48 +08:00
|
|
|
for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
|
2007-08-28 05:27:44 +08:00
|
|
|
I != E; ++I)
|
2007-09-27 05:23:31 +08:00
|
|
|
if (*I) B = WalkAST(*I);
|
2007-08-28 05:27:44 +08:00
|
|
|
|
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2007-08-29 02:30:10 +08:00
|
|
|
/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
|
|
|
|
/// expressions (a GCC extension).
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
|
|
|
|
Block->appendStmt(Terminator);
|
|
|
|
return VisitCompoundStmt(Terminator->getSubStmt());
|
2007-08-29 02:30:10 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
/// VisitStmt - Handle statements with no branching control flow.
|
|
|
|
CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
|
|
|
|
// We cannot assume that we are in the middle of a basic block, since
|
|
|
|
// the CFG might only be constructed for this single statement. If
|
|
|
|
// we have no current basic block, just create one lazily.
|
|
|
|
if (!Block) Block = createBlock();
|
2007-08-22 06:06:14 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Simply add the statement to the current block. We actually
|
|
|
|
// insert statements in reverse order; this order is reversed later
|
|
|
|
// when processing the containing element in the AST.
|
2007-08-28 03:46:09 +08:00
|
|
|
addStmt(Statement);
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
|
2008-03-18 01:19:44 +08:00
|
|
|
|
|
|
|
CFGBlock* LastBlock = NULL;
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2008-02-26 08:22:58 +08:00
|
|
|
for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
|
|
|
|
I != E; ++I ) {
|
2008-03-18 01:19:44 +08:00
|
|
|
LastBlock = Visit(*I);
|
2008-02-26 08:22:58 +08:00
|
|
|
}
|
|
|
|
|
2008-03-18 01:19:44 +08:00
|
|
|
return LastBlock;
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2007-08-22 05:42:03 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
|
|
|
|
// We may see an if statement in the middle of a basic block, or
|
|
|
|
// it may be the first statement we are processing. In either case,
|
|
|
|
// we create a new basic block. First, we create the blocks for
|
|
|
|
// the then...else statements, and then we create the block containing
|
|
|
|
// the if statement. If we were in the middle of a block, we
|
|
|
|
// stop processing that block and reverse its statements. That block
|
|
|
|
// is then the implicit successor for the "then" and "else" clauses.
|
|
|
|
|
|
|
|
// The block we were proccessing is now finished. Make it the
|
|
|
|
// successor block.
|
|
|
|
if (Block) {
|
|
|
|
Succ = Block;
|
|
|
|
FinishBlock(Block);
|
2007-08-22 06:06:14 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Process the false branch. NULL out Block so that the recursive
|
|
|
|
// call to Visit will create a new basic block.
|
|
|
|
// Null out Block so that all successor
|
|
|
|
CFGBlock* ElseBlock = Succ;
|
2007-08-22 06:06:14 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
if (Stmt* Else = I->getElse()) {
|
|
|
|
SaveAndRestore<CFGBlock*> sv(Succ);
|
2007-08-22 06:06:14 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// NULL out Block so that the recursive call to Visit will
|
|
|
|
// create a new basic block.
|
|
|
|
Block = NULL;
|
2007-08-31 02:13:31 +08:00
|
|
|
ElseBlock = Visit(Else);
|
|
|
|
|
|
|
|
if (!ElseBlock) // Can occur when the Else body has all NullStmts.
|
|
|
|
ElseBlock = sv.get();
|
|
|
|
else if (Block)
|
|
|
|
FinishBlock(ElseBlock);
|
2007-08-22 06:06:14 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// Process the true branch. NULL out Block so that the recursive
|
|
|
|
// call to Visit will create a new basic block.
|
|
|
|
// Null out Block so that all successor
|
|
|
|
CFGBlock* ThenBlock;
|
|
|
|
{
|
|
|
|
Stmt* Then = I->getThen();
|
|
|
|
assert (Then);
|
|
|
|
SaveAndRestore<CFGBlock*> sv(Succ);
|
|
|
|
Block = NULL;
|
2007-08-31 02:13:31 +08:00
|
|
|
ThenBlock = Visit(Then);
|
|
|
|
|
|
|
|
if (!ThenBlock) // Can occur when the Then body has all NullStmts.
|
|
|
|
ThenBlock = sv.get();
|
|
|
|
else if (Block)
|
|
|
|
FinishBlock(ThenBlock);
|
2007-08-22 07:26:17 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// Now create a new block containing the if statement.
|
|
|
|
Block = createBlock(false);
|
2007-08-22 07:26:17 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Set the terminator of the new block to the If statement.
|
|
|
|
Block->setTerminator(I);
|
|
|
|
|
|
|
|
// Now add the successors.
|
|
|
|
Block->addSuccessor(ThenBlock);
|
|
|
|
Block->addSuccessor(ElseBlock);
|
2007-08-28 03:46:09 +08:00
|
|
|
|
|
|
|
// Add the condition as the last statement in the new block. This
|
|
|
|
// may create new blocks as the condition may contain control-flow. Any
|
|
|
|
// newly created blocks will be pointed to be "Block".
|
2008-01-31 07:02:42 +08:00
|
|
|
return addStmt(I->getCond()->IgnoreParens());
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2007-09-12 05:29:43 +08:00
|
|
|
|
2007-08-22 07:26:17 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
|
|
|
|
// If we were in the middle of a block we stop processing that block
|
|
|
|
// and reverse its statements.
|
|
|
|
//
|
|
|
|
// NOTE: If a "return" appears in the middle of a block, this means
|
|
|
|
// that the code afterwards is DEAD (unreachable). We still
|
|
|
|
// keep a basic block for that code; a simple "mark-and-sweep"
|
|
|
|
// from the entry block will be able to report such dead
|
|
|
|
// blocks.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Create the new block.
|
|
|
|
Block = createBlock(false);
|
2007-08-22 07:26:17 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// The Exit block is the only successor.
|
|
|
|
Block->addSuccessor(&cfg->getExit());
|
2007-08-28 03:46:09 +08:00
|
|
|
|
|
|
|
// Add the return statement to the block. This may create new blocks
|
|
|
|
// if R contains control-flow (short-circuit operations).
|
|
|
|
return addStmt(R);
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2007-08-22 07:26:17 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
|
|
|
|
// Get the block of the labeled statement. Add it to our map.
|
2008-03-15 15:45:02 +08:00
|
|
|
Visit(L->getSubStmt());
|
|
|
|
CFGBlock* LabelBlock = Block;
|
2007-08-31 02:20:57 +08:00
|
|
|
|
|
|
|
if (!LabelBlock) // This can happen when the body is empty, i.e.
|
|
|
|
LabelBlock=createBlock(); // scopes that only contains NullStmts.
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
|
|
|
|
LabelMap[ L ] = LabelBlock;
|
2007-08-23 02:22:34 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Labels partition blocks, so this is the end of the basic block
|
2007-08-30 07:20:49 +08:00
|
|
|
// we were processing (L is the block's label). Because this is
|
2007-08-28 03:46:09 +08:00
|
|
|
// label (and we have already processed the substatement) there is no
|
|
|
|
// extra control-flow to worry about.
|
2007-08-30 07:20:49 +08:00
|
|
|
LabelBlock->setLabel(L);
|
2007-08-24 05:42:29 +08:00
|
|
|
FinishBlock(LabelBlock);
|
|
|
|
|
|
|
|
// We set Block to NULL to allow lazy creation of a new block
|
|
|
|
// (if necessary);
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// This block is now the implicit successor of other blocks.
|
|
|
|
Succ = LabelBlock;
|
|
|
|
|
|
|
|
return LabelBlock;
|
|
|
|
}
|
2007-08-23 06:35:28 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
|
|
|
|
// Goto is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block and create a new one.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(G);
|
|
|
|
|
|
|
|
// If we already know the mapping to the label block add the
|
|
|
|
// successor now.
|
|
|
|
LabelMapTy::iterator I = LabelMap.find(G->getLabel());
|
|
|
|
|
|
|
|
if (I == LabelMap.end())
|
|
|
|
// We will need to backpatch this block later.
|
|
|
|
BackpatchBlocks.push_back(Block);
|
|
|
|
else
|
|
|
|
Block->addSuccessor(I->second);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
|
|
|
|
// "for" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
|
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
2007-08-23 02:22:34 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
else LoopSuccessor = Succ;
|
|
|
|
|
2007-08-28 03:46:09 +08:00
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(F);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
if (Stmt* C = F->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// The condition block is the implicit successor for the loop body as
|
|
|
|
// well as any code above the loop.
|
2007-08-28 03:46:09 +08:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-23 05:05:42 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Now create the loop body.
|
|
|
|
{
|
|
|
|
assert (F->getBody());
|
2007-08-23 06:35:28 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
2008-09-05 05:48:47 +08:00
|
|
|
|
2007-08-31 02:39:40 +08:00
|
|
|
// Create a new block to contain the (bottom) of the loop body.
|
|
|
|
Block = NULL;
|
2007-08-23 05:36:54 +08:00
|
|
|
|
2008-09-05 05:48:47 +08:00
|
|
|
if (Stmt* I = F->getInc()) {
|
|
|
|
// Generate increment code in its own basic block. This is the target
|
|
|
|
// of continue statements.
|
|
|
|
Succ = addStmt(I);
|
|
|
|
Block = 0;
|
|
|
|
ContinueTargetBlock = Succ;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No increment code. Continues should go the the entry condition block.
|
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-23 05:05:42 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Now populate the body block, and in the process create new blocks
|
|
|
|
// as we walk the body of the loop.
|
|
|
|
CFGBlock* BodyBlock = Visit(F->getBody());
|
2007-08-31 02:39:40 +08:00
|
|
|
|
|
|
|
if (!BodyBlock)
|
2008-02-27 08:28:17 +08:00
|
|
|
BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
|
2007-08-31 02:39:40 +08:00
|
|
|
else if (Block)
|
|
|
|
FinishBlock(BodyBlock);
|
2007-08-23 05:05:42 +08:00
|
|
|
|
2007-08-28 03:46:09 +08:00
|
|
|
// This new body block is a successor to our "exit" condition block.
|
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-28 03:46:09 +08:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// If the loop contains initialization, create a new block for those
|
|
|
|
// statements. This block can also contain statements that precede
|
|
|
|
// the loop.
|
|
|
|
if (Stmt* I = F->getInit()) {
|
|
|
|
Block = createBlock();
|
2007-08-28 03:46:09 +08:00
|
|
|
return addStmt(I);
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There is no loop initialization. We are thus basically a while
|
|
|
|
// loop. NULL out Block to force lazy block construction.
|
2007-08-23 05:05:42 +08:00
|
|
|
Block = NULL;
|
2008-02-27 15:20:00 +08:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-28 03:46:09 +08:00
|
|
|
return EntryConditionBlock;
|
2007-08-23 05:05:42 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 01:10:00 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
|
|
|
|
// Objective-C fast enumeration 'for' statements:
|
|
|
|
// http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
|
|
|
|
//
|
|
|
|
// for ( Type newVariable in collection_expression ) { statements }
|
|
|
|
//
|
|
|
|
// becomes:
|
|
|
|
//
|
|
|
|
// prologue:
|
|
|
|
// 1. collection_expression
|
|
|
|
// T. jump to loop_entry
|
|
|
|
// loop_entry:
|
|
|
|
// 1. ObjCForCollectionStmt [performs binding to newVariable]
|
|
|
|
// T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
|
|
|
|
// TB:
|
|
|
|
// statements
|
|
|
|
// T. jump to loop_entry
|
|
|
|
// FB:
|
|
|
|
// what comes after
|
|
|
|
//
|
|
|
|
// and
|
|
|
|
//
|
|
|
|
// Type existingItem;
|
|
|
|
// for ( existingItem in expression ) { statements }
|
|
|
|
//
|
|
|
|
// becomes:
|
|
|
|
//
|
|
|
|
// the same with newVariable replaced with existingItem; the binding
|
|
|
|
// works the same except that for one ObjCForCollectionStmt::getElement()
|
|
|
|
// returns a DeclStmt and the other returns a DeclRefExpr.
|
|
|
|
//
|
|
|
|
|
|
|
|
CFGBlock* LoopSuccessor = 0;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
|
|
|
Block = 0;
|
|
|
|
}
|
|
|
|
else LoopSuccessor = Succ;
|
|
|
|
|
|
|
|
// Build the condition block. The condition has no short-circuit evaluation,
|
|
|
|
// so we don't need multiple blocks like other control-flow structures with
|
|
|
|
// conditions.
|
|
|
|
CFGBlock* ConditionBlock = createBlock(false);
|
|
|
|
ConditionBlock->appendStmt(S);
|
|
|
|
ConditionBlock->setTerminator(S); // No need to call FinishBlock; 1 stmt
|
|
|
|
|
|
|
|
// Now create the true branch.
|
2008-11-13 14:36:45 +08:00
|
|
|
// Save the current values for the continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
|
|
|
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
|
|
|
ContinueTargetBlock = ConditionBlock;
|
2008-11-12 01:10:00 +08:00
|
|
|
Succ = ConditionBlock;
|
2008-11-13 14:36:45 +08:00
|
|
|
|
2008-11-13 08:06:59 +08:00
|
|
|
CFGBlock* BodyBlock = Visit(S->getBody());
|
2008-11-12 01:10:00 +08:00
|
|
|
FinishBlock(BodyBlock);
|
|
|
|
|
|
|
|
// Connect up the condition block
|
2008-11-13 15:01:10 +08:00
|
|
|
ConditionBlock->addSuccessor(BodyBlock);
|
2008-11-12 01:10:00 +08:00
|
|
|
ConditionBlock->addSuccessor(LoopSuccessor);
|
|
|
|
|
|
|
|
// Now create a prologue block to contain the collection expression.
|
|
|
|
Block = 0;
|
|
|
|
Succ = ConditionBlock;
|
|
|
|
return addStmt(S->getCollection());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
|
|
|
|
// "while" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
2007-08-23 05:05:42 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
|
|
|
}
|
|
|
|
else LoopSuccessor = Succ;
|
2007-08-28 03:46:09 +08:00
|
|
|
|
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(W);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
// Thus we update "Succ" after adding the condition.
|
|
|
|
if (Stmt* C = W->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
2008-02-27 08:28:17 +08:00
|
|
|
assert (Block == EntryConditionBlock);
|
2007-08-28 03:46:09 +08:00
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// The condition block is the implicit successor for the loop body as
|
|
|
|
// well as any code above the loop.
|
2007-08-28 03:46:09 +08:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// Process the loop body.
|
|
|
|
{
|
|
|
|
assert (W->getBody());
|
|
|
|
|
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
|
|
|
|
|
|
|
// All continues within this loop should go to the condition block
|
2007-08-28 03:46:09 +08:00
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
2007-08-24 01:15:32 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-24 01:15:32 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// NULL out Block to force lazy instantiation of blocks for the body.
|
2007-08-24 01:15:32 +08:00
|
|
|
Block = NULL;
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Create the body. The returned block is the entry to the loop body.
|
|
|
|
CFGBlock* BodyBlock = Visit(W->getBody());
|
2007-08-31 02:39:40 +08:00
|
|
|
|
|
|
|
if (!BodyBlock)
|
2008-02-27 08:28:17 +08:00
|
|
|
BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
|
2007-08-31 02:39:40 +08:00
|
|
|
else if (Block)
|
|
|
|
FinishBlock(BodyBlock);
|
2007-08-23 05:36:54 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Add the loop body entry as a successor to the condition.
|
2007-08-28 03:46:09 +08:00
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-23 05:36:54 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-28 03:46:09 +08:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// There can be no more statements in the condition block
|
|
|
|
// since we loop back to this block. NULL out Block to force
|
|
|
|
// lazy creation of another block.
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// Return the condition block, which is the dominating block for the loop.
|
2008-02-27 15:20:00 +08:00
|
|
|
Succ = EntryConditionBlock;
|
2007-08-28 03:46:09 +08:00
|
|
|
return EntryConditionBlock;
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2007-08-24 01:29:58 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
|
|
|
|
// "do...while" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
|
|
|
|
CFGBlock* LoopSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
LoopSuccessor = Block;
|
2007-08-23 05:51:58 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
else LoopSuccessor = Succ;
|
2007-08-23 05:51:58 +08:00
|
|
|
|
2007-08-28 03:46:09 +08:00
|
|
|
// Because of short-circuit evaluation, the condition of the loop
|
|
|
|
// can span multiple basic blocks. Thus we need the "Entry" and "Exit"
|
|
|
|
// blocks that evaluate the condition.
|
|
|
|
CFGBlock* ExitConditionBlock = createBlock(false);
|
|
|
|
CFGBlock* EntryConditionBlock = ExitConditionBlock;
|
|
|
|
|
|
|
|
// Set the terminator for the "exit" condition block.
|
|
|
|
ExitConditionBlock->setTerminator(D);
|
|
|
|
|
|
|
|
// Now add the actual condition to the condition block. Because the
|
|
|
|
// condition itself may contain control-flow, new blocks may be created.
|
|
|
|
if (Stmt* C = D->getCond()) {
|
|
|
|
Block = ExitConditionBlock;
|
|
|
|
EntryConditionBlock = addStmt(C);
|
|
|
|
if (Block) FinishBlock(EntryConditionBlock);
|
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2008-02-27 15:20:00 +08:00
|
|
|
// The condition block is the implicit successor for the loop body.
|
2007-08-28 03:46:09 +08:00
|
|
|
Succ = EntryConditionBlock;
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Process the loop body.
|
2007-08-28 03:46:09 +08:00
|
|
|
CFGBlock* BodyBlock = NULL;
|
2007-08-24 05:42:29 +08:00
|
|
|
{
|
|
|
|
assert (D->getBody());
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Save the current values for Block, Succ, and continue and break targets
|
|
|
|
SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
|
|
|
|
save_continue(ContinueTargetBlock),
|
|
|
|
save_break(BreakTargetBlock);
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// All continues within this loop should go to the condition block
|
2007-08-28 03:46:09 +08:00
|
|
|
ContinueTargetBlock = EntryConditionBlock;
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// All breaks should go to the code following the loop.
|
|
|
|
BreakTargetBlock = LoopSuccessor;
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// NULL out Block to force lazy instantiation of blocks for the body.
|
2007-08-24 02:43:24 +08:00
|
|
|
Block = NULL;
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Create the body. The returned block is the entry to the loop body.
|
|
|
|
BodyBlock = Visit(D->getBody());
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-31 02:39:40 +08:00
|
|
|
if (!BodyBlock)
|
2008-02-27 08:28:17 +08:00
|
|
|
BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
|
2007-08-31 02:39:40 +08:00
|
|
|
else if (Block)
|
|
|
|
FinishBlock(BodyBlock);
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Add the loop body entry as a successor to the condition.
|
2007-08-28 03:46:09 +08:00
|
|
|
ExitConditionBlock->addSuccessor(BodyBlock);
|
2007-08-24 02:43:24 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Link up the condition block with the code that follows the loop.
|
|
|
|
// (the false branch).
|
2007-08-28 03:46:09 +08:00
|
|
|
ExitConditionBlock->addSuccessor(LoopSuccessor);
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2007-08-28 03:46:09 +08:00
|
|
|
// There can be no more statements in the body block(s)
|
|
|
|
// since we loop back to the body. NULL out Block to force
|
2007-08-24 05:42:29 +08:00
|
|
|
// lazy creation of another block.
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// Return the loop body, which is the dominating block for the loop.
|
2008-02-27 15:20:00 +08:00
|
|
|
Succ = BodyBlock;
|
2007-08-24 05:42:29 +08:00
|
|
|
return BodyBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
|
|
|
|
// "continue" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Now create a new block that ends with the continue statement.
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(C);
|
|
|
|
|
|
|
|
// If there is no target for the continue, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
|
|
|
|
// "break" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
|
|
|
|
// Now create a new block that ends with the continue statement.
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(B);
|
|
|
|
|
|
|
|
// If there is no target for the break, then we are looking at an
|
|
|
|
// incomplete AST. Handle this by not registering a successor.
|
|
|
|
if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
|
|
|
|
|
|
|
|
return Block;
|
|
|
|
}
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
|
2007-08-24 05:42:29 +08:00
|
|
|
// "switch" is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block.
|
|
|
|
CFGBlock* SwitchSuccessor = NULL;
|
|
|
|
|
|
|
|
if (Block) {
|
|
|
|
FinishBlock(Block);
|
|
|
|
SwitchSuccessor = Block;
|
2007-08-24 02:43:24 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
else SwitchSuccessor = Succ;
|
|
|
|
|
|
|
|
// Save the current "switch" context.
|
|
|
|
SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
|
2008-02-14 06:05:39 +08:00
|
|
|
save_break(BreakTargetBlock),
|
|
|
|
save_default(DefaultCaseBlock);
|
|
|
|
|
|
|
|
// Set the "default" case to be the block after the switch statement.
|
|
|
|
// If the switch statement contains a "default:", this value will
|
|
|
|
// be overwritten with the block for that code.
|
|
|
|
DefaultCaseBlock = SwitchSuccessor;
|
2008-02-14 05:46:34 +08:00
|
|
|
|
2007-08-24 05:42:29 +08:00
|
|
|
// Create a new block that will contain the switch statement.
|
|
|
|
SwitchTerminatedBlock = createBlock(false);
|
|
|
|
|
|
|
|
// Now process the switch body. The code after the switch is the implicit
|
|
|
|
// successor.
|
|
|
|
Succ = SwitchSuccessor;
|
|
|
|
BreakTargetBlock = SwitchSuccessor;
|
|
|
|
|
|
|
|
// When visiting the body, the case statements should automatically get
|
|
|
|
// linked up to the switch. We also don't keep a pointer to the body,
|
|
|
|
// since all control-flow from the switch goes to case/default statements.
|
2008-04-17 05:10:48 +08:00
|
|
|
assert (Terminator->getBody() && "switch must contain a non-NULL body");
|
2007-08-28 03:46:09 +08:00
|
|
|
Block = NULL;
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock *BodyBlock = Visit(Terminator->getBody());
|
2007-08-28 03:46:09 +08:00
|
|
|
if (Block) FinishBlock(BodyBlock);
|
|
|
|
|
2008-02-14 05:46:34 +08:00
|
|
|
// If we have no "default:" case, the default transition is to the
|
|
|
|
// code following the switch body.
|
2008-02-14 06:05:39 +08:00
|
|
|
SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
|
2008-02-14 05:46:34 +08:00
|
|
|
|
2007-08-28 03:46:09 +08:00
|
|
|
// Add the terminator and condition in the switch block.
|
2008-04-17 05:10:48 +08:00
|
|
|
SwitchTerminatedBlock->setTerminator(Terminator);
|
|
|
|
assert (Terminator->getCond() && "switch condition must be non-NULL");
|
2007-08-24 05:42:29 +08:00
|
|
|
Block = SwitchTerminatedBlock;
|
2008-02-14 05:46:34 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
return addStmt(Terminator->getCond());
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
|
2008-02-14 06:05:39 +08:00
|
|
|
// CaseStmts are essentially labels, so they are the
|
2007-08-24 05:42:29 +08:00
|
|
|
// first statement in a block.
|
2007-08-31 02:48:11 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
|
2007-08-31 02:48:11 +08:00
|
|
|
CFGBlock* CaseBlock = Block;
|
|
|
|
if (!CaseBlock) CaseBlock = createBlock();
|
|
|
|
|
2008-02-14 06:05:39 +08:00
|
|
|
// Cases statements partition blocks, so this is the top of
|
|
|
|
// the basic block we were processing (the "case XXX:" is the label).
|
2008-04-17 05:10:48 +08:00
|
|
|
CaseBlock->setLabel(Terminator);
|
2007-08-24 05:42:29 +08:00
|
|
|
FinishBlock(CaseBlock);
|
|
|
|
|
|
|
|
// Add this block to the list of successors for the block with the
|
|
|
|
// switch statement.
|
2008-02-14 06:05:39 +08:00
|
|
|
assert (SwitchTerminatedBlock);
|
|
|
|
SwitchTerminatedBlock->addSuccessor(CaseBlock);
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
// We set Block to NULL to allow lazy creation of a new block (if necessary)
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// This block is now the implicit successor of other blocks.
|
|
|
|
Succ = CaseBlock;
|
|
|
|
|
2008-03-15 15:45:02 +08:00
|
|
|
return CaseBlock;
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
2008-02-14 05:46:34 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
|
|
|
|
if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
|
2008-02-14 06:05:39 +08:00
|
|
|
DefaultCaseBlock = Block;
|
|
|
|
if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
|
|
|
|
|
|
|
|
// Default statements partition blocks, so this is the top of
|
|
|
|
// the basic block we were processing (the "default:" is the label).
|
2008-04-17 05:10:48 +08:00
|
|
|
DefaultCaseBlock->setLabel(Terminator);
|
2008-02-14 06:05:39 +08:00
|
|
|
FinishBlock(DefaultCaseBlock);
|
|
|
|
|
|
|
|
// Unlike case statements, we don't add the default block to the
|
|
|
|
// successors for the switch statement immediately. This is done
|
|
|
|
// when we finish processing the switch statement. This allows for
|
|
|
|
// the default case (including a fall-through to the code after the
|
|
|
|
// switch statement) to always be the last successor of a switch-terminated
|
|
|
|
// block.
|
|
|
|
|
|
|
|
// We set Block to NULL to allow lazy creation of a new block (if necessary)
|
|
|
|
Block = NULL;
|
|
|
|
|
|
|
|
// This block is now the implicit successor of other blocks.
|
|
|
|
Succ = DefaultCaseBlock;
|
|
|
|
|
|
|
|
return DefaultCaseBlock;
|
2008-02-14 05:46:34 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
2007-08-29 03:26:49 +08:00
|
|
|
CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
|
|
|
|
// Lazily create the indirect-goto dispatch block if there isn't one
|
|
|
|
// already.
|
|
|
|
CFGBlock* IBlock = cfg->getIndirectGotoBlock();
|
|
|
|
|
|
|
|
if (!IBlock) {
|
|
|
|
IBlock = createBlock(false);
|
|
|
|
cfg->setIndirectGotoBlock(IBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndirectGoto is a control-flow statement. Thus we stop processing the
|
|
|
|
// current block and create a new one.
|
|
|
|
if (Block) FinishBlock(Block);
|
|
|
|
Block = createBlock(false);
|
|
|
|
Block->setTerminator(I);
|
|
|
|
Block->addSuccessor(IBlock);
|
|
|
|
return addStmt(I->getTarget());
|
|
|
|
}
|
|
|
|
|
2007-08-24 02:43:24 +08:00
|
|
|
|
2007-08-24 05:26:19 +08:00
|
|
|
} // end anonymous namespace
|
2007-08-24 00:51:22 +08:00
|
|
|
|
|
|
|
/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
|
|
|
|
/// block has no successors or predecessors. If this is the first block
|
|
|
|
/// created in the CFG, it is automatically set to be the Entry and Exit
|
|
|
|
/// of the CFG.
|
2007-09-06 04:02:05 +08:00
|
|
|
CFGBlock* CFG::createBlock() {
|
2007-08-24 00:51:22 +08:00
|
|
|
bool first_block = begin() == end();
|
|
|
|
|
|
|
|
// Create the block.
|
2007-09-06 04:02:05 +08:00
|
|
|
Blocks.push_front(CFGBlock(NumBlockIDs++));
|
2007-08-24 00:51:22 +08:00
|
|
|
|
|
|
|
// If this is the first block, set it as the Entry and Exit.
|
|
|
|
if (first_block) Entry = Exit = &front();
|
|
|
|
|
|
|
|
// Return the block.
|
|
|
|
return &front();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
|
|
|
|
/// CFG is returned to the caller.
|
|
|
|
CFG* CFG::buildCFG(Stmt* Statement) {
|
2007-08-22 05:42:03 +08:00
|
|
|
CFGBuilder Builder;
|
2007-08-24 00:51:22 +08:00
|
|
|
return Builder.buildCFG(Statement);
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 00:51:22 +08:00
|
|
|
/// reverseStmts - Reverses the orders of statements within a CFGBlock.
|
2007-08-22 05:42:03 +08:00
|
|
|
void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
|
|
|
|
|
2007-10-02 03:33:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFG: Queries for BlkExprs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2008-01-18 04:48:37 +08:00
|
|
|
typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
|
2007-10-02 03:33:33 +08:00
|
|
|
}
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
|
|
|
|
if (!Terminator)
|
2008-01-26 08:03:27 +08:00
|
|
|
return;
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
|
2008-01-26 08:03:27 +08:00
|
|
|
if (!*I) continue;
|
|
|
|
|
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
|
|
|
|
if (B->isAssignmentOp()) Set.insert(B);
|
|
|
|
|
|
|
|
FindSubExprAssignments(*I, Set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-02 03:33:33 +08:00
|
|
|
static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
|
|
|
|
BlkExprMapTy* M = new BlkExprMapTy();
|
|
|
|
|
2008-01-26 08:03:27 +08:00
|
|
|
// Look for assignments that are used as subexpressions. These are the
|
2008-04-17 05:10:48 +08:00
|
|
|
// only assignments that we want to *possibly* register as a block-level
|
|
|
|
// expression. Basically, if an assignment occurs both in a subexpression
|
|
|
|
// and at the block-level, it is a block-level expression.
|
2008-01-26 08:03:27 +08:00
|
|
|
llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
|
|
|
|
|
2007-10-02 03:33:33 +08:00
|
|
|
for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
|
|
|
|
for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
|
2008-01-26 08:03:27 +08:00
|
|
|
FindSubExprAssignments(*BI, SubExprAssignments);
|
2008-01-18 04:48:37 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
|
|
|
|
|
|
|
|
// Iterate over the statements again on identify the Expr* and Stmt* at
|
|
|
|
// the block-level that are block-level expressions.
|
|
|
|
|
2008-01-26 08:03:27 +08:00
|
|
|
for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
|
2008-04-17 05:10:48 +08:00
|
|
|
if (Expr* Exp = dyn_cast<Expr>(*BI)) {
|
2008-01-26 08:03:27 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
|
2008-01-26 08:03:27 +08:00
|
|
|
// Assignment expressions that are not nested within another
|
|
|
|
// expression are really "statements" whose value is never
|
|
|
|
// used by another expression.
|
2008-04-17 05:10:48 +08:00
|
|
|
if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
|
2008-01-26 08:03:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
2008-04-17 05:10:48 +08:00
|
|
|
else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
|
2008-01-26 08:03:27 +08:00
|
|
|
// Special handling for statement expressions. The last statement
|
|
|
|
// in the statement expression is also a block-level expr.
|
2008-04-17 05:10:48 +08:00
|
|
|
const CompoundStmt* C = Terminator->getSubStmt();
|
2008-01-18 04:48:37 +08:00
|
|
|
if (!C->body_empty()) {
|
2008-01-26 08:03:27 +08:00
|
|
|
unsigned x = M->size();
|
2008-01-18 04:48:37 +08:00
|
|
|
(*M)[C->body_back()] = x;
|
|
|
|
}
|
|
|
|
}
|
2008-01-26 07:22:27 +08:00
|
|
|
|
2008-01-26 08:03:27 +08:00
|
|
|
unsigned x = M->size();
|
2008-04-17 05:10:48 +08:00
|
|
|
(*M)[Exp] = x;
|
2008-01-26 08:03:27 +08:00
|
|
|
}
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
// Look at terminators. The condition is a block-level expression.
|
|
|
|
|
2008-11-13 05:11:49 +08:00
|
|
|
Stmt* S = I->getTerminatorCondition();
|
2008-04-17 05:10:48 +08:00
|
|
|
|
2008-11-13 05:11:49 +08:00
|
|
|
if (S && M->find(S) == M->end()) {
|
2008-04-17 05:10:48 +08:00
|
|
|
unsigned x = M->size();
|
2008-11-13 05:11:49 +08:00
|
|
|
(*M)[S] = x;
|
2008-04-17 05:10:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-18 04:48:37 +08:00
|
|
|
return M;
|
2007-10-02 03:33:33 +08:00
|
|
|
}
|
|
|
|
|
2008-01-18 04:48:37 +08:00
|
|
|
CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
|
|
|
|
assert(S != NULL);
|
2007-10-02 03:33:33 +08:00
|
|
|
if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
|
|
|
|
|
|
|
|
BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
|
2008-01-18 04:48:37 +08:00
|
|
|
BlkExprMapTy::iterator I = M->find(S);
|
2007-10-02 03:33:33 +08:00
|
|
|
|
|
|
|
if (I == M->end()) return CFG::BlkExprNumTy();
|
|
|
|
else return CFG::BlkExprNumTy(I->second);
|
|
|
|
}
|
2007-08-30 05:56:09 +08:00
|
|
|
|
2007-10-02 03:33:33 +08:00
|
|
|
unsigned CFG::getNumBlkExprs() {
|
|
|
|
if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
|
|
|
|
return M->size();
|
|
|
|
else {
|
|
|
|
// We assume callers interested in the number of BlkExprs will want
|
|
|
|
// the map constructed if it doesn't already exist.
|
|
|
|
BlkExprMap = (void*) PopulateBlkExprMap(*this);
|
|
|
|
return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 02:00:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cleanup: CFG dstor.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-02 03:33:33 +08:00
|
|
|
CFG::~CFG() {
|
|
|
|
delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
|
|
|
|
}
|
|
|
|
|
2007-08-30 05:56:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFG pretty printing
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
namespace {
|
2007-08-23 05:05:42 +08:00
|
|
|
|
2008-01-09 02:15:10 +08:00
|
|
|
class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
|
2007-09-01 06:26:13 +08:00
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
|
|
|
|
StmtMapTy StmtMap;
|
|
|
|
signed CurrentBlock;
|
|
|
|
unsigned CurrentStmt;
|
2007-09-01 06:26:13 +08:00
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
public:
|
2007-09-01 06:26:13 +08:00
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
|
|
|
|
for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
|
|
|
|
unsigned j = 1;
|
|
|
|
for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
|
|
|
|
BI != BEnd; ++BI, ++j )
|
|
|
|
StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
|
|
|
|
}
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
virtual ~StmtPrinterHelper() {}
|
2007-08-30 05:56:09 +08:00
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
void setBlockID(signed i) { CurrentBlock = i; }
|
|
|
|
void setStmtID(unsigned i) { CurrentStmt = i; }
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
|
2007-09-01 06:26:13 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
StmtMapTy::iterator I = StmtMap.find(Terminator);
|
2007-08-22 05:42:03 +08:00
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
if (I == StmtMap.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
|
|
|
|
&& I->second.second == CurrentStmt)
|
|
|
|
return false;
|
|
|
|
|
2007-09-01 06:26:13 +08:00
|
|
|
OS << "[B" << I->second.first << "." << I->second.second << "]";
|
|
|
|
return true;
|
2007-09-01 05:30:12 +08:00
|
|
|
}
|
|
|
|
};
|
2007-08-23 02:22:34 +08:00
|
|
|
|
2008-01-09 02:15:10 +08:00
|
|
|
class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
|
|
|
|
: public StmtVisitor<CFGBlockTerminatorPrint,void> {
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
llvm::raw_ostream& OS;
|
2007-09-01 05:30:12 +08:00
|
|
|
StmtPrinterHelper* Helper;
|
2007-08-24 05:42:29 +08:00
|
|
|
public:
|
2008-09-13 13:16:45 +08:00
|
|
|
CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
|
2007-09-01 05:30:12 +08:00
|
|
|
: OS(os), Helper(helper) {}
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
void VisitIfStmt(IfStmt* I) {
|
|
|
|
OS << "if ";
|
2007-09-01 05:30:12 +08:00
|
|
|
I->getCond()->printPretty(OS,Helper);
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default case.
|
2008-04-17 05:10:48 +08:00
|
|
|
void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
|
2007-08-24 05:42:29 +08:00
|
|
|
|
|
|
|
void VisitForStmt(ForStmt* F) {
|
|
|
|
OS << "for (" ;
|
2007-08-31 05:28:02 +08:00
|
|
|
if (F->getInit()) OS << "...";
|
|
|
|
OS << "; ";
|
2007-09-01 05:30:12 +08:00
|
|
|
if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
|
2007-08-31 05:28:02 +08:00
|
|
|
OS << "; ";
|
|
|
|
if (F->getInc()) OS << "...";
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << ")";
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitWhileStmt(WhileStmt* W) {
|
|
|
|
OS << "while " ;
|
2007-09-01 05:30:12 +08:00
|
|
|
if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
|
2007-08-24 05:42:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDoStmt(DoStmt* D) {
|
|
|
|
OS << "do ... while ";
|
2007-09-01 05:30:12 +08:00
|
|
|
if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
|
2007-08-28 05:27:44 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
void VisitSwitchStmt(SwitchStmt* Terminator) {
|
2007-08-28 05:27:44 +08:00
|
|
|
OS << "switch ";
|
2008-04-17 05:10:48 +08:00
|
|
|
Terminator->getCond()->printPretty(OS,Helper);
|
2007-08-28 05:27:44 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 05:49:40 +08:00
|
|
|
void VisitConditionalOperator(ConditionalOperator* C) {
|
|
|
|
C->getCond()->printPretty(OS,Helper);
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << " ? ... : ...";
|
2007-09-01 05:49:40 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:29:13 +08:00
|
|
|
void VisitChooseExpr(ChooseExpr* C) {
|
|
|
|
OS << "__builtin_choose_expr( ";
|
|
|
|
C->getCond()->printPretty(OS,Helper);
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << " )";
|
2007-09-01 06:29:13 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:26:13 +08:00
|
|
|
void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
|
|
|
|
OS << "goto *";
|
|
|
|
I->getTarget()->printPretty(OS,Helper);
|
|
|
|
}
|
|
|
|
|
2007-09-01 05:49:40 +08:00
|
|
|
void VisitBinaryOperator(BinaryOperator* B) {
|
|
|
|
if (!B->isLogicalOp()) {
|
|
|
|
VisitExpr(B);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
B->getLHS()->printPretty(OS,Helper);
|
|
|
|
|
|
|
|
switch (B->getOpcode()) {
|
|
|
|
case BinaryOperator::LOr:
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << " || ...";
|
2007-09-01 05:49:40 +08:00
|
|
|
return;
|
|
|
|
case BinaryOperator::LAnd:
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << " && ...";
|
2007-09-01 05:49:40 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
assert(false && "Invalid logical operator.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-28 05:54:41 +08:00
|
|
|
void VisitExpr(Expr* E) {
|
2007-09-01 05:30:12 +08:00
|
|
|
E->printPretty(OS,Helper);
|
2007-08-28 05:54:41 +08:00
|
|
|
}
|
2007-08-24 05:42:29 +08:00
|
|
|
};
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
|
2007-09-01 06:26:13 +08:00
|
|
|
if (Helper) {
|
|
|
|
// special printing for statement-expressions.
|
2008-04-17 05:10:48 +08:00
|
|
|
if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
|
2007-09-01 06:26:13 +08:00
|
|
|
CompoundStmt* Sub = SE->getSubStmt();
|
|
|
|
|
|
|
|
if (Sub->child_begin() != Sub->child_end()) {
|
2007-09-01 06:47:06 +08:00
|
|
|
OS << "({ ... ; ";
|
2007-10-30 04:41:04 +08:00
|
|
|
Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
|
2007-09-01 06:47:06 +08:00
|
|
|
OS << " })\n";
|
2007-09-01 06:26:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// special printing for comma expressions.
|
2008-04-17 05:10:48 +08:00
|
|
|
if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
|
2007-09-01 06:26:13 +08:00
|
|
|
if (B->getOpcode() == BinaryOperator::Comma) {
|
|
|
|
OS << "... , ";
|
|
|
|
Helper->handledStmt(B->getRHS(),OS);
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
Terminator->printPretty(OS, Helper);
|
2007-09-01 06:26:13 +08:00
|
|
|
|
|
|
|
// Expressions need a newline.
|
2008-04-17 05:10:48 +08:00
|
|
|
if (isa<Expr>(Terminator)) OS << '\n';
|
2007-09-01 06:26:13 +08:00
|
|
|
}
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
|
2007-09-01 05:30:12 +08:00
|
|
|
StmtPrinterHelper* Helper, bool print_edges) {
|
|
|
|
|
|
|
|
if (Helper) Helper->setBlockID(B.getBlockID());
|
|
|
|
|
2007-08-30 05:56:09 +08:00
|
|
|
// Print the header.
|
2007-09-01 05:30:12 +08:00
|
|
|
OS << "\n [ B" << B.getBlockID();
|
|
|
|
|
|
|
|
if (&B == &cfg->getEntry())
|
|
|
|
OS << " (ENTRY) ]\n";
|
|
|
|
else if (&B == &cfg->getExit())
|
|
|
|
OS << " (EXIT) ]\n";
|
|
|
|
else if (&B == cfg->getIndirectGotoBlock())
|
2007-08-30 05:56:09 +08:00
|
|
|
OS << " (INDIRECT GOTO DISPATCH) ]\n";
|
2007-09-01 05:30:12 +08:00
|
|
|
else
|
|
|
|
OS << " ]\n";
|
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
// Print the label of this block.
|
2008-04-17 05:10:48 +08:00
|
|
|
if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
if (print_edges)
|
|
|
|
OS << " ";
|
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << L->getName();
|
2008-04-17 05:10:48 +08:00
|
|
|
else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << "case ";
|
|
|
|
C->getLHS()->printPretty(OS);
|
|
|
|
if (C->getRHS()) {
|
|
|
|
OS << " ... ";
|
|
|
|
C->getRHS()->printPretty(OS);
|
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
}
|
2008-04-17 05:10:48 +08:00
|
|
|
else if (isa<DefaultStmt>(Terminator))
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << "default";
|
2007-09-01 05:30:12 +08:00
|
|
|
else
|
|
|
|
assert(false && "Invalid label statement in CFGBlock.");
|
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << ":\n";
|
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-22 05:42:03 +08:00
|
|
|
// Iterate through the statements in the block and print them.
|
|
|
|
unsigned j = 1;
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
|
|
|
|
I != E ; ++I, ++j ) {
|
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
// Print the statement # in the basic block and the statement itself.
|
2007-09-01 05:30:12 +08:00
|
|
|
if (print_edges)
|
|
|
|
OS << " ";
|
|
|
|
|
2008-09-13 13:16:45 +08:00
|
|
|
OS << llvm::format("%3d", j) << ": ";
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
if (Helper)
|
|
|
|
Helper->setStmtID(j);
|
2007-09-01 06:26:13 +08:00
|
|
|
|
|
|
|
print_stmt(OS,Helper,*I);
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
// Print the terminator of this block.
|
2007-09-01 05:30:12 +08:00
|
|
|
if (B.getTerminator()) {
|
|
|
|
if (print_edges)
|
|
|
|
OS << " ";
|
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << " T: ";
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
if (Helper) Helper->setBlockID(-1);
|
|
|
|
|
|
|
|
CFGBlockTerminatorPrint TPrinter(OS,Helper);
|
|
|
|
TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
|
2008-01-31 07:02:42 +08:00
|
|
|
OS << '\n';
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
if (print_edges) {
|
|
|
|
// Print the predecessors of this block.
|
2007-09-01 05:30:12 +08:00
|
|
|
OS << " Predecessors (" << B.pred_size() << "):";
|
2007-08-30 07:20:49 +08:00
|
|
|
unsigned i = 0;
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
|
|
|
|
I != E; ++I, ++i) {
|
|
|
|
|
|
|
|
if (i == 8 || (i-8) == 0)
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << "\n ";
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << " B" << (*I)->getBlockID();
|
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << '\n';
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
// Print the successors of this block.
|
2007-09-01 05:30:12 +08:00
|
|
|
OS << " Successors (" << B.succ_size() << "):";
|
2007-08-30 07:20:49 +08:00
|
|
|
i = 0;
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
|
|
|
|
I != E; ++I, ++i) {
|
|
|
|
|
|
|
|
if (i == 8 || (i-8) % 10 == 0)
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << "\n ";
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << " B" << (*I)->getBlockID();
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
|
2007-08-30 07:20:49 +08:00
|
|
|
OS << '\n';
|
2007-08-22 05:42:03 +08:00
|
|
|
}
|
2007-09-01 05:30:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// dump - A simple pretty printer of a CFG that outputs to stderr.
|
2008-09-13 13:16:45 +08:00
|
|
|
void CFG::dump() const { print(llvm::errs()); }
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
/// print - A simple pretty printer of a CFG that outputs to an ostream.
|
2008-09-13 13:16:45 +08:00
|
|
|
void CFG::print(llvm::raw_ostream& OS) const {
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
StmtPrinterHelper Helper(this);
|
|
|
|
|
|
|
|
// Print the entry block.
|
|
|
|
print_block(OS, this, getEntry(), &Helper, true);
|
|
|
|
|
|
|
|
// Iterate through the CFGBlocks and print them one by one.
|
|
|
|
for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
|
|
|
|
// Skip the entry block, because we already printed it.
|
|
|
|
if (&(*I) == &getEntry() || &(*I) == &getExit())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
print_block(OS, this, *I, &Helper, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the exit block.
|
|
|
|
print_block(OS, this, getExit(), &Helper, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
|
2008-09-13 13:16:45 +08:00
|
|
|
void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
|
|
|
|
/// Generally this will only be called from CFG::print.
|
2008-09-13 13:16:45 +08:00
|
|
|
void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
|
2007-09-01 05:30:12 +08:00
|
|
|
StmtPrinterHelper Helper(cfg);
|
|
|
|
print_block(OS, cfg, *this, &Helper, true);
|
2007-08-24 00:51:22 +08:00
|
|
|
}
|
2007-08-30 05:56:09 +08:00
|
|
|
|
2008-01-31 07:02:42 +08:00
|
|
|
/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
|
2008-09-13 13:16:45 +08:00
|
|
|
void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
|
2008-01-31 07:02:42 +08:00
|
|
|
CFGBlockTerminatorPrint TPrinter(OS,NULL);
|
|
|
|
TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
|
|
|
|
}
|
|
|
|
|
2008-11-13 05:11:49 +08:00
|
|
|
Stmt* CFGBlock::getTerminatorCondition() {
|
2008-04-17 05:10:48 +08:00
|
|
|
|
|
|
|
if (!Terminator)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Expr* E = NULL;
|
|
|
|
|
|
|
|
switch (Terminator->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
E = cast<ForStmt>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
E = cast<WhileStmt>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
E = cast<DoStmt>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
E = cast<IfStmt>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::ChooseExprClass:
|
|
|
|
E = cast<ChooseExpr>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::IndirectGotoStmtClass:
|
|
|
|
E = cast<IndirectGotoStmt>(Terminator)->getTarget();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::SwitchStmtClass:
|
|
|
|
E = cast<SwitchStmt>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
E = cast<ConditionalOperator>(Terminator)->getCond();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::BinaryOperatorClass: // '&&' and '||'
|
|
|
|
E = cast<BinaryOperator>(Terminator)->getLHS();
|
2008-11-13 05:11:49 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Stmt::ObjCForCollectionStmtClass:
|
|
|
|
return Terminator;
|
2008-04-17 05:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return E ? E->IgnoreParens() : NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-17 00:06:00 +08:00
|
|
|
bool CFGBlock::hasBinaryBranchTerminator() const {
|
|
|
|
|
|
|
|
if (!Terminator)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Expr* E = NULL;
|
|
|
|
|
|
|
|
switch (Terminator->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
case Stmt::WhileStmtClass:
|
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
case Stmt::ChooseExprClass:
|
|
|
|
case Stmt::ConditionalOperatorClass:
|
|
|
|
case Stmt::BinaryOperatorClass:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return E ? E->IgnoreParens() : NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-31 07:02:42 +08:00
|
|
|
|
2007-08-30 05:56:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFG Graphviz Visualization
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-01 05:30:12 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2007-09-17 14:16:32 +08:00
|
|
|
static StmtPrinterHelper* GraphHelper;
|
2007-09-01 05:30:12 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void CFG::viewCFG() const {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
StmtPrinterHelper H(this);
|
|
|
|
GraphHelper = &H;
|
|
|
|
llvm::ViewGraph(this,"CFG");
|
|
|
|
GraphHelper = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-30 05:56:09 +08:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
|
|
|
|
|
2007-09-16 08:28:28 +08:00
|
|
|
#ifndef NDEBUG
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string OutSStr;
|
|
|
|
llvm::raw_string_ostream Out(OutSStr);
|
2007-09-01 05:30:12 +08:00
|
|
|
print_block(Out,Graph, *Node, GraphHelper, false);
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string& OutStr = Out.str();
|
2007-08-30 05:56:09 +08:00
|
|
|
|
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
|
|
|
|
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
}
|
|
|
|
|
|
|
|
return OutStr;
|
2007-09-16 08:28:28 +08:00
|
|
|
#else
|
|
|
|
return "";
|
|
|
|
#endif
|
2007-08-30 05:56:09 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|