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