Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1 | //===--- 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 Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <algorithm> |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 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 Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 47 | /// 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 | /// |
| 53 | class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 54 | CFG* cfg; |
| 55 | CFGBlock* Block; |
| 56 | CFGBlock* Exit; |
| 57 | CFGBlock* Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 58 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 59 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 60 | unsigned NumBlocks; |
| 61 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 62 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 63 | LabelMapTy LabelMap; |
| 64 | |
Ted Kremenek | f5392b7 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 65 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 66 | BackpatchBlocksTy BackpatchBlocks; |
| 67 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 68 | public: |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 69 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 70 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 71 | NumBlocks(0) { |
| 72 | // Create an empty CFG. |
| 73 | cfg = new CFG(); |
| 74 | } |
| 75 | |
| 76 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 78 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 79 | /// represent an arbitrary statement. Examples include a single expression |
| 80 | /// or a function body (compound statement). The ownership of the returned |
| 81 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 82 | /// returns NULL. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 83 | CFG* BuildCFG(Stmt* Statement) { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 84 | if (!Statement) return NULL; |
| 85 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 86 | assert (!Exit && "CFGBuilder should only be used to construct one CFG"); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 87 | |
| 88 | // Create the exit block. |
| 89 | Block = createBlock(); |
| 90 | Exit = Block; |
| 91 | |
| 92 | // Visit the statements and create the CFG. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 93 | if (CFGBlock* B = Visit(Statement)) { |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 94 | // Finalize the last constructed block. This usually involves |
| 95 | // reversing the order of the statements in the block. |
| 96 | FinishBlock(B); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 97 | cfg->setEntry(B); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 98 | |
| 99 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 100 | // when we encountered them. |
| 101 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 102 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 103 | |
| 104 | CFGBlock* B = *I; |
| 105 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 106 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 107 | |
| 108 | if (LI == LabelMap.end()) |
| 109 | return NULL; // No matching label. Bad CFG. |
| 110 | |
| 111 | B->addSuccessor(LI->second); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 112 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 114 | // NULL out cfg so that repeated calls |
| 115 | CFG* t = cfg; |
| 116 | cfg = NULL; |
| 117 | return t; |
| 118 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 119 | else return NULL; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 120 | } |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 122 | // createBlock - Used to lazily create blocks that are connected |
| 123 | // to the current (global) succcessor. |
| 124 | CFGBlock* createBlock( bool add_successor = true ) { |
| 125 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 126 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 127 | return B; |
| 128 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 129 | |
| 130 | // FinishBlock - When the last statement has been added to the block, |
| 131 | // usually we must reverse the statements because they have been inserted |
| 132 | // in reverse order. When processing labels, however, there are cases |
| 133 | // in the recursion where we may have already reversed the statements |
| 134 | // in a block. This method safely tidies up a block: if the block |
| 135 | // has a label at the front, it has already been reversed. Otherwise, |
| 136 | // we reverse it. |
| 137 | void FinishBlock(CFGBlock* B) { |
| 138 | assert (B); |
| 139 | CFGBlock::iterator I = B->begin(); |
| 140 | if (I != B->end()) { |
| 141 | Stmt* S = *I; |
| 142 | if (S->getStmtClass() != Stmt::LabelStmtClass) |
| 143 | B->reverseStmts(); |
| 144 | } |
| 145 | } |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 147 | /// Here we handle statements with no branching control flow. |
| 148 | CFGBlock* VisitStmt(Stmt* Statement) { |
| 149 | // We cannot assume that we are in the middle of a basic block, since |
| 150 | // the CFG might only be constructed for this single statement. If |
| 151 | // we have no current basic block, just create one lazily. |
| 152 | if (!Block) Block = createBlock(); |
| 153 | |
| 154 | // Simply add the statement to the current block. We actually |
| 155 | // insert statements in reverse order; this order is reversed later |
| 156 | // when processing the containing element in the AST. |
| 157 | Block->appendStmt(Statement); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 158 | |
| 159 | return Block; |
| 160 | } |
| 161 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 162 | CFGBlock* VisitNullStmt(NullStmt* Statement) { |
| 163 | return Block; |
| 164 | } |
| 165 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 166 | CFGBlock* VisitCompoundStmt(CompoundStmt* C) { |
| 167 | // The value returned from this function is the last created CFGBlock |
| 168 | // that represents the "entry" point for the translated AST node. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 169 | CFGBlock* LastBlock; |
| 170 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 171 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 172 | E = C->body_rend(); I != E; ++I ) |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 173 | // Add the statement to the current block. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 174 | if (!(LastBlock=Visit(*I))) |
| 175 | return NULL; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 177 | return LastBlock; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | CFGBlock* VisitIfStmt(IfStmt* I) { |
| 181 | |
| 182 | // We may see an if statement in the middle of a basic block, or |
| 183 | // it may be the first statement we are processing. In either case, |
| 184 | // we create a new basic block. First, we create the blocks for |
| 185 | // the then...else statements, and then we create the block containing |
| 186 | // the if statement. If we were in the middle of a block, we |
| 187 | // stop processing that block and reverse its statements. That block |
| 188 | // is then the implicit successor for the "then" and "else" clauses. |
| 189 | |
| 190 | // The block we were proccessing is now finished. Make it the |
| 191 | // successor block. |
| 192 | if (Block) { |
| 193 | Succ = Block; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 194 | FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Process the false branch. NULL out Block so that the recursive |
| 198 | // call to Visit will create a new basic block. |
| 199 | // Null out Block so that all successor |
| 200 | CFGBlock* ElseBlock = Succ; |
| 201 | |
| 202 | if (Stmt* Else = I->getElse()) { |
| 203 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 204 | |
| 205 | // NULL out Block so that the recursive call to Visit will |
| 206 | // create a new basic block. |
| 207 | Block = NULL; |
| 208 | ElseBlock = Visit(Else); |
| 209 | if (!ElseBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 210 | FinishBlock(ElseBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Process the true branch. NULL out Block so that the recursive |
| 214 | // call to Visit will create a new basic block. |
| 215 | // Null out Block so that all successor |
| 216 | CFGBlock* ThenBlock; |
| 217 | { |
| 218 | Stmt* Then = I->getThen(); |
| 219 | assert (Then); |
| 220 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 221 | Block = NULL; |
| 222 | ThenBlock = Visit(Then); |
| 223 | if (!ThenBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 224 | FinishBlock(ThenBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Now create a new block containing the if statement. |
| 228 | Block = createBlock(false); |
| 229 | |
| 230 | // Add the condition as the last statement in the new block. |
| 231 | Block->appendStmt(I->getCond()); |
| 232 | |
| 233 | // Set the terminator of the new block to the If statement. |
| 234 | Block->setTerminator(I); |
| 235 | |
| 236 | // Now add the successors. |
| 237 | Block->addSuccessor(ThenBlock); |
| 238 | Block->addSuccessor(ElseBlock); |
| 239 | |
| 240 | return Block; |
| 241 | } |
| 242 | |
| 243 | CFGBlock* VisitReturnStmt(ReturnStmt* R) { |
| 244 | // If we were in the middle of a block we stop processing that block |
| 245 | // and reverse its statements. |
| 246 | // |
| 247 | // NOTE: If a "return" appears in the middle of a block, this means |
| 248 | // that the code afterwards is DEAD (unreachable). We still |
| 249 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 250 | // from the entry block will be able to report such dead |
| 251 | // blocks. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 252 | if (Block) FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 253 | |
| 254 | // Create the new block. |
| 255 | Block = createBlock(false); |
| 256 | |
| 257 | // The Exit block is the only successor. |
| 258 | Block->addSuccessor(Exit); |
| 259 | |
| 260 | // Add the return expression to the block. |
| 261 | Block->appendStmt(R); |
| 262 | |
| 263 | // Add the return statement itself to the block. |
| 264 | if (R->getRetValue()) Block->appendStmt(R->getRetValue()); |
| 265 | |
| 266 | return Block; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | CFGBlock* VisitLabelStmt(LabelStmt* L) { |
| 270 | // Get the block of the labeled statement. Add it to our map. |
| 271 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 272 | assert (LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 273 | |
| 274 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 275 | LabelMap[ L ] = LabelBlock; |
| 276 | |
| 277 | // Labels partition blocks, so this is the end of the basic block |
| 278 | // we were processing (the label is the first statement). |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 279 | LabelBlock->appendStmt(L); |
| 280 | FinishBlock(LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 281 | |
| 282 | // We set Block to NULL to allow lazy creation of a new block |
| 283 | // (if necessary); |
| 284 | Block = NULL; |
| 285 | |
| 286 | // This block is now the implicit successor of other blocks. |
| 287 | Succ = LabelBlock; |
| 288 | |
| 289 | return LabelBlock; |
| 290 | } |
| 291 | |
| 292 | CFGBlock* VisitGotoStmt(GotoStmt* G) { |
| 293 | // Goto is a control-flow statement. Thus we stop processing the |
| 294 | // current block and create a new one. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 295 | if (Block) FinishBlock(Block); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 296 | Block = createBlock(false); |
| 297 | Block->setTerminator(G); |
| 298 | |
| 299 | // If we already know the mapping to the label block add the |
| 300 | // successor now. |
| 301 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 302 | |
| 303 | if (I == LabelMap.end()) |
| 304 | // We will need to backpatch this block later. |
| 305 | BackpatchBlocks.push_back(Block); |
| 306 | else |
| 307 | Block->addSuccessor(I->second); |
| 308 | |
| 309 | return Block; |
| 310 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 311 | |
| 312 | CFGBlock* VisitForStmt(ForStmt* F) { |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 313 | // "for" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 314 | // current block. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 316 | CFGBlock* LoopSuccessor = NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 318 | if (Block) { |
| 319 | FinishBlock(Block); |
| 320 | LoopSuccessor = Block; |
| 321 | } |
| 322 | else LoopSuccessor = Succ; |
| 323 | |
| 324 | // Create the condition block. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 325 | CFGBlock* ConditionBlock = createBlock(false); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 326 | ConditionBlock->setTerminator(F); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 327 | if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 328 | |
| 329 | // The condition block is the implicit successor for the loop body as |
| 330 | // well as any code above the loop. |
| 331 | Succ = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 332 | |
| 333 | // Now create the loop body. |
| 334 | { |
| 335 | assert (F->getBody()); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 337 | // Save the current values for Block, Succ, and continue and break targets |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 338 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 339 | save_continue(ContinueTargetBlock), |
| 340 | save_break(BreakTargetBlock); |
| 341 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 342 | // All continues within this loop should go to the condition block |
| 343 | ContinueTargetBlock = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 345 | // All breaks should go to the code following the loop. |
| 346 | BreakTargetBlock = LoopSuccessor; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 348 | // Create a new block to contain the (bottom) of the loop body. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 349 | Block = createBlock(); |
| 350 | |
| 351 | // If we have increment code, insert it at the end of the body block. |
| 352 | if (Stmt* I = F->getInc()) Block->appendStmt(I); |
| 353 | |
| 354 | // Now populate the body block, and in the process create new blocks |
| 355 | // as we walk the body of the loop. |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 356 | CFGBlock* BodyBlock = Visit(F->getBody()); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 357 | assert (BodyBlock); |
| 358 | FinishBlock(BodyBlock); |
| 359 | |
| 360 | // This new body block is a successor to our condition block. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 361 | ConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 362 | } |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 363 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 364 | // Link up the condition block with the code that follows the loop. |
| 365 | // (the false branch). |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 366 | ConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 368 | // If the loop contains initialization, create a new block for those |
| 369 | // statements. This block can also contain statements that precede |
| 370 | // the loop. |
| 371 | if (Stmt* I = F->getInit()) { |
| 372 | Block = createBlock(); |
| 373 | Block->appendStmt(I); |
| 374 | return Block; |
| 375 | } |
| 376 | else { |
| 377 | // There is no loop initialization. We are thus basically a while |
| 378 | // loop. NULL out Block to force lazy block construction. |
| 379 | Block = NULL; |
| 380 | return ConditionBlock; |
| 381 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 382 | } |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 383 | |
| 384 | CFGBlock* VisitWhileStmt(WhileStmt* W) { |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 385 | // "while" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 386 | // current block. |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 387 | |
| 388 | CFGBlock* LoopSuccessor = NULL; |
| 389 | |
| 390 | if (Block) { |
| 391 | FinishBlock(Block); |
| 392 | LoopSuccessor = Block; |
| 393 | } |
| 394 | else LoopSuccessor = Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 395 | |
| 396 | // Create the condition block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 397 | CFGBlock* ConditionBlock = createBlock(false); |
| 398 | ConditionBlock->setTerminator(W); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 399 | if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 401 | // The condition block is the implicit successor for the loop body as |
| 402 | // well as any code above the loop. |
| 403 | Succ = ConditionBlock; |
| 404 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 405 | // Process the loop body. |
| 406 | { |
| 407 | assert (W->getBody()); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 408 | |
| 409 | // Save the current values for Block, Succ, and continue and break targets |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 410 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 411 | save_continue(ContinueTargetBlock), |
| 412 | save_break(BreakTargetBlock); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 413 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 414 | // All continues within this loop should go to the condition block |
| 415 | ContinueTargetBlock = ConditionBlock; |
| 416 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 417 | // All breaks should go to the code following the loop. |
| 418 | BreakTargetBlock = LoopSuccessor; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 420 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 421 | Block = NULL; |
| 422 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 423 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 424 | CFGBlock* BodyBlock = Visit(W->getBody()); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 425 | assert (BodyBlock); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 426 | FinishBlock(BodyBlock); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 428 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 429 | ConditionBlock->addSuccessor(BodyBlock); |
| 430 | } |
| 431 | |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 432 | // Link up the condition block with the code that follows the loop. |
| 433 | // (the false branch). |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 434 | ConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 435 | |
| 436 | // There can be no more statements in the condition block |
| 437 | // since we loop back to this block. NULL out Block to force |
| 438 | // lazy creation of another block. |
| 439 | Block = NULL; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame^] | 441 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 442 | return ConditionBlock; |
| 443 | } |
| 444 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 445 | CFGBlock* VisitContinueStmt(ContinueStmt* C) { |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 446 | // "continue" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 447 | // current block. |
| 448 | if (Block) FinishBlock(Block); |
| 449 | |
| 450 | // Now create a new block that ends with the continue statement. |
| 451 | Block = createBlock(false); |
| 452 | Block->setTerminator(C); |
| 453 | |
| 454 | // FIXME: We should gracefully handle continues without resolved targets. |
| 455 | assert (ContinueTargetBlock); |
| 456 | |
| 457 | Block->addSuccessor(ContinueTargetBlock); |
| 458 | return Block; |
| 459 | } |
| 460 | |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 461 | CFGBlock* VisitBreakStmt(BreakStmt* B) { |
| 462 | // "break" is a control-flow statement. Thus we stop processing the |
| 463 | // current block. |
| 464 | if (Block) FinishBlock(Block); |
| 465 | |
| 466 | // Now create a new block that ends with the continue statement. |
| 467 | Block = createBlock(false); |
| 468 | Block->setTerminator(B); |
| 469 | |
| 470 | // FIXME: We should gracefully handle breaks without resolved targets. |
| 471 | assert (BreakTargetBlock); |
| 472 | |
| 473 | Block->addSuccessor(BreakTargetBlock); |
| 474 | return Block; |
| 475 | } |
| 476 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | // BuildCFG - A helper function that builds CFGs from ASTS. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 480 | CFG* CFG::BuildCFG(Stmt* Statement) { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 481 | CFGBuilder Builder; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 482 | return Builder.BuildCFG(Statement); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // reverseStmts - A method that reverses the order of the statements within |
| 486 | // a CFGBlock. |
| 487 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 488 | |
| 489 | // dump - A simple pretty printer of a CFG that outputs to stderr. |
| 490 | void CFG::dump() { print(std::cerr); } |
| 491 | |
| 492 | // print - A simple pretty printer of a CFG that outputs to an ostream. |
| 493 | void CFG::print(std::ostream& OS) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 494 | // First print out the Entry block, which may not be the first block |
| 495 | // in our list of blocks |
| 496 | if (begin() != end()) { |
| 497 | CFGBlock& Entry = getEntry(); |
| 498 | OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n"; |
| 499 | Entry.print(OS); |
| 500 | } |
| 501 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 502 | // Iterate through the CFGBlocks and print them one by one. Specially |
| 503 | // designate the Entry and Exit blocks. |
| 504 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 505 | // Skip the entry block, because we already printed it. |
| 506 | if (&(*I) == &getEntry()) |
| 507 | continue; |
| 508 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 509 | OS << "\n [ B" << I->getBlockID(); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 511 | if (&(*I) == &getExit()) OS << " (EXIT) ]\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 512 | else OS << " ]\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 514 | I->print(OS); |
| 515 | } |
| 516 | OS << "\n"; |
| 517 | } |
| 518 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 519 | |
| 520 | namespace { |
| 521 | |
| 522 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
| 523 | void > { |
| 524 | std::ostream& OS; |
| 525 | public: |
| 526 | CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {} |
| 527 | |
| 528 | void VisitIfStmt(IfStmt* I) { |
| 529 | OS << "if "; |
| 530 | I->getCond()->printPretty(std::cerr); |
| 531 | OS << "\n"; |
| 532 | } |
| 533 | |
| 534 | // Default case. |
| 535 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
| 536 | |
| 537 | void VisitForStmt(ForStmt* F) { |
| 538 | OS << "for (" ; |
| 539 | if (Stmt* I = F->getInit()) I->printPretty(OS); |
| 540 | OS << " ; "; |
| 541 | if (Stmt* C = F->getCond()) C->printPretty(OS); |
| 542 | OS << " ; "; |
| 543 | if (Stmt* I = F->getInc()) I->printPretty(OS); |
| 544 | OS << ")\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void VisitWhileStmt(WhileStmt* W) { |
| 548 | OS << "while " ; |
| 549 | if (Stmt* C = W->getCond()) C->printPretty(OS); |
| 550 | OS << "\n"; |
| 551 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 552 | }; |
| 553 | } |
| 554 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 555 | // dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
| 556 | void CFGBlock::dump() { print(std::cerr); } |
| 557 | |
| 558 | // print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 559 | // Generally this will only be called from CFG::print. |
| 560 | void CFGBlock::print(std::ostream& OS) { |
| 561 | |
| 562 | // Iterate through the statements in the block and print them. |
| 563 | OS << " ------------------------\n"; |
| 564 | unsigned j = 1; |
| 565 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 566 | // Print the statement # in the basic block. |
| 567 | OS << " " << std::setw(3) << j << ": "; |
| 568 | |
| 569 | // Print the statement/expression. |
| 570 | Stmt* S = *I; |
| 571 | |
| 572 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 573 | OS << L->getName() << ": (LABEL)\n"; |
| 574 | else |
| 575 | (*I)->printPretty(OS); |
| 576 | |
| 577 | // Expressions need a newline. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 578 | if (isa<Expr>(*I)) OS << '\n'; |
| 579 | } |
| 580 | OS << " ------------------------\n"; |
| 581 | |
| 582 | // Print the predecessors of this block. |
| 583 | OS << " Predecessors (" << pred_size() << "):"; |
| 584 | unsigned i = 0; |
| 585 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 586 | if (i == 8 || (i-8) == 0) { |
| 587 | OS << "\n "; |
| 588 | } |
| 589 | OS << " B" << (*I)->getBlockID(); |
| 590 | } |
| 591 | |
| 592 | // Print the terminator of this block. |
| 593 | OS << "\n Terminator: "; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 594 | if (ControlFlowStmt) |
| 595 | CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt); |
| 596 | else |
| 597 | OS << "<NULL>\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 598 | |
| 599 | // Print the successors of this block. |
| 600 | OS << " Successors (" << succ_size() << "):"; |
| 601 | i = 0; |
| 602 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 603 | if (i == 8 || (i-8) % 10 == 0) { |
| 604 | OS << "\n "; |
| 605 | } |
| 606 | OS << " B" << (*I)->getBlockID(); |
| 607 | } |
| 608 | OS << '\n'; |
| 609 | } |