blob: 82351370c110c2f9c87c3a337bfa9a15667af004 [file] [log] [blame]
Ted Kremenek97f75312007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Ted Kremenek and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/CFG.h"
16#include "clang/AST/Expr.h"
Ted Kremenek95e854d2007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000019#include <iostream>
20#include <iomanip>
21#include <algorithm>
22using namespace clang;
23
24namespace {
25
26 // SaveAndRestore - A utility class that uses RIIA to save and restore
27 // the value of a variable.
28 template<typename T>
29 struct SaveAndRestore {
30 SaveAndRestore(T& x) : X(x), old_value(x) {}
31 ~SaveAndRestore() { X = old_value; }
32
33 T& X;
34 T old_value;
35 };
36}
37
38/// CFGBuilder - This class is implements CFG construction from an AST.
39/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Ted Kremenek95e854d2007-08-21 22:06:14 +000047/// CFG construction is done via a recursive walk of an AST.
48/// We actually parse the AST in reverse order so that the successor
49/// of a basic block is constructed prior to its predecessor. This
50/// allows us to nicely capture implicit fall-throughs without extra
51/// basic blocks.
52///
53class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000054 CFG* cfg;
55 CFGBlock* Block;
56 CFGBlock* Exit;
57 CFGBlock* Succ;
58 unsigned NumBlocks;
59
Ted Kremenekc5de2222007-08-21 23:26:17 +000060 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
61 LabelMapTy LabelMap;
62
Ted Kremenekf5392b72007-08-22 15:40:58 +000063 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000064 BackpatchBlocksTy BackpatchBlocks;
65
Ted Kremenek97f75312007-08-21 21:42:03 +000066public:
67 explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL),
68 NumBlocks(0) {
69 // Create an empty CFG.
70 cfg = new CFG();
71 }
72
73 ~CFGBuilder() { delete cfg; }
Ted Kremenekd8313202007-08-22 18:22:34 +000074
Ted Kremenekbec06e82007-08-22 21:05:42 +000075 /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
Ted Kremenek97f75312007-08-21 21:42:03 +000076 /// represent an arbitrary statement. Examples include a single expression
77 /// or a function body (compound statement). The ownership of the returned
78 /// CFG is transferred to the caller. If CFG construction fails, this method
79 /// returns NULL.
Ted Kremenekbec06e82007-08-22 21:05:42 +000080 CFG* BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +000081 if (!Statement) return NULL;
82
Ted Kremenekc5de2222007-08-21 23:26:17 +000083 assert (!Exit && "CFGBuilder should only be used to construct one CFG");
Ted Kremenek97f75312007-08-21 21:42:03 +000084
85 // Create the exit block.
86 Block = createBlock();
87 Exit = Block;
88
89 // Visit the statements and create the CFG.
Ted Kremenek95e854d2007-08-21 22:06:14 +000090 if (CFGBlock* B = Visit(Statement)) {
Ted Kremenekd8313202007-08-22 18:22:34 +000091 // Finalize the last constructed block. This usually involves
92 // reversing the order of the statements in the block.
93 FinishBlock(B);
Ted Kremenekbec06e82007-08-22 21:05:42 +000094 cfg->setEntry(B);
Ted Kremenekc5de2222007-08-21 23:26:17 +000095
96 // Backpatch the gotos whose label -> block mappings we didn't know
97 // when we encountered them.
98 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
99 E = BackpatchBlocks.end(); I != E; ++I ) {
100
101 CFGBlock* B = *I;
102 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
103 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
104
105 if (LI == LabelMap.end())
106 return NULL; // No matching label. Bad CFG.
107
108 B->addSuccessor(LI->second);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000109 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000110
Ted Kremenek97f75312007-08-21 21:42:03 +0000111 // NULL out cfg so that repeated calls
112 CFG* t = cfg;
113 cfg = NULL;
114 return t;
115 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000116 else return NULL;
Ted Kremenek97f75312007-08-21 21:42:03 +0000117 }
Ted Kremenek95e854d2007-08-21 22:06:14 +0000118
Ted Kremenek97f75312007-08-21 21:42:03 +0000119 // createBlock - Used to lazily create blocks that are connected
120 // to the current (global) succcessor.
121 CFGBlock* createBlock( bool add_successor = true ) {
122 CFGBlock* B = cfg->createBlock(NumBlocks++);
123 if (add_successor && Succ) B->addSuccessor(Succ);
124 return B;
125 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000126
127 // FinishBlock - When the last statement has been added to the block,
128 // usually we must reverse the statements because they have been inserted
129 // in reverse order. When processing labels, however, there are cases
130 // in the recursion where we may have already reversed the statements
131 // in a block. This method safely tidies up a block: if the block
132 // has a label at the front, it has already been reversed. Otherwise,
133 // we reverse it.
134 void FinishBlock(CFGBlock* B) {
135 assert (B);
136 CFGBlock::iterator I = B->begin();
137 if (I != B->end()) {
138 Stmt* S = *I;
139 if (S->getStmtClass() != Stmt::LabelStmtClass)
140 B->reverseStmts();
141 }
142 }
Ted Kremenek97f75312007-08-21 21:42:03 +0000143
Ted Kremenek95e854d2007-08-21 22:06:14 +0000144 /// Here we handle statements with no branching control flow.
145 CFGBlock* VisitStmt(Stmt* Statement) {
146 // We cannot assume that we are in the middle of a basic block, since
147 // the CFG might only be constructed for this single statement. If
148 // we have no current basic block, just create one lazily.
149 if (!Block) Block = createBlock();
150
151 // Simply add the statement to the current block. We actually
152 // insert statements in reverse order; this order is reversed later
153 // when processing the containing element in the AST.
154 Block->appendStmt(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000155
156 return Block;
157 }
158
Ted Kremenekd8313202007-08-22 18:22:34 +0000159 CFGBlock* VisitNullStmt(NullStmt* Statement) {
160 return Block;
161 }
162
Ted Kremenek95e854d2007-08-21 22:06:14 +0000163 CFGBlock* VisitCompoundStmt(CompoundStmt* C) {
164 // The value returned from this function is the last created CFGBlock
165 // that represents the "entry" point for the translated AST node.
Ted Kremenekd8313202007-08-22 18:22:34 +0000166 CFGBlock* LastBlock;
167
Ted Kremenek95e854d2007-08-21 22:06:14 +0000168 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
Ted Kremenekd8313202007-08-22 18:22:34 +0000169 E = C->body_rend(); I != E; ++I )
Ted Kremenek95e854d2007-08-21 22:06:14 +0000170 // Add the statement to the current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000171 if (!(LastBlock=Visit(*I)))
172 return NULL;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000173
Ted Kremenekd8313202007-08-22 18:22:34 +0000174 return LastBlock;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000175 }
176
177 CFGBlock* VisitIfStmt(IfStmt* I) {
178
179 // We may see an if statement in the middle of a basic block, or
180 // it may be the first statement we are processing. In either case,
181 // we create a new basic block. First, we create the blocks for
182 // the then...else statements, and then we create the block containing
183 // the if statement. If we were in the middle of a block, we
184 // stop processing that block and reverse its statements. That block
185 // is then the implicit successor for the "then" and "else" clauses.
186
187 // The block we were proccessing is now finished. Make it the
188 // successor block.
189 if (Block) {
190 Succ = Block;
Ted Kremenekd8313202007-08-22 18:22:34 +0000191 FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000192 }
193
194 // Process the false branch. NULL out Block so that the recursive
195 // call to Visit will create a new basic block.
196 // Null out Block so that all successor
197 CFGBlock* ElseBlock = Succ;
198
199 if (Stmt* Else = I->getElse()) {
200 SaveAndRestore<CFGBlock*> sv(Succ);
201
202 // NULL out Block so that the recursive call to Visit will
203 // create a new basic block.
204 Block = NULL;
205 ElseBlock = Visit(Else);
206 if (!ElseBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000207 FinishBlock(ElseBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000208 }
209
210 // Process the true branch. NULL out Block so that the recursive
211 // call to Visit will create a new basic block.
212 // Null out Block so that all successor
213 CFGBlock* ThenBlock;
214 {
215 Stmt* Then = I->getThen();
216 assert (Then);
217 SaveAndRestore<CFGBlock*> sv(Succ);
218 Block = NULL;
219 ThenBlock = Visit(Then);
220 if (!ThenBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000221 FinishBlock(ThenBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000222 }
223
224 // Now create a new block containing the if statement.
225 Block = createBlock(false);
226
227 // Add the condition as the last statement in the new block.
228 Block->appendStmt(I->getCond());
229
230 // Set the terminator of the new block to the If statement.
231 Block->setTerminator(I);
232
233 // Now add the successors.
234 Block->addSuccessor(ThenBlock);
235 Block->addSuccessor(ElseBlock);
236
237 return Block;
238 }
239
240 CFGBlock* VisitReturnStmt(ReturnStmt* R) {
241 // If we were in the middle of a block we stop processing that block
242 // and reverse its statements.
243 //
244 // NOTE: If a "return" appears in the middle of a block, this means
245 // that the code afterwards is DEAD (unreachable). We still
246 // keep a basic block for that code; a simple "mark-and-sweep"
247 // from the entry block will be able to report such dead
248 // blocks.
Ted Kremenekd8313202007-08-22 18:22:34 +0000249 if (Block) FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000250
251 // Create the new block.
252 Block = createBlock(false);
253
254 // The Exit block is the only successor.
255 Block->addSuccessor(Exit);
256
257 // Add the return expression to the block.
258 Block->appendStmt(R);
259
260 // Add the return statement itself to the block.
261 if (R->getRetValue()) Block->appendStmt(R->getRetValue());
262
263 return Block;
Ted Kremenekc5de2222007-08-21 23:26:17 +0000264 }
265
266 CFGBlock* VisitLabelStmt(LabelStmt* L) {
267 // Get the block of the labeled statement. Add it to our map.
268 CFGBlock* LabelBlock = Visit(L->getSubStmt());
Ted Kremenekd8313202007-08-22 18:22:34 +0000269 assert (LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000270
271 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
272 LabelMap[ L ] = LabelBlock;
273
274 // Labels partition blocks, so this is the end of the basic block
275 // we were processing (the label is the first statement).
Ted Kremenekd8313202007-08-22 18:22:34 +0000276 LabelBlock->appendStmt(L);
277 FinishBlock(LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000278
279 // We set Block to NULL to allow lazy creation of a new block
280 // (if necessary);
281 Block = NULL;
282
283 // This block is now the implicit successor of other blocks.
284 Succ = LabelBlock;
285
286 return LabelBlock;
287 }
288
289 CFGBlock* VisitGotoStmt(GotoStmt* G) {
290 // Goto is a control-flow statement. Thus we stop processing the
291 // current block and create a new one.
Ted Kremenekd8313202007-08-22 18:22:34 +0000292 if (Block) FinishBlock(Block);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000293 Block = createBlock(false);
294 Block->setTerminator(G);
295
296 // If we already know the mapping to the label block add the
297 // successor now.
298 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
299
300 if (I == LabelMap.end())
301 // We will need to backpatch this block later.
302 BackpatchBlocks.push_back(Block);
303 else
304 Block->addSuccessor(I->second);
305
306 return Block;
307 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000308
309 CFGBlock* VisitForStmt(ForStmt* F) {
310 // For is a control-flow statement. Thus we stop processing the
311 // current block.
312 if (Block) FinishBlock(Block);
313
314 // Besides the loop body, we actually create two new blocks:
315 //
316 // The first contains the initialization statement for the loop.
317 //
318 // The second block evaluates the loop condition.
319 //
320 // We create the initialization block last, as that will be the block
321 // we return for the recursion.
322
323 CFGBlock* CondBlock = createBlock(false);
324 if (Stmt* C = F->getCond()) CondBlock->appendStmt(C);
325 CondBlock->setTerminator(F);
326 Succ = CondBlock;
327
328 // Now create the loop body.
329 {
330 assert (F->getBody());
331 SaveAndRestore<CFGBlock*> sv(Block);
332
333 // create a new block to contain the body.
334 Block = createBlock();
335
336 // If we have increment code, insert it at the end of the body block.
337 if (Stmt* I = F->getInc()) Block->appendStmt(I);
338
339 // Now populate the body block, and in the process create new blocks
340 // as we walk the body of the loop.
341 CFGBlock* BodyBlock = Visit(F->getBody());
342
343 assert (BodyBlock);
344 FinishBlock(BodyBlock);
345
346 // This new body block is a successor to our condition block.
347 CondBlock->addSuccessor(BodyBlock);
348 }
349
350 // Link up the condition block with the code that follows the loop.
351 // (the false branch).
352 CondBlock->addSuccessor(Block);
353
354 // Now create the block to contain the initialization.
355 Succ = CondBlock;
356 Block = createBlock();
357
358 if (Stmt* I = F->getInit()) Block->appendStmt(I);
359 return Block;
360 }
Ted Kremenekbec06e82007-08-22 21:05:42 +0000361
362 CFGBlock* VisitWhileStmt(WhileStmt* W) {
363 // While is a control-flow statement. Thus we stop processing the
364 // current block.
365 if (Block) FinishBlock(Block);
366
367 CFGBlock* ConditionBlock = createBlock(false);
368 ConditionBlock->setTerminator(W);
369 if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C);
370
371 // Process the loop body.
372 {
373 assert (W->getBody());
374 SaveAndRestore<CFGBlock*> sv(Block);
375
376 Succ = ConditionBlock;
377 Block = NULL;
378 CFGBlock* BodyBlock = Visit(W->getBody());
379
380 assert (BodyBlock);
381
382 ConditionBlock->addSuccessor(BodyBlock);
383 }
384
385 ConditionBlock->addSuccessor(Block);
386
387 // There can be no more statements in the condition block
388 // since we loop back to this block. NULL out Block to force
389 // lazy creation of another block.
390 Block = NULL;
391 Succ = ConditionBlock;
392
393 return ConditionBlock;
394 }
395
Ted Kremenek97f75312007-08-21 21:42:03 +0000396};
397
398// BuildCFG - A helper function that builds CFGs from ASTS.
Ted Kremenek95e854d2007-08-21 22:06:14 +0000399CFG* CFG::BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +0000400 CFGBuilder Builder;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000401 return Builder.BuildCFG(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000402}
403
404// reverseStmts - A method that reverses the order of the statements within
405// a CFGBlock.
406void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
407
408// dump - A simple pretty printer of a CFG that outputs to stderr.
409void CFG::dump() { print(std::cerr); }
410
411// print - A simple pretty printer of a CFG that outputs to an ostream.
412void CFG::print(std::ostream& OS) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000413 // First print out the Entry block, which may not be the first block
414 // in our list of blocks
415 if (begin() != end()) {
416 CFGBlock& Entry = getEntry();
417 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
418 Entry.print(OS);
419 }
420
Ted Kremenek97f75312007-08-21 21:42:03 +0000421 // Iterate through the CFGBlocks and print them one by one. Specially
422 // designate the Entry and Exit blocks.
423 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000424 // Skip the entry block, because we already printed it.
425 if (&(*I) == &getEntry())
426 continue;
427
Ted Kremenek97f75312007-08-21 21:42:03 +0000428 OS << "\n [ B" << I->getBlockID();
Ted Kremenekbec06e82007-08-22 21:05:42 +0000429
Ted Kremenekc5de2222007-08-21 23:26:17 +0000430 if (&(*I) == &getExit()) OS << " (EXIT) ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000431 else OS << " ]\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000432
Ted Kremenek97f75312007-08-21 21:42:03 +0000433 I->print(OS);
434 }
435 OS << "\n";
436}
437
Ted Kremenekd8313202007-08-22 18:22:34 +0000438
439namespace {
440
441 class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
442 void > {
443 std::ostream& OS;
444 public:
445 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
446
447 void VisitIfStmt(IfStmt* I) {
448 OS << "if ";
449 I->getCond()->printPretty(std::cerr);
450 OS << "\n";
451 }
452
453 // Default case.
454 void VisitStmt(Stmt* S) { S->printPretty(OS); }
455
456 void VisitForStmt(ForStmt* F) {
457 OS << "for (" ;
458 if (Stmt* I = F->getInit()) I->printPretty(OS);
459 OS << " ; ";
460 if (Stmt* C = F->getCond()) C->printPretty(OS);
461 OS << " ; ";
462 if (Stmt* I = F->getInc()) I->printPretty(OS);
463 OS << ")\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000464 }
465
466 void VisitWhileStmt(WhileStmt* W) {
467 OS << "while " ;
468 if (Stmt* C = W->getCond()) C->printPretty(OS);
469 OS << "\n";
470 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000471 };
472}
473
Ted Kremenek97f75312007-08-21 21:42:03 +0000474// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
475void CFGBlock::dump() { print(std::cerr); }
476
477// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
478// Generally this will only be called from CFG::print.
479void CFGBlock::print(std::ostream& OS) {
480
481 // Iterate through the statements in the block and print them.
482 OS << " ------------------------\n";
483 unsigned j = 1;
484 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000485 // Print the statement # in the basic block.
486 OS << " " << std::setw(3) << j << ": ";
487
488 // Print the statement/expression.
489 Stmt* S = *I;
490
491 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
492 OS << L->getName() << ": (LABEL)\n";
493 else
494 (*I)->printPretty(OS);
495
496 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000497 if (isa<Expr>(*I)) OS << '\n';
498 }
499 OS << " ------------------------\n";
500
501 // Print the predecessors of this block.
502 OS << " Predecessors (" << pred_size() << "):";
503 unsigned i = 0;
504 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
505 if (i == 8 || (i-8) == 0) {
506 OS << "\n ";
507 }
508 OS << " B" << (*I)->getBlockID();
509 }
510
511 // Print the terminator of this block.
512 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000513 if (ControlFlowStmt)
514 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
515 else
516 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000517
518 // Print the successors of this block.
519 OS << " Successors (" << succ_size() << "):";
520 i = 0;
521 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
522 if (i == 8 || (i-8) % 10 == 0) {
523 OS << "\n ";
524 }
525 OS << " B" << (*I)->getBlockID();
526 }
527 OS << '\n';
528}