support inlining of non-outermost call expressions
[pet.git] / inlined_calls.cc
blobc54991b03cb76c3de0a91cc3382fbad6f6eebe63
1 /*
2 * Copyright 2017 Sven Verdoolaege. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Sven Verdoolaege.
34 #include "clang.h"
35 #include "expr_plus.h"
36 #include "id.h"
37 #include "inlined_calls.h"
38 #include "scan.h"
39 #include "tree.h"
41 using namespace std;
42 using namespace clang;
44 pet_inlined_calls::~pet_inlined_calls()
46 vector<pet_tree *>::iterator it;
47 map<Stmt *, isl_id *>::iterator it_id;
49 for (it = inlined.begin(); it != inlined.end(); ++it)
50 pet_tree_free(*it);
51 for (it_id = call2id.begin(); it_id != call2id.end(); ++it_id)
52 isl_id_free(it_id->second);
55 /* This method is called for each call expression "call"
56 * in an expression statement.
58 * If the corresponding function body is marked "inline", then add
59 * it to this->calls.
61 * Return true to continue the traversal.
63 bool pet_inlined_calls::VisitCallExpr(clang::CallExpr *call)
65 FunctionDecl *fd = call->getDirectCallee();
67 fd = pet_clang_find_function_decl_with_body(fd);
69 if (fd && fd->isInlineSpecified())
70 calls.push_back(call);
72 return true;
75 /* Extract a pet_tree corresponding to the inlined function that
76 * is called by "call" and store it in this->inlined.
77 * If, moreover, the inlined function has a return value,
78 * then create a corresponding variable and store it in this->call2id.
80 * While extracting the inlined function, take into account
81 * the mapping from call expressions to return variables for
82 * previously extracted inlined functions in order to handle
83 * nested calls.
85 void pet_inlined_calls::add(CallExpr *call)
87 FunctionDecl *fd = call->getDirectCallee();
88 QualType qt;
89 isl_id *id = NULL;
91 qt = fd->getReturnType();
92 if (!qt->isVoidType()) {
93 id = pet_id_ret_from_type(scan->ctx, scan->n_ret++, qt);
94 call2id[call] = isl_id_copy(id);
96 scan->call2id = &this->call2id;
97 inlined.push_back(scan->extract_inlined_call(call, fd, id));
98 scan->call2id = NULL;
99 isl_id_free(id);
102 /* Collect all the call expressions in "stmt" that need to be inlined,
103 * the corresponding pet_tree objects and the variables that store
104 * the return values.
106 * The call expressions are first collected outermost to innermost.
107 * Then the corresponding inlined functions are extracted in reverse order
108 * to ensure that a nested call is performed before an outer call.
110 void pet_inlined_calls::collect(Stmt *stmt)
112 int n;
114 TraverseStmt(stmt);
116 n = calls.size();
117 for (int i = n - 1; i >= 0; --i)
118 add(cast<CallExpr>(calls[i]));
121 /* Add the inlined call expressions to "tree", where "tree" corresponds
122 * to the original expression statement containing the calls, but with
123 * the calls replaced by accesses to the return variables in this->call2id.
124 * In particular, construct a new block containing declarations
125 * for the return variables in this->call2id, the inlined functions
126 * from innermost to outermost and finally "tree" itself.
128 __isl_give pet_tree *pet_inlined_calls::add_inlined(__isl_take pet_tree *tree)
130 pet_tree *block;
131 int n;
132 std::vector<pet_tree *>::iterator it_in;
133 std::map<Stmt *, isl_id *>::iterator it;
135 if (inlined.empty())
136 return tree;
138 n = call2id.size() + inlined.size() + 1;
140 block = pet_tree_new_block(scan->ctx, 1, n);
141 for (it = call2id.begin(); it != call2id.end(); ++it) {
142 pet_expr *expr;
143 pet_tree *decl;
145 expr = pet_expr_access_from_id(isl_id_copy(it->second),
146 scan->ast_context);
147 decl = pet_tree_new_decl(expr);
148 block = pet_tree_block_add_child(block, decl);
150 for (it_in = inlined.begin(); it_in != inlined.end(); ++it_in)
151 block = pet_tree_block_add_child(block, pet_tree_copy(*it_in));
152 block = pet_tree_block_add_child(block, tree);
154 return block;