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
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
35 #include "expr_plus.h"
37 #include "inlined_calls.h"
44 using namespace clang
;
46 pet_inlined_calls::~pet_inlined_calls()
48 vector
<pet_tree
*>::iterator it
;
49 map
<Stmt
*, isl_id
*>::iterator it_id
;
51 for (it
= inlined
.begin(); it
!= inlined
.end(); ++it
)
53 for (it_id
= call2id
.begin(); it_id
!= call2id
.end(); ++it_id
)
54 isl_id_free(it_id
->second
);
57 /* This method is called for each call expression "call"
58 * in an expression statement.
60 * If the corresponding function body is marked "inline", then add
63 * Return true to continue the traversal.
65 bool pet_inlined_calls::VisitCallExpr(clang::CallExpr
*call
)
67 FunctionDecl
*fd
= call
->getDirectCallee();
69 fd
= pet_clang_find_function_decl_with_body(fd
);
71 if (fd
&& fd
->isInlineSpecified())
72 calls
.push_back(call
);
77 /* Extract a pet_tree corresponding to the inlined function that
78 * is called by "call" and store it in this->inlined.
79 * If, moreover, the inlined function has a return value,
80 * then create a corresponding variable and store it in this->call2id.
82 * While extracting the inlined function, take into account
83 * the mapping from call expressions to return variables for
84 * previously extracted inlined functions in order to handle
87 void pet_inlined_calls::add(CallExpr
*call
)
89 FunctionDecl
*fd
= call
->getDirectCallee();
93 qt
= fd
->getReturnType();
94 if (!qt
->isVoidType()) {
95 id
= pet_id_ret_from_type(scan
->ctx
, scan
->n_ret
++, qt
);
96 call2id
[call
] = isl_id_copy(id
);
98 scan
->call2id
= &this->call2id
;
99 inlined
.push_back(scan
->extract_inlined_call(call
, fd
, id
));
100 scan
->call2id
= NULL
;
104 /* Collect all the call expressions in "stmt" that need to be inlined,
105 * the corresponding pet_tree objects and the variables that store
108 * The call expressions are first collected outermost to innermost.
109 * Then the corresponding inlined functions are extracted in reverse order
110 * to ensure that a nested call is performed before an outer call.
112 void pet_inlined_calls::collect(Stmt
*stmt
)
119 for (int i
= n
- 1; i
>= 0; --i
)
120 add(cast
<CallExpr
>(calls
[i
]));
123 /* Add the inlined call expressions to "tree", where "tree" corresponds
124 * to the original expression statement containing the calls, but with
125 * the calls replaced by accesses to the return variables in this->call2id.
126 * In particular, construct a new block containing declarations
127 * for the return variables in this->call2id, the inlined functions
128 * from innermost to outermost and finally "tree" itself.
130 __isl_give pet_tree
*pet_inlined_calls::add_inlined(__isl_take pet_tree
*tree
)
134 std::vector
<pet_tree
*>::iterator it_in
;
135 std::map
<Stmt
*, isl_id
*>::iterator it
;
140 n
= call2id
.size() + inlined
.size() + 1;
142 block
= pet_tree_new_block(scan
->ctx
, 1, n
);
143 for (it
= call2id
.begin(); it
!= call2id
.end(); ++it
) {
147 expr
= pet_expr_access_from_id(isl_id_copy(it
->second
),
149 decl
= pet_tree_new_decl(expr
);
150 block
= pet_tree_block_add_child(block
, decl
);
152 for (it_in
= inlined
.begin(); it_in
!= inlined
.end(); ++it_in
)
153 block
= pet_tree_block_add_child(block
, pet_tree_copy(*it_in
));
154 block
= pet_tree_block_add_child(block
, tree
);