Bug Summary

File:polly/lib/CodeGen/IslAst.cpp
Warning:line 699, column 3
Potential leak of memory pointed to by field '_M_head_impl'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name IslAst.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/tools/polly/lib -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/tools/polly/lib -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/tools/polly/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/External -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-long-long -Wno-unused-parameter -Wwrite-strings -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/build-llvm/tools/polly/lib -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2021-03-08-182450-10039-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp

/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp

1//===- IslAst.cpp - isl code generator interface --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The isl code generator interface takes a Scop and generates an isl_ast. This
10// ist_ast can either be returned directly or it can be pretty printed to
11// stdout.
12//
13// A typical isl_ast output looks like this:
14//
15// for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) {
16// bb2(c2);
17// }
18//
19// An in-depth discussion of our AST generation approach can be found in:
20//
21// Polyhedral AST generation is more than scanning polyhedra
22// Tobias Grosser, Sven Verdoolaege, Albert Cohen
23// ACM Transactions on Programming Languages and Systems (TOPLAS),
24// 37(4), July 2015
25// http://www.grosser.es/#pub-polyhedral-AST-generation
26//
27//===----------------------------------------------------------------------===//
28
29#include "polly/CodeGen/IslAst.h"
30#include "polly/CodeGen/CodeGeneration.h"
31#include "polly/DependenceInfo.h"
32#include "polly/LinkAllPasses.h"
33#include "polly/Options.h"
34#include "polly/ScopDetection.h"
35#include "polly/ScopInfo.h"
36#include "polly/ScopPass.h"
37#include "polly/Support/GICHelper.h"
38#include "llvm/ADT/Statistic.h"
39#include "llvm/IR/Function.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/raw_ostream.h"
42#include "isl/aff.h"
43#include "isl/ast.h"
44#include "isl/ast_build.h"
45#include "isl/id.h"
46#include "isl/isl-noexceptions.h"
47#include "isl/printer.h"
48#include "isl/schedule.h"
49#include "isl/set.h"
50#include "isl/union_map.h"
51#include "isl/val.h"
52#include <cassert>
53#include <cstdlib>
54
55#define DEBUG_TYPE"polly-ast" "polly-ast"
56
57using namespace llvm;
58using namespace polly;
59
60using IslAstUserPayload = IslAstInfo::IslAstUserPayload;
61
62static cl::opt<bool>
63 PollyParallel("polly-parallel",
64 cl::desc("Generate thread parallel code (isl codegen only)"),
65 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
66
67static cl::opt<bool> PrintAccesses("polly-ast-print-accesses",
68 cl::desc("Print memory access functions"),
69 cl::init(false), cl::ZeroOrMore,
70 cl::cat(PollyCategory));
71
72static cl::opt<bool> PollyParallelForce(
73 "polly-parallel-force",
74 cl::desc(
75 "Force generation of thread parallel code ignoring any cost model"),
76 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
77
78static cl::opt<bool> UseContext("polly-ast-use-context",
79 cl::desc("Use context"), cl::Hidden,
80 cl::init(true), cl::ZeroOrMore,
81 cl::cat(PollyCategory));
82
83static cl::opt<bool> DetectParallel("polly-ast-detect-parallel",
84 cl::desc("Detect parallelism"), cl::Hidden,
85 cl::init(false), cl::ZeroOrMore,
86 cl::cat(PollyCategory));
87
88STATISTIC(ScopsProcessed, "Number of SCoPs processed")static llvm::Statistic ScopsProcessed = {"polly-ast", "ScopsProcessed"
, "Number of SCoPs processed"}
;
89STATISTIC(ScopsBeneficial, "Number of beneficial SCoPs")static llvm::Statistic ScopsBeneficial = {"polly-ast", "ScopsBeneficial"
, "Number of beneficial SCoPs"}
;
90STATISTIC(BeneficialAffineLoops, "Number of beneficial affine loops")static llvm::Statistic BeneficialAffineLoops = {"polly-ast", "BeneficialAffineLoops"
, "Number of beneficial affine loops"}
;
91STATISTIC(BeneficialBoxedLoops, "Number of beneficial boxed loops")static llvm::Statistic BeneficialBoxedLoops = {"polly-ast", "BeneficialBoxedLoops"
, "Number of beneficial boxed loops"}
;
92
93STATISTIC(NumForLoops, "Number of for-loops")static llvm::Statistic NumForLoops = {"polly-ast", "NumForLoops"
, "Number of for-loops"}
;
94STATISTIC(NumParallel, "Number of parallel for-loops")static llvm::Statistic NumParallel = {"polly-ast", "NumParallel"
, "Number of parallel for-loops"}
;
95STATISTIC(NumInnermostParallel, "Number of innermost parallel for-loops")static llvm::Statistic NumInnermostParallel = {"polly-ast", "NumInnermostParallel"
, "Number of innermost parallel for-loops"}
;
96STATISTIC(NumOutermostParallel, "Number of outermost parallel for-loops")static llvm::Statistic NumOutermostParallel = {"polly-ast", "NumOutermostParallel"
, "Number of outermost parallel for-loops"}
;
97STATISTIC(NumReductionParallel, "Number of reduction-parallel for-loops")static llvm::Statistic NumReductionParallel = {"polly-ast", "NumReductionParallel"
, "Number of reduction-parallel for-loops"}
;
98STATISTIC(NumExecutedInParallel, "Number of for-loops executed in parallel")static llvm::Statistic NumExecutedInParallel = {"polly-ast", "NumExecutedInParallel"
, "Number of for-loops executed in parallel"}
;
99STATISTIC(NumIfConditions, "Number of if-conditions")static llvm::Statistic NumIfConditions = {"polly-ast", "NumIfConditions"
, "Number of if-conditions"}
;
100
101namespace polly {
102
103/// Temporary information used when building the ast.
104struct AstBuildUserInfo {
105 /// Construct and initialize the helper struct for AST creation.
106 AstBuildUserInfo() = default;
107
108 /// The dependence information used for the parallelism check.
109 const Dependences *Deps = nullptr;
110
111 /// Flag to indicate that we are inside a parallel for node.
112 bool InParallelFor = false;
113
114 /// Flag to indicate that we are inside an SIMD node.
115 bool InSIMD = false;
116
117 /// The last iterator id created for the current SCoP.
118 isl_id *LastForNodeId = nullptr;
119};
120} // namespace polly
121
122/// Free an IslAstUserPayload object pointed to by @p Ptr.
123static void freeIslAstUserPayload(void *Ptr) {
124 delete ((IslAstInfo::IslAstUserPayload *)Ptr);
125}
126
127IslAstInfo::IslAstUserPayload::~IslAstUserPayload() {
128 isl_ast_build_free(Build);
129}
130
131/// Print a string @p str in a single line using @p Printer.
132static isl_printer *printLine(__isl_take isl_printer *Printer,
133 const std::string &str,
134 __isl_keep isl_pw_aff *PWA = nullptr) {
135 Printer = isl_printer_start_line(Printer);
136 Printer = isl_printer_print_str(Printer, str.c_str());
137 if (PWA)
138 Printer = isl_printer_print_pw_aff(Printer, PWA);
139 return isl_printer_end_line(Printer);
140}
141
142/// Return all broken reductions as a string of clauses (OpenMP style).
143static const std::string getBrokenReductionsStr(__isl_keep isl_ast_node *Node) {
144 IslAstInfo::MemoryAccessSet *BrokenReductions;
145 std::string str;
146
147 BrokenReductions = IslAstInfo::getBrokenReductions(Node);
148 if (!BrokenReductions || BrokenReductions->empty())
149 return "";
150
151 // Map each type of reduction to a comma separated list of the base addresses.
152 std::map<MemoryAccess::ReductionType, std::string> Clauses;
153 for (MemoryAccess *MA : *BrokenReductions)
154 if (MA->isWrite())
155 Clauses[MA->getReductionType()] +=
156 ", " + MA->getScopArrayInfo()->getName();
157
158 // Now print the reductions sorted by type. Each type will cause a clause
159 // like: reduction (+ : sum0, sum1, sum2)
160 for (const auto &ReductionClause : Clauses) {
161 str += " reduction (";
162 str += MemoryAccess::getReductionOperatorStr(ReductionClause.first);
163 // Remove the first two symbols (", ") to make the output look pretty.
164 str += " : " + ReductionClause.second.substr(2) + ")";
165 }
166
167 return str;
168}
169
170/// Callback executed for each for node in the ast in order to print it.
171static isl_printer *cbPrintFor(__isl_take isl_printer *Printer,
172 __isl_take isl_ast_print_options *Options,
173 __isl_keep isl_ast_node *Node, void *) {
174 isl_pw_aff *DD = IslAstInfo::getMinimalDependenceDistance(Node);
175 const std::string BrokenReductionsStr = getBrokenReductionsStr(Node);
176 const std::string KnownParallelStr = "#pragma known-parallel";
177 const std::string DepDisPragmaStr = "#pragma minimal dependence distance: ";
178 const std::string SimdPragmaStr = "#pragma simd";
179 const std::string OmpPragmaStr = "#pragma omp parallel for";
180
181 if (DD)
182 Printer = printLine(Printer, DepDisPragmaStr, DD);
183
184 if (IslAstInfo::isInnermostParallel(isl::manage_copy(Node)))
185 Printer = printLine(Printer, SimdPragmaStr + BrokenReductionsStr);
186
187 if (IslAstInfo::isExecutedInParallel(Node))
188 Printer = printLine(Printer, OmpPragmaStr);
189 else if (IslAstInfo::isOutermostParallel(isl::manage_copy(Node)))
190 Printer = printLine(Printer, KnownParallelStr + BrokenReductionsStr);
191
192 isl_pw_aff_free(DD);
193 return isl_ast_node_for_print(Node, Printer, Options);
194}
195
196/// Check if the current scheduling dimension is parallel.
197///
198/// In case the dimension is parallel we also check if any reduction
199/// dependences is broken when we exploit this parallelism. If so,
200/// @p IsReductionParallel will be set to true. The reduction dependences we use
201/// to check are actually the union of the transitive closure of the initial
202/// reduction dependences together with their reversal. Even though these
203/// dependences connect all iterations with each other (thus they are cyclic)
204/// we can perform the parallelism check as we are only interested in a zero
205/// (or non-zero) dependence distance on the dimension in question.
206static bool astScheduleDimIsParallel(__isl_keep isl_ast_build *Build,
207 const Dependences *D,
208 IslAstUserPayload *NodeInfo) {
209 if (!D->hasValidDependences())
210 return false;
211
212 isl_union_map *Schedule = isl_ast_build_get_schedule(Build);
213 isl_union_map *Deps =
214 D->getDependences(Dependences::TYPE_RAW | Dependences::TYPE_WAW |
215 Dependences::TYPE_WAR)
216 .release();
217
218 if (!D->isParallel(Schedule, Deps)) {
219 isl_union_map *DepsAll =
220 D->getDependences(Dependences::TYPE_RAW | Dependences::TYPE_WAW |
221 Dependences::TYPE_WAR | Dependences::TYPE_TC_RED)
222 .release();
223 isl_pw_aff *MinimalDependenceDistance = nullptr;
224 D->isParallel(Schedule, DepsAll, &MinimalDependenceDistance);
225 NodeInfo->MinimalDependenceDistance =
226 isl::manage(MinimalDependenceDistance);
227 isl_union_map_free(Schedule);
228 return false;
229 }
230
231 isl_union_map *RedDeps =
232 D->getDependences(Dependences::TYPE_TC_RED).release();
233 if (!D->isParallel(Schedule, RedDeps))
234 NodeInfo->IsReductionParallel = true;
235
236 if (!NodeInfo->IsReductionParallel && !isl_union_map_free(Schedule))
237 return true;
238
239 // Annotate reduction parallel nodes with the memory accesses which caused the
240 // reduction dependences parallel execution of the node conflicts with.
241 for (const auto &MaRedPair : D->getReductionDependences()) {
242 if (!MaRedPair.second)
243 continue;
244 RedDeps = isl_union_map_from_map(isl_map_copy(MaRedPair.second));
245 if (!D->isParallel(Schedule, RedDeps))
246 NodeInfo->BrokenReductions.insert(MaRedPair.first);
247 }
248
249 isl_union_map_free(Schedule);
250 return true;
251}
252
253// This method is executed before the construction of a for node. It creates
254// an isl_id that is used to annotate the subsequently generated ast for nodes.
255//
256// In this function we also run the following analyses:
257//
258// - Detection of openmp parallel loops
259//
260static __isl_give isl_id *astBuildBeforeFor(__isl_keep isl_ast_build *Build,
261 void *User) {
262 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
263 IslAstUserPayload *Payload = new IslAstUserPayload();
264 isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
265 Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
266 BuildInfo->LastForNodeId = Id;
267
268 Payload->IsParallel =
269 astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
270
271 // Test for parallelism only if we are not already inside a parallel loop
272 if (!BuildInfo->InParallelFor && !BuildInfo->InSIMD)
273 BuildInfo->InParallelFor = Payload->IsOutermostParallel =
274 Payload->IsParallel;
275
276 return Id;
277}
278
279// This method is executed after the construction of a for node.
280//
281// It performs the following actions:
282//
283// - Reset the 'InParallelFor' flag, as soon as we leave a for node,
284// that is marked as openmp parallel.
285//
286static __isl_give isl_ast_node *
287astBuildAfterFor(__isl_take isl_ast_node *Node, __isl_keep isl_ast_build *Build,
288 void *User) {
289 isl_id *Id = isl_ast_node_get_annotation(Node);
290 assert(Id && "Post order visit assumes annotated for nodes")((Id && "Post order visit assumes annotated for nodes"
) ? static_cast<void> (0) : __assert_fail ("Id && \"Post order visit assumes annotated for nodes\""
, "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 290, __PRETTY_FUNCTION__))
;
291 IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
292 assert(Payload && "Post order visit assumes annotated for nodes")((Payload && "Post order visit assumes annotated for nodes"
) ? static_cast<void> (0) : __assert_fail ("Payload && \"Post order visit assumes annotated for nodes\""
, "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 292, __PRETTY_FUNCTION__))
;
293
294 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
295 assert(!Payload->Build && "Build environment already set")((!Payload->Build && "Build environment already set"
) ? static_cast<void> (0) : __assert_fail ("!Payload->Build && \"Build environment already set\""
, "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 295, __PRETTY_FUNCTION__))
;
296 Payload->Build = isl_ast_build_copy(Build);
297 Payload->IsInnermost = (Id == BuildInfo->LastForNodeId);
298
299 Payload->IsInnermostParallel =
300 Payload->IsInnermost && (BuildInfo->InSIMD || Payload->IsParallel);
301 if (Payload->IsOutermostParallel)
302 BuildInfo->InParallelFor = false;
303
304 isl_id_free(Id);
305 return Node;
306}
307
308static isl_stat astBuildBeforeMark(__isl_keep isl_id *MarkId,
309 __isl_keep isl_ast_build *Build,
310 void *User) {
311 if (!MarkId)
312 return isl_stat_error;
313
314 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
315 if (strcmp(isl_id_get_name(MarkId), "SIMD") == 0)
316 BuildInfo->InSIMD = true;
317
318 return isl_stat_ok;
319}
320
321static __isl_give isl_ast_node *
322astBuildAfterMark(__isl_take isl_ast_node *Node,
323 __isl_keep isl_ast_build *Build, void *User) {
324 assert(isl_ast_node_get_type(Node) == isl_ast_node_mark)((isl_ast_node_get_type(Node) == isl_ast_node_mark) ? static_cast
<void> (0) : __assert_fail ("isl_ast_node_get_type(Node) == isl_ast_node_mark"
, "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 324, __PRETTY_FUNCTION__))
;
325 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
326 auto *Id = isl_ast_node_mark_get_id(Node);
327 if (strcmp(isl_id_get_name(Id), "SIMD") == 0)
328 BuildInfo->InSIMD = false;
329 isl_id_free(Id);
330 return Node;
331}
332
333static __isl_give isl_ast_node *AtEachDomain(__isl_take isl_ast_node *Node,
334 __isl_keep isl_ast_build *Build,
335 void *User) {
336 assert(!isl_ast_node_get_annotation(Node) && "Node already annotated")((!isl_ast_node_get_annotation(Node) && "Node already annotated"
) ? static_cast<void> (0) : __assert_fail ("!isl_ast_node_get_annotation(Node) && \"Node already annotated\""
, "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 336, __PRETTY_FUNCTION__))
;
337
338 IslAstUserPayload *Payload = new IslAstUserPayload();
339 isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
340 Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
341
342 Payload->Build = isl_ast_build_copy(Build);
343
344 return isl_ast_node_set_annotation(Node, Id);
345}
346
347// Build alias check condition given a pair of minimal/maximal access.
348static isl::ast_expr buildCondition(Scop &S, isl::ast_build Build,
349 const Scop::MinMaxAccessTy *It0,
350 const Scop::MinMaxAccessTy *It1) {
351
352 isl::pw_multi_aff AFirst = It0->first;
353 isl::pw_multi_aff ASecond = It0->second;
354 isl::pw_multi_aff BFirst = It1->first;
355 isl::pw_multi_aff BSecond = It1->second;
356
357 isl::id Left = AFirst.get_tuple_id(isl::dim::set);
358 isl::id Right = BFirst.get_tuple_id(isl::dim::set);
359
360 isl::ast_expr True =
361 isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 1));
362 isl::ast_expr False =
363 isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 0));
364
365 const ScopArrayInfo *BaseLeft =
366 ScopArrayInfo::getFromId(Left)->getBasePtrOriginSAI();
367 const ScopArrayInfo *BaseRight =
368 ScopArrayInfo::getFromId(Right)->getBasePtrOriginSAI();
369 if (BaseLeft && BaseLeft == BaseRight)
370 return True;
371
372 isl::set Params = S.getContext();
373
374 isl::ast_expr NonAliasGroup, MinExpr, MaxExpr;
375
376 // In the following, we first check if any accesses will be empty under
377 // the execution context of the scop and do not code generate them if this
378 // is the case as isl will fail to derive valid AST expressions for such
379 // accesses.
380
381 if (!AFirst.intersect_params(Params).domain().is_empty() &&
382 !BSecond.intersect_params(Params).domain().is_empty()) {
383 MinExpr = Build.access_from(AFirst).address_of();
384 MaxExpr = Build.access_from(BSecond).address_of();
385 NonAliasGroup = MaxExpr.le(MinExpr);
386 }
387
388 if (!BFirst.intersect_params(Params).domain().is_empty() &&
389 !ASecond.intersect_params(Params).domain().is_empty()) {
390 MinExpr = Build.access_from(BFirst).address_of();
391 MaxExpr = Build.access_from(ASecond).address_of();
392
393 isl::ast_expr Result = MaxExpr.le(MinExpr);
394 if (!NonAliasGroup.is_null())
395 NonAliasGroup = isl::manage(
396 isl_ast_expr_or(NonAliasGroup.release(), Result.release()));
397 else
398 NonAliasGroup = Result;
399 }
400
401 if (NonAliasGroup.is_null())
402 NonAliasGroup = True;
403
404 return NonAliasGroup;
405}
406
407__isl_give isl_ast_expr *
408IslAst::buildRunCondition(Scop &S, __isl_keep isl_ast_build *Build) {
409 isl_ast_expr *RunCondition;
410
411 // The conditions that need to be checked at run-time for this scop are
412 // available as an isl_set in the runtime check context from which we can
413 // directly derive a run-time condition.
414 auto *PosCond =
415 isl_ast_build_expr_from_set(Build, S.getAssumedContext().release());
416 if (S.hasTrivialInvalidContext()) {
417 RunCondition = PosCond;
418 } else {
419 auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build));
420 auto *NegCond =
421 isl_ast_build_expr_from_set(Build, S.getInvalidContext().release());
422 auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond);
423 RunCondition = isl_ast_expr_and(PosCond, NotNegCond);
424 }
425
426 // Create the alias checks from the minimal/maximal accesses in each alias
427 // group which consists of read only and non read only (read write) accesses.
428 // This operation is by construction quadratic in the read-write pointers and
429 // linear in the read only pointers in each alias group.
430 for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S.getAliasGroups()) {
431 auto &MinMaxReadWrite = MinMaxAccessPair.first;
432 auto &MinMaxReadOnly = MinMaxAccessPair.second;
433 auto RWAccEnd = MinMaxReadWrite.end();
434
435 for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd;
436 ++RWAccIt0) {
437 for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1)
438 RunCondition = isl_ast_expr_and(
439 RunCondition,
440 buildCondition(S, isl::manage_copy(Build), RWAccIt0, RWAccIt1)
441 .release());
442 for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly)
443 RunCondition = isl_ast_expr_and(
444 RunCondition,
445 buildCondition(S, isl::manage_copy(Build), RWAccIt0, &ROAccIt)
446 .release());
447 }
448 }
449
450 return RunCondition;
451}
452
453/// Simple cost analysis for a given SCoP.
454///
455/// TODO: Improve this analysis and extract it to make it usable in other
456/// places too.
457/// In order to improve the cost model we could either keep track of
458/// performed optimizations (e.g., tiling) or compute properties on the
459/// original as well as optimized SCoP (e.g., #stride-one-accesses).
460static bool benefitsFromPolly(Scop &Scop, bool PerformParallelTest) {
461 if (PollyProcessUnprofitable)
462 return true;
463
464 // Check if nothing interesting happened.
465 if (!PerformParallelTest && !Scop.isOptimized() &&
466 Scop.getAliasGroups().empty())
467 return false;
468
469 // The default assumption is that Polly improves the code.
470 return true;
471}
472
473/// Collect statistics for the syntax tree rooted at @p Ast.
474static void walkAstForStatistics(__isl_keep isl_ast_node *Ast) {
475 assert(Ast)((Ast) ? static_cast<void> (0) : __assert_fail ("Ast", "/build/llvm-toolchain-snapshot-13~++20210308111132+66e3a4abe99c/polly/lib/CodeGen/IslAst.cpp"
, 475, __PRETTY_FUNCTION__))
;
476 isl_ast_node_foreach_descendant_top_down(
477 Ast,
478 [](__isl_keep isl_ast_node *Node, void *User) -> isl_bool {
479 switch (isl_ast_node_get_type(Node)) {
480 case isl_ast_node_for:
481 NumForLoops++;
482 if (IslAstInfo::isParallel(Node))
483 NumParallel++;
484 if (IslAstInfo::isInnermostParallel(isl::manage_copy(Node)))
485 NumInnermostParallel++;
486 if (IslAstInfo::isOutermostParallel(isl::manage_copy(Node)))
487 NumOutermostParallel++;
488 if (IslAstInfo::isReductionParallel(Node))
489 NumReductionParallel++;
490 if (IslAstInfo::isExecutedInParallel(Node))
491 NumExecutedInParallel++;
492 break;
493
494 case isl_ast_node_if:
495 NumIfConditions++;
496 break;
497
498 default:
499 break;
500 }
501
502 // Continue traversing subtrees.
503 return isl_bool_true;
504 },
505 nullptr);
506}
507
508IslAst::IslAst(Scop &Scop) : S(Scop), Ctx(Scop.getSharedIslCtx()) {}
509
510IslAst::IslAst(IslAst &&O)
511 : S(O.S), Root(O.Root), RunCondition(O.RunCondition), Ctx(O.Ctx) {
512 O.Root = nullptr;
513 O.RunCondition = nullptr;
514}
515
516IslAst::~IslAst() {
517 isl_ast_node_free(Root);
518 isl_ast_expr_free(RunCondition);
519}
520
521void IslAst::init(const Dependences &D) {
522 bool PerformParallelTest = PollyParallel || DetectParallel ||
523 PollyVectorizerChoice != VECTORIZER_NONE;
524 auto ScheduleTree = S.getScheduleTree();
525
526 // Skip AST and code generation if there was no benefit achieved.
527 if (!benefitsFromPolly(S, PerformParallelTest))
528 return;
529
530 auto ScopStats = S.getStatistics();
531 ScopsBeneficial++;
532 BeneficialAffineLoops += ScopStats.NumAffineLoops;
533 BeneficialBoxedLoops += ScopStats.NumBoxedLoops;
534
535 auto Ctx = S.getIslCtx();
536 isl_options_set_ast_build_atomic_upper_bound(Ctx.get(), true);
537 isl_options_set_ast_build_detect_min_max(Ctx.get(), true);
538 isl_ast_build *Build;
539 AstBuildUserInfo BuildInfo;
540
541 if (UseContext)
542 Build = isl_ast_build_from_context(S.getContext().release());
543 else
544 Build = isl_ast_build_from_context(
545 isl_set_universe(S.getParamSpace().release()));
546
547 Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr);
548
549 if (PerformParallelTest) {
550 BuildInfo.Deps = &D;
551 BuildInfo.InParallelFor = false;
552 BuildInfo.InSIMD = false;
553
554 Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor,
555 &BuildInfo);
556 Build =
557 isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo);
558
559 Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark,
560 &BuildInfo);
561
562 Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark,
563 &BuildInfo);
564 }
565
566 RunCondition = buildRunCondition(S, Build);
567
568 Root = isl_ast_build_node_from_schedule(Build, S.getScheduleTree().release());
569 walkAstForStatistics(Root);
570
571 isl_ast_build_free(Build);
572}
573
574IslAst IslAst::create(Scop &Scop, const Dependences &D) {
575 IslAst Ast{Scop};
576 Ast.init(D);
577 return Ast;
578}
579
580__isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); }
581__isl_give isl_ast_expr *IslAst::getRunCondition() {
582 return isl_ast_expr_copy(RunCondition);
583}
584
585__isl_give isl_ast_node *IslAstInfo::getAst() { return Ast.getAst(); }
586__isl_give isl_ast_expr *IslAstInfo::getRunCondition() {
587 return Ast.getRunCondition();
588}
589
590IslAstUserPayload *IslAstInfo::getNodePayload(const isl::ast_node &Node) {
591 isl::id Id = Node.get_annotation();
592 if (!Id)
593 return nullptr;
594 IslAstUserPayload *Payload = (IslAstUserPayload *)Id.get_user();
595 return Payload;
596}
597
598bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) {
599 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
600 return Payload && Payload->IsInnermost;
601}
602
603bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) {
604 return IslAstInfo::isInnermostParallel(isl::manage_copy(Node)) ||
605 IslAstInfo::isOutermostParallel(isl::manage_copy(Node));
606}
607
608bool IslAstInfo::isInnermostParallel(const isl::ast_node &Node) {
609 IslAstUserPayload *Payload = getNodePayload(Node);
610 return Payload && Payload->IsInnermostParallel;
611}
612
613bool IslAstInfo::isOutermostParallel(const isl::ast_node &Node) {
614 IslAstUserPayload *Payload = getNodePayload(Node);
615 return Payload && Payload->IsOutermostParallel;
616}
617
618bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) {
619 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
620 return Payload && Payload->IsReductionParallel;
621}
622
623bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) {
624 if (!PollyParallel)
625 return false;
626
627 // Do not parallelize innermost loops.
628 //
629 // Parallelizing innermost loops is often not profitable, especially if
630 // they have a low number of iterations.
631 //
632 // TODO: Decide this based on the number of loop iterations that will be
633 // executed. This can possibly require run-time checks, which again
634 // raises the question of both run-time check overhead and code size
635 // costs.
636 if (!PollyParallelForce && isInnermost(Node))
637 return false;
638
639 return isOutermostParallel(isl::manage_copy(Node)) &&
640 !isReductionParallel(Node);
641}
642
643__isl_give isl_union_map *
644IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) {
645 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
646 return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr;
647}
648
649__isl_give isl_pw_aff *
650IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) {
651 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
652 return Payload ? Payload->MinimalDependenceDistance.copy() : nullptr;
653}
654
655IslAstInfo::MemoryAccessSet *
656IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) {
657 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
658 return Payload ? &Payload->BrokenReductions : nullptr;
659}
660
661isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) {
662 IslAstUserPayload *Payload = getNodePayload(isl::manage_copy(Node));
663 return Payload ? Payload->Build : nullptr;
664}
665
666static std::unique_ptr<IslAstInfo> runIslAst(
667 Scop &Scop,
668 function_ref<const Dependences &(Dependences::AnalysisLevel)> GetDeps) {
669 // Skip SCoPs in case they're already handled by PPCGCodeGeneration.
670 if (Scop.isToBeSkipped())
2
Assuming the condition is false
3
Taking false branch
671 return {};
672
673 ScopsProcessed++;
674
675 const Dependences &D = GetDeps(Dependences::AL_Statement);
676
677 if (D.getSharedIslCtx() != Scop.getSharedIslCtx()) {
4
Taking false branch
678 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { dbgs() << "Got dependence analysis for different SCoP/isl_ctx\n"
; } } while (false)
679 dbgs() << "Got dependence analysis for different SCoP/isl_ctx\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { dbgs() << "Got dependence analysis for different SCoP/isl_ctx\n"
; } } while (false)
;
680 return {};
681 }
682
683 std::unique_ptr<IslAstInfo> Ast = std::make_unique<IslAstInfo>(Scop, D);
5
Calling 'make_unique<polly::IslAstInfo, polly::Scop &, const polly::Dependences &>'
7
Returned allocated memory
684
685 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { if (Ast) Ast->print(dbgs()); }; } } while
(false)
8
Assuming 'DebugFlag' is false
9
Loop condition is false. Exiting loop
686 if (Ast)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { if (Ast) Ast->print(dbgs()); }; } } while
(false)
687 Ast->print(dbgs());do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { if (Ast) Ast->print(dbgs()); }; } } while
(false)
688 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { if (Ast) Ast->print(dbgs()); }; } } while
(false)
;
689
690 return Ast;
691}
692
693IslAstInfo IslAstAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
694 ScopStandardAnalysisResults &SAR) {
695 auto GetDeps = [&](Dependences::AnalysisLevel Lvl) -> const Dependences & {
696 return SAM.getResult<DependenceAnalysis>(S, SAR).getDependences(Lvl);
697 };
698
699 return std::move(*runIslAst(S, GetDeps).release());
1
Calling 'runIslAst'
10
Returned allocated memory
11
Potential leak of memory pointed to by field '_M_head_impl'
700}
701
702static __isl_give isl_printer *cbPrintUser(__isl_take isl_printer *P,
703 __isl_take isl_ast_print_options *O,
704 __isl_keep isl_ast_node *Node,
705 void *User) {
706 isl::ast_node AstNode = isl::manage_copy(Node);
707 isl::ast_expr NodeExpr = AstNode.user_get_expr();
708 isl::ast_expr CallExpr = NodeExpr.get_op_arg(0);
709 isl::id CallExprId = CallExpr.get_id();
710 ScopStmt *AccessStmt = (ScopStmt *)CallExprId.get_user();
711
712 P = isl_printer_start_line(P);
713 P = isl_printer_print_str(P, AccessStmt->getBaseName());
714 P = isl_printer_print_str(P, "(");
715 P = isl_printer_end_line(P);
716 P = isl_printer_indent(P, 2);
717
718 for (MemoryAccess *MemAcc : *AccessStmt) {
719 P = isl_printer_start_line(P);
720
721 if (MemAcc->isRead())
722 P = isl_printer_print_str(P, "/* read */ &");
723 else
724 P = isl_printer_print_str(P, "/* write */ ");
725
726 isl::ast_build Build = isl::manage_copy(IslAstInfo::getBuild(Node));
727 if (MemAcc->isAffine()) {
728 isl_pw_multi_aff *PwmaPtr =
729 MemAcc->applyScheduleToAccessRelation(Build.get_schedule()).release();
730 isl::pw_multi_aff Pwma = isl::manage(PwmaPtr);
731 isl::ast_expr AccessExpr = Build.access_from(Pwma);
732 P = isl_printer_print_ast_expr(P, AccessExpr.get());
733 } else {
734 P = isl_printer_print_str(
735 P, MemAcc->getLatestScopArrayInfo()->getName().c_str());
736 P = isl_printer_print_str(P, "[*]");
737 }
738 P = isl_printer_end_line(P);
739 }
740
741 P = isl_printer_indent(P, -2);
742 P = isl_printer_start_line(P);
743 P = isl_printer_print_str(P, ");");
744 P = isl_printer_end_line(P);
745
746 isl_ast_print_options_free(O);
747 return P;
748}
749
750void IslAstInfo::print(raw_ostream &OS) {
751 isl_ast_print_options *Options;
752 isl_ast_node *RootNode = Ast.getAst();
753 Function &F = S.getFunction();
754
755 OS << ":: isl ast :: " << F.getName() << " :: " << S.getNameStr() << "\n";
756
757 if (!RootNode) {
758 OS << ":: isl ast generation and code generation was skipped!\n\n";
759 OS << ":: This is either because no useful optimizations could be applied "
760 "(use -polly-process-unprofitable to enforce code generation) or "
761 "because earlier passes such as dependence analysis timed out (use "
762 "-polly-dependences-computeout=0 to set dependence analysis timeout "
763 "to infinity)\n\n";
764 return;
765 }
766
767 isl_ast_expr *RunCondition = Ast.getRunCondition();
768 char *RtCStr, *AstStr;
769
770 Options = isl_ast_print_options_alloc(S.getIslCtx().get());
771
772 if (PrintAccesses)
773 Options =
774 isl_ast_print_options_set_print_user(Options, cbPrintUser, nullptr);
775 Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr);
776
777 isl_printer *P = isl_printer_to_str(S.getIslCtx().get());
778 P = isl_printer_set_output_format(P, ISL_FORMAT_C4);
779 P = isl_printer_print_ast_expr(P, RunCondition);
780 RtCStr = isl_printer_get_str(P);
781 P = isl_printer_flush(P);
782 P = isl_printer_indent(P, 4);
783 P = isl_ast_node_print(RootNode, P, Options);
784 AstStr = isl_printer_get_str(P);
785
786 auto *Schedule = S.getScheduleTree().release();
787
788 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { dbgs() << S.getContextStr() <<
"\n"; dbgs() << stringFromIslObj(Schedule); }; } } while
(false)
789 dbgs() << S.getContextStr() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { dbgs() << S.getContextStr() <<
"\n"; dbgs() << stringFromIslObj(Schedule); }; } } while
(false)
790 dbgs() << stringFromIslObj(Schedule);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { dbgs() << S.getContextStr() <<
"\n"; dbgs() << stringFromIslObj(Schedule); }; } } while
(false)
791 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("polly-ast")) { { dbgs() << S.getContextStr() <<
"\n"; dbgs() << stringFromIslObj(Schedule); }; } } while
(false)
;
792 OS << "\nif (" << RtCStr << ")\n\n";
793 OS << AstStr << "\n";
794 OS << "else\n";
795 OS << " { /* original code */ }\n\n";
796
797 free(RtCStr);
798 free(AstStr);
799
800 isl_ast_expr_free(RunCondition);
801 isl_schedule_free(Schedule);
802 isl_ast_node_free(RootNode);
803 isl_printer_free(P);
804}
805
806AnalysisKey IslAstAnalysis::Key;
807PreservedAnalyses IslAstPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
808 ScopStandardAnalysisResults &SAR,
809 SPMUpdater &U) {
810 auto &Ast = SAM.getResult<IslAstAnalysis>(S, SAR);
811 Ast.print(OS);
812 return PreservedAnalyses::all();
813}
814
815void IslAstInfoWrapperPass::releaseMemory() { Ast.reset(); }
816
817bool IslAstInfoWrapperPass::runOnScop(Scop &Scop) {
818 auto GetDeps = [this](Dependences::AnalysisLevel Lvl) -> const Dependences & {
819 return getAnalysis<DependenceInfo>().getDependences(Lvl);
820 };
821
822 Ast = runIslAst(Scop, GetDeps);
823
824 return false;
825}
826
827void IslAstInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
828 // Get the Common analysis usage of ScopPasses.
829 ScopPass::getAnalysisUsage(AU);
830 AU.addRequiredTransitive<ScopInfoRegionPass>();
831 AU.addRequired<DependenceInfo>();
832
833 AU.addPreserved<DependenceInfo>();
834}
835
836void IslAstInfoWrapperPass::printScop(raw_ostream &OS, Scop &S) const {
837 OS << "Printing analysis 'Polly - Generate an AST of the SCoP (isl)'"
838 << S.getName() << "' in function '" << S.getFunction().getName() << "':\n";
839 if (Ast)
840 Ast->print(OS);
841}
842
843char IslAstInfoWrapperPass::ID = 0;
844
845Pass *polly::createIslAstInfoWrapperPassPass() {
846 return new IslAstInfoWrapperPass();
847}
848
849INITIALIZE_PASS_BEGIN(IslAstInfoWrapperPass, "polly-ast",static void *initializeIslAstInfoWrapperPassPassOnce(PassRegistry
&Registry) {
850 "Polly - Generate an AST of the SCoP (isl)", false,static void *initializeIslAstInfoWrapperPassPassOnce(PassRegistry
&Registry) {
851 false)static void *initializeIslAstInfoWrapperPassPassOnce(PassRegistry
&Registry) {
;
852INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass)initializeScopInfoRegionPassPass(Registry);;
853INITIALIZE_PASS_DEPENDENCY(DependenceInfo)initializeDependenceInfoPass(Registry);;
854INITIALIZE_PASS_END(IslAstInfoWrapperPass, "polly-ast",PassInfo *PI = new PassInfo( "Polly - Generate an AST from the SCoP (isl)"
, "polly-ast", &IslAstInfoWrapperPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<IslAstInfoWrapperPass>), false, false)
; Registry.registerPass(*PI, true); return PI; } static llvm::
once_flag InitializeIslAstInfoWrapperPassPassFlag; void llvm::
initializeIslAstInfoWrapperPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeIslAstInfoWrapperPassPassFlag, initializeIslAstInfoWrapperPassPassOnce
, std::ref(Registry)); }
855 "Polly - Generate an AST from the SCoP (isl)", false, false)PassInfo *PI = new PassInfo( "Polly - Generate an AST from the SCoP (isl)"
, "polly-ast", &IslAstInfoWrapperPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<IslAstInfoWrapperPass>), false, false)
; Registry.registerPass(*PI, true); return PI; } static llvm::
once_flag InitializeIslAstInfoWrapperPassPassFlag; void llvm::
initializeIslAstInfoWrapperPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeIslAstInfoWrapperPassPassFlag, initializeIslAstInfoWrapperPassPassOnce
, std::ref(Registry)); }

/usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/unique_ptr.h

1// unique_ptr implementation -*- C++ -*-
2
3// Copyright (C) 2008-2016 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _UNIQUE_PTR_H1
31#define _UNIQUE_PTR_H1 1
32
33#include <bits/c++config.h>
34#include <debug/assertions.h>
35#include <type_traits>
36#include <utility>
37#include <tuple>
38
39namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
42
43 /**
44 * @addtogroup pointer_abstractions
45 * @{
46 */
47
48#if _GLIBCXX_USE_DEPRECATED1
49 template<typename> class auto_ptr;
50#endif
51
52 /// Primary template of default_delete, used by unique_ptr
53 template<typename _Tp>
54 struct default_delete
55 {
56 /// Default constructor
57 constexpr default_delete() noexcept = default;
58
59 /** @brief Converting constructor.
60 *
61 * Allows conversion from a deleter for arrays of another type, @p _Up,
62 * only if @p _Up* is convertible to @p _Tp*.
63 */
64 template<typename _Up, typename = typename
65 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
66 default_delete(const default_delete<_Up>&) noexcept { }
67
68 /// Calls @c delete @p __ptr
69 void
70 operator()(_Tp* __ptr) const
71 {
72 static_assert(!is_void<_Tp>::value,
73 "can't delete pointer to incomplete type");
74 static_assert(sizeof(_Tp)>0,
75 "can't delete pointer to incomplete type");
76 delete __ptr;
77 }
78 };
79
80 // _GLIBCXX_RESOLVE_LIB_DEFECTS
81 // DR 740 - omit specialization for array objects with a compile time length
82 /// Specialization for arrays, default_delete.
83 template<typename _Tp>
84 struct default_delete<_Tp[]>
85 {
86 public:
87 /// Default constructor
88 constexpr default_delete() noexcept = default;
89
90 /** @brief Converting constructor.
91 *
92 * Allows conversion from a deleter for arrays of another type, such as
93 * a const-qualified version of @p _Tp.
94 *
95 * Conversions from types derived from @c _Tp are not allowed because
96 * it is unsafe to @c delete[] an array of derived types through a
97 * pointer to the base type.
98 */
99 template<typename _Up, typename = typename
100 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type>
101 default_delete(const default_delete<_Up[]>&) noexcept { }
102
103 /// Calls @c delete[] @p __ptr
104 template<typename _Up>
105 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
106 operator()(_Up* __ptr) const
107 {
108 static_assert(sizeof(_Tp)>0,
109 "can't delete pointer to incomplete type");
110 delete [] __ptr;
111 }
112 };
113
114 /// 20.7.1.2 unique_ptr for single objects.
115 template <typename _Tp, typename _Dp = default_delete<_Tp> >
116 class unique_ptr
117 {
118 // use SFINAE to determine whether _Del::pointer exists
119 class _Pointer
120 {
121 template<typename _Up>
122 static typename _Up::pointer __test(typename _Up::pointer*);
123
124 template<typename _Up>
125 static _Tp* __test(...);
126
127 typedef typename remove_reference<_Dp>::type _Del;
128
129 public:
130 typedef decltype(__test<_Del>(0)) type;
131 };
132
133 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
134 __tuple_type _M_t;
135
136 public:
137 typedef typename _Pointer::type pointer;
138 typedef _Tp element_type;
139 typedef _Dp deleter_type;
140
141
142 // helper template for detecting a safe conversion from another
143 // unique_ptr
144 template<typename _Up, typename _Ep>
145 using __safe_conversion_up = __and_<
146 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
147 __not_<is_array<_Up>>,
148 __or_<__and_<is_reference<deleter_type>,
149 is_same<deleter_type, _Ep>>,
150 __and_<__not_<is_reference<deleter_type>>,
151 is_convertible<_Ep, deleter_type>>
152 >
153 >;
154
155 // Constructors.
156
157 /// Default constructor, creates a unique_ptr that owns nothing.
158 constexpr unique_ptr() noexcept
159 : _M_t()
160 { static_assert(!is_pointer<deleter_type>::value,
161 "constructed with null function pointer deleter"); }
162
163 /** Takes ownership of a pointer.
164 *
165 * @param __p A pointer to an object of @c element_type
166 *
167 * The deleter will be value-initialized.
168 */
169 explicit
170 unique_ptr(pointer __p) noexcept
171 : _M_t()
172 {
173 std::get<0>(_M_t) = __p;
174 static_assert(!is_pointer<deleter_type>::value,
175 "constructed with null function pointer deleter");
176 }
177
178 /** Takes ownership of a pointer.
179 *
180 * @param __p A pointer to an object of @c element_type
181 * @param __d A reference to a deleter.
182 *
183 * The deleter will be initialized with @p __d
184 */
185 unique_ptr(pointer __p,
186 typename conditional<is_reference<deleter_type>::value,
187 deleter_type, const deleter_type&>::type __d) noexcept
188 : _M_t(__p, __d) { }
189
190 /** Takes ownership of a pointer.
191 *
192 * @param __p A pointer to an object of @c element_type
193 * @param __d An rvalue reference to a deleter.
194 *
195 * The deleter will be initialized with @p std::move(__d)
196 */
197 unique_ptr(pointer __p,
198 typename remove_reference<deleter_type>::type&& __d) noexcept
199 : _M_t(std::move(__p), std::move(__d))
200 { static_assert(!std::is_reference<deleter_type>::value,
201 "rvalue deleter bound to reference"); }
202
203 /// Creates a unique_ptr that owns nothing.
204 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
205
206 // Move constructors.
207
208 /// Move constructor.
209 unique_ptr(unique_ptr&& __u) noexcept
210 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
211
212 /** @brief Converting constructor from another type
213 *
214 * Requires that the pointer owned by @p __u is convertible to the
215 * type of pointer owned by this object, @p __u does not own an array,
216 * and @p __u has a compatible deleter type.
217 */
218 template<typename _Up, typename _Ep, typename = _Require<
219 __safe_conversion_up<_Up, _Ep>,
220 typename conditional<is_reference<_Dp>::value,
221 is_same<_Ep, _Dp>,
222 is_convertible<_Ep, _Dp>>::type>>
223 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
224 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
225 { }
226
227#if _GLIBCXX_USE_DEPRECATED1
228 /// Converting constructor from @c auto_ptr
229 template<typename _Up, typename = _Require<
230 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
231 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
232#endif
233
234 /// Destructor, invokes the deleter if the stored pointer is not null.
235 ~unique_ptr() noexcept
236 {
237 auto& __ptr = std::get<0>(_M_t);
238 if (__ptr != nullptr)
239 get_deleter()(__ptr);
240 __ptr = pointer();
241 }
242
243 // Assignment.
244
245 /** @brief Move assignment operator.
246 *
247 * @param __u The object to transfer ownership from.
248 *
249 * Invokes the deleter first if this object owns a pointer.
250 */
251 unique_ptr&
252 operator=(unique_ptr&& __u) noexcept
253 {
254 reset(__u.release());
255 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
256 return *this;
257 }
258
259 /** @brief Assignment from another type.
260 *
261 * @param __u The object to transfer ownership from, which owns a
262 * convertible pointer to a non-array object.
263 *
264 * Invokes the deleter first if this object owns a pointer.
265 */
266 template<typename _Up, typename _Ep>
267 typename enable_if< __and_<
268 __safe_conversion_up<_Up, _Ep>,
269 is_assignable<deleter_type&, _Ep&&>
270 >::value,
271 unique_ptr&>::type
272 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
273 {
274 reset(__u.release());
275 get_deleter() = std::forward<_Ep>(__u.get_deleter());
276 return *this;
277 }
278
279 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
280 unique_ptr&
281 operator=(nullptr_t) noexcept
282 {
283 reset();
284 return *this;
285 }
286
287 // Observers.
288
289 /// Dereference the stored pointer.
290 typename add_lvalue_reference<element_type>::type
291 operator*() const
292 {
293 __glibcxx_assert(get() != pointer());
294 return *get();
295 }
296
297 /// Return the stored pointer.
298 pointer
299 operator->() const noexcept
300 {
301 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
302 return get();
303 }
304
305 /// Return the stored pointer.
306 pointer
307 get() const noexcept
308 { return std::get<0>(_M_t); }
309
310 /// Return a reference to the stored deleter.
311 deleter_type&
312 get_deleter() noexcept
313 { return std::get<1>(_M_t); }
314
315 /// Return a reference to the stored deleter.
316 const deleter_type&
317 get_deleter() const noexcept
318 { return std::get<1>(_M_t); }
319
320 /// Return @c true if the stored pointer is not null.
321 explicit operator bool() const noexcept
322 { return get() == pointer() ? false : true; }
323
324 // Modifiers.
325
326 /// Release ownership of any stored pointer.
327 pointer
328 release() noexcept
329 {
330 pointer __p = get();
331 std::get<0>(_M_t) = pointer();
332 return __p;
333 }
334
335 /** @brief Replace the stored pointer.
336 *
337 * @param __p The new pointer to store.
338 *
339 * The deleter will be invoked if a pointer is already owned.
340 */
341 void
342 reset(pointer __p = pointer()) noexcept
343 {
344 using std::swap;
345 swap(std::get<0>(_M_t), __p);
346 if (__p != pointer())
347 get_deleter()(__p);
348 }
349
350 /// Exchange the pointer and deleter with another object.
351 void
352 swap(unique_ptr& __u) noexcept
353 {
354 using std::swap;
355 swap(_M_t, __u._M_t);
356 }
357
358 // Disable copy from lvalue.
359 unique_ptr(const unique_ptr&) = delete;
360 unique_ptr& operator=(const unique_ptr&) = delete;
361 };
362
363 /// 20.7.1.3 unique_ptr for array objects with a runtime length
364 // [unique.ptr.runtime]
365 // _GLIBCXX_RESOLVE_LIB_DEFECTS
366 // DR 740 - omit specialization for array objects with a compile time length
367 template<typename _Tp, typename _Dp>
368 class unique_ptr<_Tp[], _Dp>
369 {
370 // use SFINAE to determine whether _Del::pointer exists
371 class _Pointer
372 {
373 template<typename _Up>
374 static typename _Up::pointer __test(typename _Up::pointer*);
375
376 template<typename _Up>
377 static _Tp* __test(...);
378
379 typedef typename remove_reference<_Dp>::type _Del;
380
381 public:
382 typedef decltype(__test<_Del>(0)) type;
383 };
384
385 typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
386 __tuple_type _M_t;
387
388 template<typename _Up>
389 using __remove_cv = typename remove_cv<_Up>::type;
390
391 // like is_base_of<_Tp, _Up> but false if unqualified types are the same
392 template<typename _Up>
393 using __is_derived_Tp
394 = __and_< is_base_of<_Tp, _Up>,
395 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
396
397
398 public:
399 typedef typename _Pointer::type pointer;
400 typedef _Tp element_type;
401 typedef _Dp deleter_type;
402
403 // helper template for detecting a safe conversion from another
404 // unique_ptr
405 template<typename _Up, typename _Ep,
406 typename _Up_up = unique_ptr<_Up, _Ep>,
407 typename _Up_element_type = typename _Up_up::element_type>
408 using __safe_conversion_up = __and_<
409 is_array<_Up>,
410 is_same<pointer, element_type*>,
411 is_same<typename _Up_up::pointer, _Up_element_type*>,
412 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
413 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
414 __and_<__not_<is_reference<deleter_type>>,
415 is_convertible<_Ep, deleter_type>>>
416 >;
417
418 // helper template for detecting a safe conversion from a raw pointer
419 template<typename _Up>
420 using __safe_conversion_raw = __and_<
421 __or_<__or_<is_same<_Up, pointer>,
422 is_same<_Up, nullptr_t>>,
423 __and_<is_pointer<_Up>,
424 is_same<pointer, element_type*>,
425 is_convertible<
426 typename remove_pointer<_Up>::type(*)[],
427 element_type(*)[]>
428 >
429 >
430 >;
431
432 // Constructors.
433
434 /// Default constructor, creates a unique_ptr that owns nothing.
435 constexpr unique_ptr() noexcept
436 : _M_t()
437 { static_assert(!std::is_pointer<deleter_type>::value,
438 "constructed with null function pointer deleter"); }
439
440 /** Takes ownership of a pointer.
441 *
442 * @param __p A pointer to an array of a type safely convertible
443 * to an array of @c element_type
444 *
445 * The deleter will be value-initialized.
446 */
447 template<typename _Up,
448 typename = typename enable_if<
449 __safe_conversion_raw<_Up>::value, bool>::type>
450 explicit
451 unique_ptr(_Up __p) noexcept
452 : _M_t(__p, deleter_type())
453 { static_assert(!is_pointer<deleter_type>::value,
454 "constructed with null function pointer deleter"); }
455
456 /** Takes ownership of a pointer.
457 *
458 * @param __p A pointer to an array of a type safely convertible
459 * to an array of @c element_type
460 * @param __d A reference to a deleter.
461 *
462 * The deleter will be initialized with @p __d
463 */
464 template<typename _Up,
465 typename = typename enable_if<
466 __safe_conversion_raw<_Up>::value, bool>::type>
467 unique_ptr(_Up __p,
468 typename conditional<is_reference<deleter_type>::value,
469 deleter_type, const deleter_type&>::type __d) noexcept
470 : _M_t(__p, __d) { }
471
472 /** Takes ownership of a pointer.
473 *
474 * @param __p A pointer to an array of a type safely convertible
475 * to an array of @c element_type
476 * @param __d A reference to a deleter.
477 *
478 * The deleter will be initialized with @p std::move(__d)
479 */
480 template<typename _Up,
481 typename = typename enable_if<
482 __safe_conversion_raw<_Up>::value, bool>::type>
483 unique_ptr(_Up __p, typename
484 remove_reference<deleter_type>::type&& __d) noexcept
485 : _M_t(std::move(__p), std::move(__d))
486 { static_assert(!is_reference<deleter_type>::value,
487 "rvalue deleter bound to reference"); }
488
489 /// Move constructor.
490 unique_ptr(unique_ptr&& __u) noexcept
491 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
492
493 /// Creates a unique_ptr that owns nothing.
494 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
495
496 template<typename _Up, typename _Ep,
497 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
498 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
499 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
500 { }
501
502 /// Destructor, invokes the deleter if the stored pointer is not null.
503 ~unique_ptr()
504 {
505 auto& __ptr = std::get<0>(_M_t);
506 if (__ptr != nullptr)
507 get_deleter()(__ptr);
508 __ptr = pointer();
509 }
510
511 // Assignment.
512
513 /** @brief Move assignment operator.
514 *
515 * @param __u The object to transfer ownership from.
516 *
517 * Invokes the deleter first if this object owns a pointer.
518 */
519 unique_ptr&
520 operator=(unique_ptr&& __u) noexcept
521 {
522 reset(__u.release());
523 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
524 return *this;
525 }
526
527 /** @brief Assignment from another type.
528 *
529 * @param __u The object to transfer ownership from, which owns a
530 * convertible pointer to an array object.
531 *
532 * Invokes the deleter first if this object owns a pointer.
533 */
534 template<typename _Up, typename _Ep>
535 typename
536 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
537 is_assignable<deleter_type&, _Ep&&>
538 >::value,
539 unique_ptr&>::type
540 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
541 {
542 reset(__u.release());
543 get_deleter() = std::forward<_Ep>(__u.get_deleter());
544 return *this;
545 }
546
547 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
548 unique_ptr&
549 operator=(nullptr_t) noexcept
550 {
551 reset();
552 return *this;
553 }
554
555 // Observers.
556
557 /// Access an element of owned array.
558 typename std::add_lvalue_reference<element_type>::type
559 operator[](size_t __i) const
560 {
561 __glibcxx_assert(get() != pointer());
562 return get()[__i];
563 }
564
565 /// Return the stored pointer.
566 pointer
567 get() const noexcept
568 { return std::get<0>(_M_t); }
569
570 /// Return a reference to the stored deleter.
571 deleter_type&
572 get_deleter() noexcept
573 { return std::get<1>(_M_t); }
574
575 /// Return a reference to the stored deleter.
576 const deleter_type&
577 get_deleter() const noexcept
578 { return std::get<1>(_M_t); }
579
580 /// Return @c true if the stored pointer is not null.
581 explicit operator bool() const noexcept
582 { return get() == pointer() ? false : true; }
583
584 // Modifiers.
585
586 /// Release ownership of any stored pointer.
587 pointer
588 release() noexcept
589 {
590 pointer __p = get();
591 std::get<0>(_M_t) = pointer();
592 return __p;
593 }
594
595 /** @brief Replace the stored pointer.
596 *
597 * @param __p The new pointer to store.
598 *
599 * The deleter will be invoked if a pointer is already owned.
600 */
601 template <typename _Up,
602 typename = _Require<
603 __or_<is_same<_Up, pointer>,
604 __and_<is_same<pointer, element_type*>,
605 is_pointer<_Up>,
606 is_convertible<
607 typename remove_pointer<_Up>::type(*)[],
608 element_type(*)[]
609 >
610 >
611 >
612 >>
613 void
614 reset(_Up __p) noexcept
615 {
616 pointer __ptr = __p;
617 using std::swap;
618 swap(std::get<0>(_M_t), __ptr);
619 if (__ptr != nullptr)
620 get_deleter()(__ptr);
621 }
622
623 void reset(nullptr_t = nullptr) noexcept
624 {
625 reset(pointer());
626 }
627
628 /// Exchange the pointer and deleter with another object.
629 void
630 swap(unique_ptr& __u) noexcept
631 {
632 using std::swap;
633 swap(_M_t, __u._M_t);
634 }
635
636 // Disable copy from lvalue.
637 unique_ptr(const unique_ptr&) = delete;
638 unique_ptr& operator=(const unique_ptr&) = delete;
639 };
640
641 template<typename _Tp, typename _Dp>
642 inline void
643 swap(unique_ptr<_Tp, _Dp>& __x,
644 unique_ptr<_Tp, _Dp>& __y) noexcept
645 { __x.swap(__y); }
646
647 template<typename _Tp, typename _Dp,
648 typename _Up, typename _Ep>
649 inline bool
650 operator==(const unique_ptr<_Tp, _Dp>& __x,
651 const unique_ptr<_Up, _Ep>& __y)
652 { return __x.get() == __y.get(); }
653
654 template<typename _Tp, typename _Dp>
655 inline bool
656 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
657 { return !__x; }
658
659 template<typename _Tp, typename _Dp>
660 inline bool
661 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
662 { return !__x; }
663
664 template<typename _Tp, typename _Dp,
665 typename _Up, typename _Ep>
666 inline bool
667 operator!=(const unique_ptr<_Tp, _Dp>& __x,
668 const unique_ptr<_Up, _Ep>& __y)
669 { return __x.get() != __y.get(); }
670
671 template<typename _Tp, typename _Dp>
672 inline bool
673 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
674 { return (bool)__x; }
675
676 template<typename _Tp, typename _Dp>
677 inline bool
678 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
679 { return (bool)__x; }
680
681 template<typename _Tp, typename _Dp,
682 typename _Up, typename _Ep>
683 inline bool
684 operator<(const unique_ptr<_Tp, _Dp>& __x,
685 const unique_ptr<_Up, _Ep>& __y)
686 {
687 typedef typename
688 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
689 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
690 return std::less<_CT>()(__x.get(), __y.get());
691 }
692
693 template<typename _Tp, typename _Dp>
694 inline bool
695 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
696 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
697 nullptr); }
698
699 template<typename _Tp, typename _Dp>
700 inline bool
701 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
702 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
703 __x.get()); }
704
705 template<typename _Tp, typename _Dp,
706 typename _Up, typename _Ep>
707 inline bool
708 operator<=(const unique_ptr<_Tp, _Dp>& __x,
709 const unique_ptr<_Up, _Ep>& __y)
710 { return !(__y < __x); }
711
712 template<typename _Tp, typename _Dp>
713 inline bool
714 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
715 { return !(nullptr < __x); }
716
717 template<typename _Tp, typename _Dp>
718 inline bool
719 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
720 { return !(__x < nullptr); }
721
722 template<typename _Tp, typename _Dp,
723 typename _Up, typename _Ep>
724 inline bool
725 operator>(const unique_ptr<_Tp, _Dp>& __x,
726 const unique_ptr<_Up, _Ep>& __y)
727 { return (__y < __x); }
728
729 template<typename _Tp, typename _Dp>
730 inline bool
731 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
732 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
733 __x.get()); }
734
735 template<typename _Tp, typename _Dp>
736 inline bool
737 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
738 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
739 nullptr); }
740
741 template<typename _Tp, typename _Dp,
742 typename _Up, typename _Ep>
743 inline bool
744 operator>=(const unique_ptr<_Tp, _Dp>& __x,
745 const unique_ptr<_Up, _Ep>& __y)
746 { return !(__x < __y); }
747
748 template<typename _Tp, typename _Dp>
749 inline bool
750 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
751 { return !(__x < nullptr); }
752
753 template<typename _Tp, typename _Dp>
754 inline bool
755 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
756 { return !(nullptr < __x); }
757
758 /// std::hash specialization for unique_ptr.
759 template<typename _Tp, typename _Dp>
760 struct hash<unique_ptr<_Tp, _Dp>>
761 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>
762 {
763 size_t
764 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
765 {
766 typedef unique_ptr<_Tp, _Dp> _UP;
767 return std::hash<typename _UP::pointer>()(__u.get());
768 }
769 };
770
771#if __cplusplus201402L > 201103L
772
773#define __cpp_lib_make_unique201304 201304
774
775 template<typename _Tp>
776 struct _MakeUniq
777 { typedef unique_ptr<_Tp> __single_object; };
778
779 template<typename _Tp>
780 struct _MakeUniq<_Tp[]>
781 { typedef unique_ptr<_Tp[]> __array; };
782
783 template<typename _Tp, size_t _Bound>
784 struct _MakeUniq<_Tp[_Bound]>
785 { struct __invalid_type { }; };
786
787 /// std::make_unique for single objects
788 template<typename _Tp, typename... _Args>
789 inline typename _MakeUniq<_Tp>::__single_object
790 make_unique(_Args&&... __args)
791 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
6
Memory is allocated
792
793 /// std::make_unique for arrays of unknown bound
794 template<typename _Tp>
795 inline typename _MakeUniq<_Tp>::__array
796 make_unique(size_t __num)
797 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
798
799 /// Disable std::make_unique for arrays of known bound
800 template<typename _Tp, typename... _Args>
801 inline typename _MakeUniq<_Tp>::__invalid_type
802 make_unique(_Args&&...) = delete;
803#endif
804
805 // @} group pointer_abstractions
806
807_GLIBCXX_END_NAMESPACE_VERSION
808} // namespace
809
810#endif /* _UNIQUE_PTR_H */