1 | //===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===// |
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 | // This contains code to emit Stmt nodes as LLVM code. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "CGDebugInfo.h" |
14 | #include "CGOpenMPRuntime.h" |
15 | #include "CodeGenFunction.h" |
16 | #include "CodeGenModule.h" |
17 | #include "TargetInfo.h" |
18 | #include "clang/AST/Attr.h" |
19 | #include "clang/AST/Expr.h" |
20 | #include "clang/AST/Stmt.h" |
21 | #include "clang/AST/StmtVisitor.h" |
22 | #include "clang/Basic/Builtins.h" |
23 | #include "clang/Basic/DiagnosticSema.h" |
24 | #include "clang/Basic/PrettyStackTrace.h" |
25 | #include "clang/Basic/SourceManager.h" |
26 | #include "clang/Basic/TargetInfo.h" |
27 | #include "llvm/ADT/SmallSet.h" |
28 | #include "llvm/ADT/StringExtras.h" |
29 | #include "llvm/IR/DataLayout.h" |
30 | #include "llvm/IR/InlineAsm.h" |
31 | #include "llvm/IR/Intrinsics.h" |
32 | #include "llvm/IR/MDBuilder.h" |
33 | #include "llvm/Support/SaveAndRestore.h" |
34 | |
35 | using namespace clang; |
36 | using namespace CodeGen; |
37 | |
38 | //===----------------------------------------------------------------------===// |
39 | // Statement Emission |
40 | //===----------------------------------------------------------------------===// |
41 | |
42 | void CodeGenFunction::EmitStopPoint(const Stmt *S) { |
43 | if (CGDebugInfo *DI = getDebugInfo()) { |
44 | SourceLocation Loc; |
45 | Loc = S->getBeginLoc(); |
46 | DI->EmitLocation(Builder, Loc); |
47 | |
48 | LastStopPoint = Loc; |
49 | } |
50 | } |
51 | |
52 | void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) { |
53 | assert(S && "Null statement?" ); |
54 | PGO.setCurrentStmt(S); |
55 | |
56 | // These statements have their own debug info handling. |
57 | if (EmitSimpleStmt(S, Attrs)) |
58 | return; |
59 | |
60 | // Check if we are generating unreachable code. |
61 | if (!HaveInsertPoint()) { |
62 | // If so, and the statement doesn't contain a label, then we do not need to |
63 | // generate actual code. This is safe because (1) the current point is |
64 | // unreachable, so we don't need to execute the code, and (2) we've already |
65 | // handled the statements which update internal data structures (like the |
66 | // local variable map) which could be used by subsequent statements. |
67 | if (!ContainsLabel(S)) { |
68 | // Verify that any decl statements were handled as simple, they may be in |
69 | // scope of subsequent reachable statements. |
70 | assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!" ); |
71 | return; |
72 | } |
73 | |
74 | // Otherwise, make a new block to hold the code. |
75 | EnsureInsertPoint(); |
76 | } |
77 | |
78 | // Generate a stoppoint if we are emitting debug info. |
79 | EmitStopPoint(S); |
80 | |
81 | // Ignore all OpenMP directives except for simd if OpenMP with Simd is |
82 | // enabled. |
83 | if (getLangOpts().OpenMP && getLangOpts().OpenMPSimd) { |
84 | if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) { |
85 | EmitSimpleOMPExecutableDirective(*D); |
86 | return; |
87 | } |
88 | } |
89 | |
90 | switch (S->getStmtClass()) { |
91 | case Stmt::NoStmtClass: |
92 | case Stmt::CXXCatchStmtClass: |
93 | case Stmt::SEHExceptStmtClass: |
94 | case Stmt::SEHFinallyStmtClass: |
95 | case Stmt::MSDependentExistsStmtClass: |
96 | llvm_unreachable("invalid statement class to emit generically" ); |
97 | case Stmt::NullStmtClass: |
98 | case Stmt::CompoundStmtClass: |
99 | case Stmt::DeclStmtClass: |
100 | case Stmt::LabelStmtClass: |
101 | case Stmt::AttributedStmtClass: |
102 | case Stmt::GotoStmtClass: |
103 | case Stmt::BreakStmtClass: |
104 | case Stmt::ContinueStmtClass: |
105 | case Stmt::DefaultStmtClass: |
106 | case Stmt::CaseStmtClass: |
107 | case Stmt::SEHLeaveStmtClass: |
108 | llvm_unreachable("should have emitted these statements as simple" ); |
109 | |
110 | #define STMT(Type, Base) |
111 | #define ABSTRACT_STMT(Op) |
112 | #define EXPR(Type, Base) \ |
113 | case Stmt::Type##Class: |
114 | #include "clang/AST/StmtNodes.inc" |
115 | { |
116 | // Remember the block we came in on. |
117 | llvm::BasicBlock *incoming = Builder.GetInsertBlock(); |
118 | assert(incoming && "expression emission must have an insertion point" ); |
119 | |
120 | EmitIgnoredExpr(cast<Expr>(S)); |
121 | |
122 | llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); |
123 | assert(outgoing && "expression emission cleared block!" ); |
124 | |
125 | // The expression emitters assume (reasonably!) that the insertion |
126 | // point is always set. To maintain that, the call-emission code |
127 | // for noreturn functions has to enter a new block with no |
128 | // predecessors. We want to kill that block and mark the current |
129 | // insertion point unreachable in the common case of a call like |
130 | // "exit();". Since expression emission doesn't otherwise create |
131 | // blocks with no predecessors, we can just test for that. |
132 | // However, we must be careful not to do this to our incoming |
133 | // block, because *statement* emission does sometimes create |
134 | // reachable blocks which will have no predecessors until later in |
135 | // the function. This occurs with, e.g., labels that are not |
136 | // reachable by fallthrough. |
137 | if (incoming != outgoing && outgoing->use_empty()) { |
138 | outgoing->eraseFromParent(); |
139 | Builder.ClearInsertionPoint(); |
140 | } |
141 | break; |
142 | } |
143 | |
144 | case Stmt::IndirectGotoStmtClass: |
145 | EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; |
146 | |
147 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
148 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break; |
149 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break; |
150 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break; |
151 | |
152 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
153 | |
154 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
155 | case Stmt::GCCAsmStmtClass: // Intentional fall-through. |
156 | case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
157 | case Stmt::CoroutineBodyStmtClass: |
158 | EmitCoroutineBody(cast<CoroutineBodyStmt>(*S)); |
159 | break; |
160 | case Stmt::CoreturnStmtClass: |
161 | EmitCoreturnStmt(cast<CoreturnStmt>(*S)); |
162 | break; |
163 | case Stmt::CapturedStmtClass: { |
164 | const CapturedStmt *CS = cast<CapturedStmt>(S); |
165 | EmitCapturedStmt(*CS, CS->getCapturedRegionKind()); |
166 | } |
167 | break; |
168 | case Stmt::ObjCAtTryStmtClass: |
169 | EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); |
170 | break; |
171 | case Stmt::ObjCAtCatchStmtClass: |
172 | llvm_unreachable( |
173 | "@catch statements should be handled by EmitObjCAtTryStmt" ); |
174 | case Stmt::ObjCAtFinallyStmtClass: |
175 | llvm_unreachable( |
176 | "@finally statements should be handled by EmitObjCAtTryStmt" ); |
177 | case Stmt::ObjCAtThrowStmtClass: |
178 | EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); |
179 | break; |
180 | case Stmt::ObjCAtSynchronizedStmtClass: |
181 | EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); |
182 | break; |
183 | case Stmt::ObjCForCollectionStmtClass: |
184 | EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); |
185 | break; |
186 | case Stmt::ObjCAutoreleasePoolStmtClass: |
187 | EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); |
188 | break; |
189 | |
190 | case Stmt::CXXTryStmtClass: |
191 | EmitCXXTryStmt(cast<CXXTryStmt>(*S)); |
192 | break; |
193 | case Stmt::CXXForRangeStmtClass: |
194 | EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs); |
195 | break; |
196 | case Stmt::SEHTryStmtClass: |
197 | EmitSEHTryStmt(cast<SEHTryStmt>(*S)); |
198 | break; |
199 | case Stmt::OMPCanonicalLoopClass: |
200 | EmitOMPCanonicalLoop(cast<OMPCanonicalLoop>(S)); |
201 | break; |
202 | case Stmt::OMPParallelDirectiveClass: |
203 | EmitOMPParallelDirective(cast<OMPParallelDirective>(*S)); |
204 | break; |
205 | case Stmt::OMPSimdDirectiveClass: |
206 | EmitOMPSimdDirective(cast<OMPSimdDirective>(*S)); |
207 | break; |
208 | case Stmt::OMPTileDirectiveClass: |
209 | EmitOMPTileDirective(cast<OMPTileDirective>(*S)); |
210 | break; |
211 | case Stmt::OMPForDirectiveClass: |
212 | EmitOMPForDirective(cast<OMPForDirective>(*S)); |
213 | break; |
214 | case Stmt::OMPForSimdDirectiveClass: |
215 | EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S)); |
216 | break; |
217 | case Stmt::OMPSectionsDirectiveClass: |
218 | EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S)); |
219 | break; |
220 | case Stmt::OMPSectionDirectiveClass: |
221 | EmitOMPSectionDirective(cast<OMPSectionDirective>(*S)); |
222 | break; |
223 | case Stmt::OMPSingleDirectiveClass: |
224 | EmitOMPSingleDirective(cast<OMPSingleDirective>(*S)); |
225 | break; |
226 | case Stmt::OMPMasterDirectiveClass: |
227 | EmitOMPMasterDirective(cast<OMPMasterDirective>(*S)); |
228 | break; |
229 | case Stmt::OMPCriticalDirectiveClass: |
230 | EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S)); |
231 | break; |
232 | case Stmt::OMPParallelForDirectiveClass: |
233 | EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S)); |
234 | break; |
235 | case Stmt::OMPParallelForSimdDirectiveClass: |
236 | EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S)); |
237 | break; |
238 | case Stmt::OMPParallelMasterDirectiveClass: |
239 | EmitOMPParallelMasterDirective(cast<OMPParallelMasterDirective>(*S)); |
240 | break; |
241 | case Stmt::OMPParallelSectionsDirectiveClass: |
242 | EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S)); |
243 | break; |
244 | case Stmt::OMPTaskDirectiveClass: |
245 | EmitOMPTaskDirective(cast<OMPTaskDirective>(*S)); |
246 | break; |
247 | case Stmt::OMPTaskyieldDirectiveClass: |
248 | EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S)); |
249 | break; |
250 | case Stmt::OMPBarrierDirectiveClass: |
251 | EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S)); |
252 | break; |
253 | case Stmt::OMPTaskwaitDirectiveClass: |
254 | EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S)); |
255 | break; |
256 | case Stmt::OMPTaskgroupDirectiveClass: |
257 | EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S)); |
258 | break; |
259 | case Stmt::OMPFlushDirectiveClass: |
260 | EmitOMPFlushDirective(cast<OMPFlushDirective>(*S)); |
261 | break; |
262 | case Stmt::OMPDepobjDirectiveClass: |
263 | EmitOMPDepobjDirective(cast<OMPDepobjDirective>(*S)); |
264 | break; |
265 | case Stmt::OMPScanDirectiveClass: |
266 | EmitOMPScanDirective(cast<OMPScanDirective>(*S)); |
267 | break; |
268 | case Stmt::OMPOrderedDirectiveClass: |
269 | EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S)); |
270 | break; |
271 | case Stmt::OMPAtomicDirectiveClass: |
272 | EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S)); |
273 | break; |
274 | case Stmt::OMPTargetDirectiveClass: |
275 | EmitOMPTargetDirective(cast<OMPTargetDirective>(*S)); |
276 | break; |
277 | case Stmt::OMPTeamsDirectiveClass: |
278 | EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S)); |
279 | break; |
280 | case Stmt::OMPCancellationPointDirectiveClass: |
281 | EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S)); |
282 | break; |
283 | case Stmt::OMPCancelDirectiveClass: |
284 | EmitOMPCancelDirective(cast<OMPCancelDirective>(*S)); |
285 | break; |
286 | case Stmt::OMPTargetDataDirectiveClass: |
287 | EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S)); |
288 | break; |
289 | case Stmt::OMPTargetEnterDataDirectiveClass: |
290 | EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S)); |
291 | break; |
292 | case Stmt::OMPTargetExitDataDirectiveClass: |
293 | EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S)); |
294 | break; |
295 | case Stmt::OMPTargetParallelDirectiveClass: |
296 | EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S)); |
297 | break; |
298 | case Stmt::OMPTargetParallelForDirectiveClass: |
299 | EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S)); |
300 | break; |
301 | case Stmt::OMPTaskLoopDirectiveClass: |
302 | EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S)); |
303 | break; |
304 | case Stmt::OMPTaskLoopSimdDirectiveClass: |
305 | EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S)); |
306 | break; |
307 | case Stmt::OMPMasterTaskLoopDirectiveClass: |
308 | EmitOMPMasterTaskLoopDirective(cast<OMPMasterTaskLoopDirective>(*S)); |
309 | break; |
310 | case Stmt::OMPMasterTaskLoopSimdDirectiveClass: |
311 | EmitOMPMasterTaskLoopSimdDirective( |
312 | cast<OMPMasterTaskLoopSimdDirective>(*S)); |
313 | break; |
314 | case Stmt::OMPParallelMasterTaskLoopDirectiveClass: |
315 | EmitOMPParallelMasterTaskLoopDirective( |
316 | cast<OMPParallelMasterTaskLoopDirective>(*S)); |
317 | break; |
318 | case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass: |
319 | EmitOMPParallelMasterTaskLoopSimdDirective( |
320 | cast<OMPParallelMasterTaskLoopSimdDirective>(*S)); |
321 | break; |
322 | case Stmt::OMPDistributeDirectiveClass: |
323 | EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S)); |
324 | break; |
325 | case Stmt::OMPTargetUpdateDirectiveClass: |
326 | EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S)); |
327 | break; |
328 | case Stmt::OMPDistributeParallelForDirectiveClass: |
329 | EmitOMPDistributeParallelForDirective( |
330 | cast<OMPDistributeParallelForDirective>(*S)); |
331 | break; |
332 | case Stmt::OMPDistributeParallelForSimdDirectiveClass: |
333 | EmitOMPDistributeParallelForSimdDirective( |
334 | cast<OMPDistributeParallelForSimdDirective>(*S)); |
335 | break; |
336 | case Stmt::OMPDistributeSimdDirectiveClass: |
337 | EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S)); |
338 | break; |
339 | case Stmt::OMPTargetParallelForSimdDirectiveClass: |
340 | EmitOMPTargetParallelForSimdDirective( |
341 | cast<OMPTargetParallelForSimdDirective>(*S)); |
342 | break; |
343 | case Stmt::OMPTargetSimdDirectiveClass: |
344 | EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S)); |
345 | break; |
346 | case Stmt::OMPTeamsDistributeDirectiveClass: |
347 | EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S)); |
348 | break; |
349 | case Stmt::OMPTeamsDistributeSimdDirectiveClass: |
350 | EmitOMPTeamsDistributeSimdDirective( |
351 | cast<OMPTeamsDistributeSimdDirective>(*S)); |
352 | break; |
353 | case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass: |
354 | EmitOMPTeamsDistributeParallelForSimdDirective( |
355 | cast<OMPTeamsDistributeParallelForSimdDirective>(*S)); |
356 | break; |
357 | case Stmt::OMPTeamsDistributeParallelForDirectiveClass: |
358 | EmitOMPTeamsDistributeParallelForDirective( |
359 | cast<OMPTeamsDistributeParallelForDirective>(*S)); |
360 | break; |
361 | case Stmt::OMPTargetTeamsDirectiveClass: |
362 | EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S)); |
363 | break; |
364 | case Stmt::OMPTargetTeamsDistributeDirectiveClass: |
365 | EmitOMPTargetTeamsDistributeDirective( |
366 | cast<OMPTargetTeamsDistributeDirective>(*S)); |
367 | break; |
368 | case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass: |
369 | EmitOMPTargetTeamsDistributeParallelForDirective( |
370 | cast<OMPTargetTeamsDistributeParallelForDirective>(*S)); |
371 | break; |
372 | case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass: |
373 | EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
374 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S)); |
375 | break; |
376 | case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass: |
377 | EmitOMPTargetTeamsDistributeSimdDirective( |
378 | cast<OMPTargetTeamsDistributeSimdDirective>(*S)); |
379 | break; |
380 | case Stmt::OMPInteropDirectiveClass: |
381 | llvm_unreachable("Interop directive not supported yet." ); |
382 | break; |
383 | case Stmt::OMPDispatchDirectiveClass: |
384 | llvm_unreachable("Dispatch directive not supported yet." ); |
385 | break; |
386 | case Stmt::OMPMaskedDirectiveClass: |
387 | EmitOMPMaskedDirective(cast<OMPMaskedDirective>(*S)); |
388 | break; |
389 | } |
390 | } |
391 | |
392 | bool CodeGenFunction::EmitSimpleStmt(const Stmt *S, |
393 | ArrayRef<const Attr *> Attrs) { |
394 | switch (S->getStmtClass()) { |
395 | default: |
396 | return false; |
397 | case Stmt::NullStmtClass: |
398 | break; |
399 | case Stmt::CompoundStmtClass: |
400 | EmitCompoundStmt(cast<CompoundStmt>(*S)); |
401 | break; |
402 | case Stmt::DeclStmtClass: |
403 | EmitDeclStmt(cast<DeclStmt>(*S)); |
404 | break; |
405 | case Stmt::LabelStmtClass: |
406 | EmitLabelStmt(cast<LabelStmt>(*S)); |
407 | break; |
408 | case Stmt::AttributedStmtClass: |
409 | EmitAttributedStmt(cast<AttributedStmt>(*S)); |
410 | break; |
411 | case Stmt::GotoStmtClass: |
412 | EmitGotoStmt(cast<GotoStmt>(*S)); |
413 | break; |
414 | case Stmt::BreakStmtClass: |
415 | EmitBreakStmt(cast<BreakStmt>(*S)); |
416 | break; |
417 | case Stmt::ContinueStmtClass: |
418 | EmitContinueStmt(cast<ContinueStmt>(*S)); |
419 | break; |
420 | case Stmt::DefaultStmtClass: |
421 | EmitDefaultStmt(cast<DefaultStmt>(*S), Attrs); |
422 | break; |
423 | case Stmt::CaseStmtClass: |
424 | EmitCaseStmt(cast<CaseStmt>(*S), Attrs); |
425 | break; |
426 | case Stmt::SEHLeaveStmtClass: |
427 | EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); |
428 | break; |
429 | } |
430 | return true; |
431 | } |
432 | |
433 | /// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true, |
434 | /// this captures the expression result of the last sub-statement and returns it |
435 | /// (for use by the statement expression extension). |
436 | Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
437 | AggValueSlot AggSlot) { |
438 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), |
439 | "LLVM IR generation of compound statement ('{}')" ); |
440 | |
441 | // Keep track of the current cleanup stack depth, including debug scopes. |
442 | LexicalScope Scope(*this, S.getSourceRange()); |
443 | |
444 | return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); |
445 | } |
446 | |
447 | Address |
448 | CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, |
449 | bool GetLast, |
450 | AggValueSlot AggSlot) { |
451 | |
452 | const Stmt *ExprResult = S.getStmtExprResult(); |
453 | assert((!GetLast || (GetLast && ExprResult)) && |
454 | "If GetLast is true then the CompoundStmt must have a StmtExprResult" ); |
455 | |
456 | Address RetAlloca = Address::invalid(); |
457 | |
458 | for (auto *CurStmt : S.body()) { |
459 | if (GetLast && ExprResult == CurStmt) { |
460 | // We have to special case labels here. They are statements, but when put |
461 | // at the end of a statement expression, they yield the value of their |
462 | // subexpression. Handle this by walking through all labels we encounter, |
463 | // emitting them before we evaluate the subexpr. |
464 | // Similar issues arise for attributed statements. |
465 | while (!isa<Expr>(ExprResult)) { |
466 | if (const auto *LS = dyn_cast<LabelStmt>(ExprResult)) { |
467 | EmitLabel(LS->getDecl()); |
468 | ExprResult = LS->getSubStmt(); |
469 | } else if (const auto *AS = dyn_cast<AttributedStmt>(ExprResult)) { |
470 | // FIXME: Update this if we ever have attributes that affect the |
471 | // semantics of an expression. |
472 | ExprResult = AS->getSubStmt(); |
473 | } else { |
474 | llvm_unreachable("unknown value statement" ); |
475 | } |
476 | } |
477 | |
478 | EnsureInsertPoint(); |
479 | |
480 | const Expr *E = cast<Expr>(ExprResult); |
481 | QualType ExprTy = E->getType(); |
482 | if (hasAggregateEvaluationKind(ExprTy)) { |
483 | EmitAggExpr(E, AggSlot); |
484 | } else { |
485 | // We can't return an RValue here because there might be cleanups at |
486 | // the end of the StmtExpr. Because of that, we have to emit the result |
487 | // here into a temporary alloca. |
488 | RetAlloca = CreateMemTemp(ExprTy); |
489 | EmitAnyExprToMem(E, RetAlloca, Qualifiers(), |
490 | /*IsInit*/ false); |
491 | } |
492 | } else { |
493 | EmitStmt(CurStmt); |
494 | } |
495 | } |
496 | |
497 | return RetAlloca; |
498 | } |
499 | |
500 | void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { |
501 | llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); |
502 | |
503 | // If there is a cleanup stack, then we it isn't worth trying to |
504 | // simplify this block (we would need to remove it from the scope map |
505 | // and cleanup entry). |
506 | if (!EHStack.empty()) |
507 | return; |
508 | |
509 | // Can only simplify direct branches. |
510 | if (!BI || !BI->isUnconditional()) |
511 | return; |
512 | |
513 | // Can only simplify empty blocks. |
514 | if (BI->getIterator() != BB->begin()) |
515 | return; |
516 | |
517 | BB->replaceAllUsesWith(BI->getSuccessor(0)); |
518 | BI->eraseFromParent(); |
519 | BB->eraseFromParent(); |
520 | } |
521 | |
522 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { |
523 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
524 | |
525 | // Fall out of the current block (if necessary). |
526 | EmitBranch(BB); |
527 | |
528 | if (IsFinished && BB->use_empty()) { |
529 | delete BB; |
530 | return; |
531 | } |
532 | |
533 | // Place the block after the current block, if possible, or else at |
534 | // the end of the function. |
535 | if (CurBB && CurBB->getParent()) |
536 | CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB); |
537 | else |
538 | CurFn->getBasicBlockList().push_back(BB); |
539 | Builder.SetInsertPoint(BB); |
540 | } |
541 | |
542 | void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { |
543 | // Emit a branch from the current block to the target one if this |
544 | // was a real block. If this was just a fall-through block after a |
545 | // terminator, don't emit it. |
546 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
547 | |
548 | if (!CurBB || CurBB->getTerminator()) { |
549 | // If there is no insert point or the previous block is already |
550 | // terminated, don't touch it. |
551 | } else { |
552 | // Otherwise, create a fall-through branch. |
553 | Builder.CreateBr(Target); |
554 | } |
555 | |
556 | Builder.ClearInsertionPoint(); |
557 | } |
558 | |
559 | void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { |
560 | bool inserted = false; |
561 | for (llvm::User *u : block->users()) { |
562 | if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) { |
563 | CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(), |
564 | block); |
565 | inserted = true; |
566 | break; |
567 | } |
568 | } |
569 | |
570 | if (!inserted) |
571 | CurFn->getBasicBlockList().push_back(block); |
572 | |
573 | Builder.SetInsertPoint(block); |
574 | } |
575 | |
576 | CodeGenFunction::JumpDest |
577 | CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { |
578 | JumpDest &Dest = LabelMap[D]; |
579 | if (Dest.isValid()) return Dest; |
580 | |
581 | // Create, but don't insert, the new block. |
582 | Dest = JumpDest(createBasicBlock(D->getName()), |
583 | EHScopeStack::stable_iterator::invalid(), |
584 | NextCleanupDestIndex++); |
585 | return Dest; |
586 | } |
587 | |
588 | void CodeGenFunction::EmitLabel(const LabelDecl *D) { |
589 | // Add this label to the current lexical scope if we're within any |
590 | // normal cleanups. Jumps "in" to this label --- when permitted by |
591 | // the language --- may need to be routed around such cleanups. |
592 | if (EHStack.hasNormalCleanups() && CurLexicalScope) |
593 | CurLexicalScope->addLabel(D); |
594 | |
595 | JumpDest &Dest = LabelMap[D]; |
596 | |
597 | // If we didn't need a forward reference to this label, just go |
598 | // ahead and create a destination at the current scope. |
599 | if (!Dest.isValid()) { |
600 | Dest = getJumpDestInCurrentScope(D->getName()); |
601 | |
602 | // Otherwise, we need to give this label a target depth and remove |
603 | // it from the branch-fixups list. |
604 | } else { |
605 | assert(!Dest.getScopeDepth().isValid() && "already emitted label!" ); |
606 | Dest.setScopeDepth(EHStack.stable_begin()); |
607 | ResolveBranchFixups(Dest.getBlock()); |
608 | } |
609 | |
610 | EmitBlock(Dest.getBlock()); |
611 | |
612 | // Emit debug info for labels. |
613 | if (CGDebugInfo *DI = getDebugInfo()) { |
614 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) { |
615 | DI->setLocation(D->getLocation()); |
616 | DI->EmitLabel(D, Builder); |
617 | } |
618 | } |
619 | |
620 | incrementProfileCounter(D->getStmt()); |
621 | } |
622 | |
623 | /// Change the cleanup scope of the labels in this lexical scope to |
624 | /// match the scope of the enclosing context. |
625 | void CodeGenFunction::LexicalScope::rescopeLabels() { |
626 | assert(!Labels.empty()); |
627 | EHScopeStack::stable_iterator innermostScope |
628 | = CGF.EHStack.getInnermostNormalCleanup(); |
629 | |
630 | // Change the scope depth of all the labels. |
631 | for (SmallVectorImpl<const LabelDecl*>::const_iterator |
632 | i = Labels.begin(), e = Labels.end(); i != e; ++i) { |
633 | assert(CGF.LabelMap.count(*i)); |
634 | JumpDest &dest = CGF.LabelMap.find(*i)->second; |
635 | assert(dest.getScopeDepth().isValid()); |
636 | assert(innermostScope.encloses(dest.getScopeDepth())); |
637 | dest.setScopeDepth(innermostScope); |
638 | } |
639 | |
640 | // Reparent the labels if the new scope also has cleanups. |
641 | if (innermostScope != EHScopeStack::stable_end() && ParentScope) { |
642 | ParentScope->Labels.append(Labels.begin(), Labels.end()); |
643 | } |
644 | } |
645 | |
646 | |
647 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
648 | EmitLabel(S.getDecl()); |
649 | EmitStmt(S.getSubStmt()); |
650 | } |
651 | |
652 | void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { |
653 | bool nomerge = false; |
654 | const CallExpr *musttail = nullptr; |
655 | |
656 | for (const auto *A : S.getAttrs()) { |
657 | if (A->getKind() == attr::NoMerge) { |
658 | nomerge = true; |
659 | } |
660 | if (A->getKind() == attr::MustTail) { |
661 | const Stmt *Sub = S.getSubStmt(); |
662 | const ReturnStmt *R = cast<ReturnStmt>(Sub); |
663 | musttail = cast<CallExpr>(R->getRetValue()->IgnoreParens()); |
664 | } |
665 | } |
666 | SaveAndRestore<bool> save_nomerge(InNoMergeAttributedStmt, nomerge); |
667 | SaveAndRestore<const CallExpr *> save_musttail(MustTailCall, musttail); |
668 | EmitStmt(S.getSubStmt(), S.getAttrs()); |
669 | } |
670 | |
671 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
672 | // If this code is reachable then emit a stop point (if generating |
673 | // debug info). We have to do this ourselves because we are on the |
674 | // "simple" statement path. |
675 | if (HaveInsertPoint()) |
676 | EmitStopPoint(&S); |
677 | |
678 | EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); |
679 | } |
680 | |
681 | |
682 | void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { |
683 | if (const LabelDecl *Target = S.getConstantTarget()) { |
684 | EmitBranchThroughCleanup(getJumpDestForLabel(Target)); |
685 | return; |
686 | } |
687 | |
688 | // Ensure that we have an i8* for our PHI node. |
689 | llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), |
690 | Int8PtrTy, "addr" ); |
691 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
692 | |
693 | // Get the basic block for the indirect goto. |
694 | llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); |
695 | |
696 | // The first instruction in the block has to be the PHI for the switch dest, |
697 | // add an entry for this branch. |
698 | cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); |
699 | |
700 | EmitBranch(IndGotoBB); |
701 | } |
702 | |
703 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
704 | // C99 6.8.4.1: The first substatement is executed if the expression compares |
705 | // unequal to 0. The condition must be a scalar type. |
706 | LexicalScope ConditionScope(*this, S.getCond()->getSourceRange()); |
707 | |
708 | if (S.getInit()) |
709 | EmitStmt(S.getInit()); |
710 | |
711 | if (S.getConditionVariable()) |
712 | EmitDecl(*S.getConditionVariable()); |
713 | |
714 | // If the condition constant folds and can be elided, try to avoid emitting |
715 | // the condition and the dead arm of the if/else. |
716 | bool CondConstant; |
717 | if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant, |
718 | S.isConstexpr())) { |
719 | // Figure out which block (then or else) is executed. |
720 | const Stmt *Executed = S.getThen(); |
721 | const Stmt *Skipped = S.getElse(); |
722 | if (!CondConstant) // Condition false? |
723 | std::swap(Executed, Skipped); |
724 | |
725 | // If the skipped block has no labels in it, just emit the executed block. |
726 | // This avoids emitting dead code and simplifies the CFG substantially. |
727 | if (S.isConstexpr() || !ContainsLabel(Skipped)) { |
728 | if (CondConstant) |
729 | incrementProfileCounter(&S); |
730 | if (Executed) { |
731 | RunCleanupsScope ExecutedScope(*this); |
732 | EmitStmt(Executed); |
733 | } |
734 | return; |
735 | } |
736 | } |
737 | |
738 | // Otherwise, the condition did not fold, or we couldn't elide it. Just emit |
739 | // the conditional branch. |
740 | llvm::BasicBlock *ThenBlock = createBasicBlock("if.then" ); |
741 | llvm::BasicBlock *ContBlock = createBasicBlock("if.end" ); |
742 | llvm::BasicBlock *ElseBlock = ContBlock; |
743 | if (S.getElse()) |
744 | ElseBlock = createBasicBlock("if.else" ); |
745 | |
746 | // Prefer the PGO based weights over the likelihood attribute. |
747 | // When the build isn't optimized the metadata isn't used, so don't generate |
748 | // it. |
749 | Stmt::Likelihood LH = Stmt::LH_None; |
750 | uint64_t Count = getProfileCount(S.getThen()); |
751 | if (!Count && CGM.getCodeGenOpts().OptimizationLevel) |
752 | LH = Stmt::getLikelihood(S.getThen(), S.getElse()); |
753 | EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, Count, LH); |
754 | |
755 | // Emit the 'then' code. |
756 | EmitBlock(ThenBlock); |
757 | incrementProfileCounter(&S); |
758 | { |
759 | RunCleanupsScope ThenScope(*this); |
760 | EmitStmt(S.getThen()); |
761 | } |
762 | EmitBranch(ContBlock); |
763 | |
764 | // Emit the 'else' code if present. |
765 | if (const Stmt *Else = S.getElse()) { |
766 | { |
767 | // There is no need to emit line number for an unconditional branch. |
768 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
769 | EmitBlock(ElseBlock); |
770 | } |
771 | { |
772 | RunCleanupsScope ElseScope(*this); |
773 | EmitStmt(Else); |
774 | } |
775 | { |
776 | // There is no need to emit line number for an unconditional branch. |
777 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
778 | EmitBranch(ContBlock); |
779 | } |
780 | } |
781 | |
782 | // Emit the continuation block for code after the if. |
783 | EmitBlock(ContBlock, true); |
784 | } |
785 | |
786 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S, |
787 | ArrayRef<const Attr *> WhileAttrs) { |
788 | // Emit the header for the loop, which will also become |
789 | // the continue target. |
790 | JumpDest = getJumpDestInCurrentScope("while.cond" ); |
791 | EmitBlock(LoopHeader.getBlock()); |
792 | |
793 | // Create an exit block for when the condition fails, which will |
794 | // also become the break target. |
795 | JumpDest LoopExit = getJumpDestInCurrentScope("while.end" ); |
796 | |
797 | // Store the blocks to use for break and continue. |
798 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); |
799 | |
800 | // C++ [stmt.while]p2: |
801 | // When the condition of a while statement is a declaration, the |
802 | // scope of the variable that is declared extends from its point |
803 | // of declaration (3.3.2) to the end of the while statement. |
804 | // [...] |
805 | // The object created in a condition is destroyed and created |
806 | // with each iteration of the loop. |
807 | RunCleanupsScope ConditionScope(*this); |
808 | |
809 | if (S.getConditionVariable()) |
810 | EmitDecl(*S.getConditionVariable()); |
811 | |
812 | // Evaluate the conditional in the while header. C99 6.8.5.1: The |
813 | // evaluation of the controlling expression takes place before each |
814 | // execution of the loop body. |
815 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
816 | |
817 | // while(1) is common, avoid extra exit blocks. Be sure |
818 | // to correctly handle break/continue though. |
819 | llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal); |
820 | bool CondIsConstInt = C != nullptr; |
821 | bool EmitBoolCondBranch = !CondIsConstInt || !C->isOne(); |
822 | const SourceRange &R = S.getSourceRange(); |
823 | LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), CGM.getCodeGenOpts(), |
824 | WhileAttrs, SourceLocToDebugLoc(R.getBegin()), |
825 | SourceLocToDebugLoc(R.getEnd()), |
826 | checkIfLoopMustProgress(CondIsConstInt)); |
827 | |
828 | // As long as the condition is true, go to the loop body. |
829 | llvm::BasicBlock *LoopBody = createBasicBlock("while.body" ); |
830 | if (EmitBoolCondBranch) { |
831 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
832 | if (ConditionScope.requiresCleanups()) |
833 | ExitBlock = createBasicBlock("while.exit" ); |
834 | llvm::MDNode *Weights = |
835 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); |
836 | if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) |
837 | BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( |
838 | BoolCondVal, Stmt::getLikelihood(S.getBody())); |
839 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock, Weights); |
840 | |
841 | if (ExitBlock != LoopExit.getBlock()) { |
842 | EmitBlock(ExitBlock); |
843 | EmitBranchThroughCleanup(LoopExit); |
844 | } |
845 | } else if (const Attr *A = Stmt::getLikelihoodAttr(S.getBody())) { |
846 | CGM.getDiags().Report(A->getLocation(), |
847 | diag::warn_attribute_has_no_effect_on_infinite_loop) |
848 | << A << A->getRange(); |
849 | CGM.getDiags().Report( |
850 | S.getWhileLoc(), |
851 | diag::note_attribute_has_no_effect_on_infinite_loop_here) |
852 | << SourceRange(S.getWhileLoc(), S.getRParenLoc()); |
853 | } |
854 | |
855 | // Emit the loop body. We have to emit this in a cleanup scope |
856 | // because it might be a singleton DeclStmt. |
857 | { |
858 | RunCleanupsScope BodyScope(*this); |
859 | EmitBlock(LoopBody); |
860 | incrementProfileCounter(&S); |
861 | EmitStmt(S.getBody()); |
862 | } |
863 | |
864 | BreakContinueStack.pop_back(); |
865 | |
866 | // Immediately force cleanup. |
867 | ConditionScope.ForceCleanup(); |
868 | |
869 | EmitStopPoint(&S); |
870 | // Branch to the loop header again. |
871 | EmitBranch(LoopHeader.getBlock()); |
872 | |
873 | LoopStack.pop(); |
874 | |
875 | // Emit the exit block. |
876 | EmitBlock(LoopExit.getBlock(), true); |
877 | |
878 | // The LoopHeader typically is just a branch if we skipped emitting |
879 | // a branch, try to erase it. |
880 | if (!EmitBoolCondBranch) |
881 | SimplifyForwardingBlocks(LoopHeader.getBlock()); |
882 | } |
883 | |
884 | void CodeGenFunction::EmitDoStmt(const DoStmt &S, |
885 | ArrayRef<const Attr *> DoAttrs) { |
886 | JumpDest LoopExit = getJumpDestInCurrentScope("do.end" ); |
887 | JumpDest LoopCond = getJumpDestInCurrentScope("do.cond" ); |
888 | |
889 | uint64_t ParentCount = getCurrentProfileCount(); |
890 | |
891 | // Store the blocks to use for break and continue. |
892 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); |
893 | |
894 | // Emit the body of the loop. |
895 | llvm::BasicBlock *LoopBody = createBasicBlock("do.body" ); |
896 | |
897 | EmitBlockWithFallThrough(LoopBody, &S); |
898 | { |
899 | RunCleanupsScope BodyScope(*this); |
900 | EmitStmt(S.getBody()); |
901 | } |
902 | |
903 | EmitBlock(LoopCond.getBlock()); |
904 | |
905 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
906 | // after each execution of the loop body." |
907 | |
908 | // Evaluate the conditional in the while header. |
909 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
910 | // compares unequal to 0. The condition must be a scalar type. |
911 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
912 | |
913 | BreakContinueStack.pop_back(); |
914 | |
915 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
916 | // to correctly handle break/continue though. |
917 | llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal); |
918 | bool CondIsConstInt = C; |
919 | bool EmitBoolCondBranch = !C || !C->isZero(); |
920 | |
921 | const SourceRange &R = S.getSourceRange(); |
922 | LoopStack.push(LoopBody, CGM.getContext(), CGM.getCodeGenOpts(), DoAttrs, |
923 | SourceLocToDebugLoc(R.getBegin()), |
924 | SourceLocToDebugLoc(R.getEnd()), |
925 | checkIfLoopMustProgress(CondIsConstInt)); |
926 | |
927 | // As long as the condition is true, iterate the loop. |
928 | if (EmitBoolCondBranch) { |
929 | uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount; |
930 | Builder.CreateCondBr( |
931 | BoolCondVal, LoopBody, LoopExit.getBlock(), |
932 | createProfileWeightsForLoop(S.getCond(), BackedgeCount)); |
933 | } |
934 | |
935 | LoopStack.pop(); |
936 | |
937 | // Emit the exit block. |
938 | EmitBlock(LoopExit.getBlock()); |
939 | |
940 | // The DoCond block typically is just a branch if we skipped |
941 | // emitting a branch, try to erase it. |
942 | if (!EmitBoolCondBranch) |
943 | SimplifyForwardingBlocks(LoopCond.getBlock()); |
944 | } |
945 | |
946 | void CodeGenFunction::EmitForStmt(const ForStmt &S, |
947 | ArrayRef<const Attr *> ForAttrs) { |
948 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end" ); |
949 | |
950 | LexicalScope ForScope(*this, S.getSourceRange()); |
951 | |
952 | // Evaluate the first part before the loop. |
953 | if (S.getInit()) |
954 | EmitStmt(S.getInit()); |
955 | |
956 | // Start the loop with a block that tests the condition. |
957 | // If there's an increment, the continue scope will be overwritten |
958 | // later. |
959 | JumpDest CondDest = getJumpDestInCurrentScope("for.cond" ); |
960 | llvm::BasicBlock *CondBlock = CondDest.getBlock(); |
961 | EmitBlock(CondBlock); |
962 | |
963 | Expr::EvalResult Result; |
964 | bool CondIsConstInt = |
965 | !S.getCond() || S.getCond()->EvaluateAsInt(Result, getContext()); |
966 | |
967 | const SourceRange &R = S.getSourceRange(); |
968 | LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs, |
969 | SourceLocToDebugLoc(R.getBegin()), |
970 | SourceLocToDebugLoc(R.getEnd()), |
971 | checkIfLoopMustProgress(CondIsConstInt)); |
972 | |
973 | // Create a cleanup scope for the condition variable cleanups. |
974 | LexicalScope ConditionScope(*this, S.getSourceRange()); |
975 | |
976 | // If the for loop doesn't have an increment we can just use the condition as |
977 | // the continue block. Otherwise, if there is no condition variable, we can |
978 | // form the continue block now. If there is a condition variable, we can't |
979 | // form the continue block until after we've emitted the condition, because |
980 | // the condition is in scope in the increment, but Sema's jump diagnostics |
981 | // ensure that there are no continues from the condition variable that jump |
982 | // to the loop increment. |
983 | JumpDest Continue; |
984 | if (!S.getInc()) |
985 | Continue = CondDest; |
986 | else if (!S.getConditionVariable()) |
987 | Continue = getJumpDestInCurrentScope("for.inc" ); |
988 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
989 | |
990 | if (S.getCond()) { |
991 | // If the for statement has a condition scope, emit the local variable |
992 | // declaration. |
993 | if (S.getConditionVariable()) { |
994 | EmitDecl(*S.getConditionVariable()); |
995 | |
996 | // We have entered the condition variable's scope, so we're now able to |
997 | // jump to the continue block. |
998 | Continue = S.getInc() ? getJumpDestInCurrentScope("for.inc" ) : CondDest; |
999 | BreakContinueStack.back().ContinueBlock = Continue; |
1000 | } |
1001 | |
1002 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
1003 | // If there are any cleanups between here and the loop-exit scope, |
1004 | // create a block to stage a loop exit along. |
1005 | if (ForScope.requiresCleanups()) |
1006 | ExitBlock = createBasicBlock("for.cond.cleanup" ); |
1007 | |
1008 | // As long as the condition is true, iterate the loop. |
1009 | llvm::BasicBlock *ForBody = createBasicBlock("for.body" ); |
1010 | |
1011 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
1012 | // compares unequal to 0. The condition must be a scalar type. |
1013 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
1014 | llvm::MDNode *Weights = |
1015 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); |
1016 | if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) |
1017 | BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( |
1018 | BoolCondVal, Stmt::getLikelihood(S.getBody())); |
1019 | |
1020 | Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights); |
1021 | |
1022 | if (ExitBlock != LoopExit.getBlock()) { |
1023 | EmitBlock(ExitBlock); |
1024 | EmitBranchThroughCleanup(LoopExit); |
1025 | } |
1026 | |
1027 | EmitBlock(ForBody); |
1028 | } else { |
1029 | // Treat it as a non-zero constant. Don't even create a new block for the |
1030 | // body, just fall into it. |
1031 | } |
1032 | incrementProfileCounter(&S); |
1033 | |
1034 | { |
1035 | // Create a separate cleanup scope for the body, in case it is not |
1036 | // a compound statement. |
1037 | RunCleanupsScope BodyScope(*this); |
1038 | EmitStmt(S.getBody()); |
1039 | } |
1040 | |
1041 | // If there is an increment, emit it next. |
1042 | if (S.getInc()) { |
1043 | EmitBlock(Continue.getBlock()); |
1044 | EmitStmt(S.getInc()); |
1045 | } |
1046 | |
1047 | BreakContinueStack.pop_back(); |
1048 | |
1049 | ConditionScope.ForceCleanup(); |
1050 | |
1051 | EmitStopPoint(&S); |
1052 | EmitBranch(CondBlock); |
1053 | |
1054 | ForScope.ForceCleanup(); |
1055 | |
1056 | LoopStack.pop(); |
1057 | |
1058 | // Emit the fall-through block. |
1059 | EmitBlock(LoopExit.getBlock(), true); |
1060 | } |
1061 | |
1062 | void |
1063 | CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S, |
1064 | ArrayRef<const Attr *> ForAttrs) { |
1065 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end" ); |
1066 | |
1067 | LexicalScope ForScope(*this, S.getSourceRange()); |
1068 | |
1069 | // Evaluate the first pieces before the loop. |
1070 | if (S.getInit()) |
1071 | EmitStmt(S.getInit()); |
1072 | EmitStmt(S.getRangeStmt()); |
1073 | EmitStmt(S.getBeginStmt()); |
1074 | EmitStmt(S.getEndStmt()); |
1075 | |
1076 | // Start the loop with a block that tests the condition. |
1077 | // If there's an increment, the continue scope will be overwritten |
1078 | // later. |
1079 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond" ); |
1080 | EmitBlock(CondBlock); |
1081 | |
1082 | const SourceRange &R = S.getSourceRange(); |
1083 | LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), ForAttrs, |
1084 | SourceLocToDebugLoc(R.getBegin()), |
1085 | SourceLocToDebugLoc(R.getEnd())); |
1086 | |
1087 | // If there are any cleanups between here and the loop-exit scope, |
1088 | // create a block to stage a loop exit along. |
1089 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
1090 | if (ForScope.requiresCleanups()) |
1091 | ExitBlock = createBasicBlock("for.cond.cleanup" ); |
1092 | |
1093 | // The loop body, consisting of the specified body and the loop variable. |
1094 | llvm::BasicBlock *ForBody = createBasicBlock("for.body" ); |
1095 | |
1096 | // The body is executed if the expression, contextually converted |
1097 | // to bool, is true. |
1098 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
1099 | llvm::MDNode *Weights = |
1100 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())); |
1101 | if (!Weights && CGM.getCodeGenOpts().OptimizationLevel) |
1102 | BoolCondVal = emitCondLikelihoodViaExpectIntrinsic( |
1103 | BoolCondVal, Stmt::getLikelihood(S.getBody())); |
1104 | Builder.CreateCondBr(BoolCondVal, ForBody, ExitBlock, Weights); |
1105 | |
1106 | if (ExitBlock != LoopExit.getBlock()) { |
1107 | EmitBlock(ExitBlock); |
1108 | EmitBranchThroughCleanup(LoopExit); |
1109 | } |
1110 | |
1111 | EmitBlock(ForBody); |
1112 | incrementProfileCounter(&S); |
1113 | |
1114 | // Create a block for the increment. In case of a 'continue', we jump there. |
1115 | JumpDest Continue = getJumpDestInCurrentScope("for.inc" ); |
1116 | |
1117 | // Store the blocks to use for break and continue. |
1118 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
1119 | |
1120 | { |
1121 | // Create a separate cleanup scope for the loop variable and body. |
1122 | LexicalScope BodyScope(*this, S.getSourceRange()); |
1123 | EmitStmt(S.getLoopVarStmt()); |
1124 | EmitStmt(S.getBody()); |
1125 | } |
1126 | |
1127 | EmitStopPoint(&S); |
1128 | // If there is an increment, emit it next. |
1129 | EmitBlock(Continue.getBlock()); |
1130 | EmitStmt(S.getInc()); |
1131 | |
1132 | BreakContinueStack.pop_back(); |
1133 | |
1134 | EmitBranch(CondBlock); |
1135 | |
1136 | ForScope.ForceCleanup(); |
1137 | |
1138 | LoopStack.pop(); |
1139 | |
1140 | // Emit the fall-through block. |
1141 | EmitBlock(LoopExit.getBlock(), true); |
1142 | } |
1143 | |
1144 | void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { |
1145 | if (RV.isScalar()) { |
1146 | Builder.CreateStore(RV.getScalarVal(), ReturnValue); |
1147 | } else if (RV.isAggregate()) { |
1148 | LValue Dest = MakeAddrLValue(ReturnValue, Ty); |
1149 | LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty); |
1150 | EmitAggregateCopy(Dest, Src, Ty, getOverlapForReturnValue()); |
1151 | } else { |
1152 | EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty), |
1153 | /*init*/ true); |
1154 | } |
1155 | EmitBranchThroughCleanup(ReturnBlock); |
1156 | } |
1157 | |
1158 | namespace { |
1159 | // RAII struct used to save and restore a return statment's result expression. |
1160 | struct SaveRetExprRAII { |
1161 | SaveRetExprRAII(const Expr *RetExpr, CodeGenFunction &CGF) |
1162 | : OldRetExpr(CGF.RetExpr), CGF(CGF) { |
1163 | CGF.RetExpr = RetExpr; |
1164 | } |
1165 | ~SaveRetExprRAII() { CGF.RetExpr = OldRetExpr; } |
1166 | const Expr *OldRetExpr; |
1167 | CodeGenFunction &CGF; |
1168 | }; |
1169 | } // namespace |
1170 | |
1171 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
1172 | /// if the function returns void, or may be missing one if the function returns |
1173 | /// non-void. Fun stuff :). |
1174 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
1175 | if (requiresReturnValueCheck()) { |
1176 | llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc()); |
1177 | auto *SLocPtr = |
1178 | new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false, |
1179 | llvm::GlobalVariable::PrivateLinkage, SLoc); |
1180 | SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
1181 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr); |
1182 | assert(ReturnLocation.isValid() && "No valid return location" ); |
1183 | Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy), |
1184 | ReturnLocation); |
1185 | } |
1186 | |
1187 | // Returning from an outlined SEH helper is UB, and we already warn on it. |
1188 | if (IsOutlinedSEHHelper) { |
1189 | Builder.CreateUnreachable(); |
1190 | Builder.ClearInsertionPoint(); |
1191 | } |
1192 | |
1193 | // Emit the result value, even if unused, to evaluate the side effects. |
1194 | const Expr *RV = S.getRetValue(); |
1195 | |
1196 | // Record the result expression of the return statement. The recorded |
1197 | // expression is used to determine whether a block capture's lifetime should |
1198 | // end at the end of the full expression as opposed to the end of the scope |
1199 | // enclosing the block expression. |
1200 | // |
1201 | // This permits a small, easily-implemented exception to our over-conservative |
1202 | // rules about not jumping to statements following block literals with |
1203 | // non-trivial cleanups. |
1204 | SaveRetExprRAII SaveRetExpr(RV, *this); |
1205 | |
1206 | RunCleanupsScope cleanupScope(*this); |
1207 | if (const auto *EWC = dyn_cast_or_null<ExprWithCleanups>(RV)) |
1208 | RV = EWC->getSubExpr(); |
1209 | // FIXME: Clean this up by using an LValue for ReturnTemp, |
1210 | // EmitStoreThroughLValue, and EmitAnyExpr. |
1211 | // Check if the NRVO candidate was not globalized in OpenMP mode. |
1212 | if (getLangOpts().ElideConstructors && S.getNRVOCandidate() && |
1213 | S.getNRVOCandidate()->isNRVOVariable() && |
1214 | (!getLangOpts().OpenMP || |
1215 | !CGM.getOpenMPRuntime() |
1216 | .getAddressOfLocalVariable(*this, S.getNRVOCandidate()) |
1217 | .isValid())) { |
1218 | // Apply the named return value optimization for this return statement, |
1219 | // which means doing nothing: the appropriate result has already been |
1220 | // constructed into the NRVO variable. |
1221 | |
1222 | // If there is an NRVO flag for this variable, set it to 1 into indicate |
1223 | // that the cleanup code should not destroy the variable. |
1224 | if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) |
1225 | Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag); |
1226 | } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) { |
1227 | // Make sure not to return anything, but evaluate the expression |
1228 | // for side effects. |
1229 | if (RV) |
1230 | EmitAnyExpr(RV); |
1231 | } else if (!RV) { |
1232 | // Do nothing (return value is left uninitialized) |
1233 | } else if (FnRetTy->isReferenceType()) { |
1234 | // If this function returns a reference, take the address of the expression |
1235 | // rather than the value. |
1236 | RValue Result = EmitReferenceBindingToExpr(RV); |
1237 | Builder.CreateStore(Result.getScalarVal(), ReturnValue); |
1238 | } else { |
1239 | switch (getEvaluationKind(RV->getType())) { |
1240 | case TEK_Scalar: |
1241 | Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); |
1242 | break; |
1243 | case TEK_Complex: |
1244 | EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()), |
1245 | /*isInit*/ true); |
1246 | break; |
1247 | case TEK_Aggregate: |
1248 | EmitAggExpr(RV, AggValueSlot::forAddr( |
1249 | ReturnValue, Qualifiers(), |
1250 | AggValueSlot::IsDestructed, |
1251 | AggValueSlot::DoesNotNeedGCBarriers, |
1252 | AggValueSlot::IsNotAliased, |
1253 | getOverlapForReturnValue())); |
1254 | break; |
1255 | } |
1256 | } |
1257 | |
1258 | ++NumReturnExprs; |
1259 | if (!RV || RV->isEvaluatable(getContext())) |
1260 | ++NumSimpleReturnExprs; |
1261 | |
1262 | cleanupScope.ForceCleanup(); |
1263 | EmitBranchThroughCleanup(ReturnBlock); |
1264 | } |
1265 | |
1266 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
1267 | // As long as debug info is modeled with instructions, we have to ensure we |
1268 | // have a place to insert here and write the stop point here. |
1269 | if (HaveInsertPoint()) |
1270 | EmitStopPoint(&S); |
1271 | |
1272 | for (const auto *I : S.decls()) |
1273 | EmitDecl(*I); |
1274 | } |
1275 | |
1276 | void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { |
1277 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!" ); |
1278 | |
1279 | // If this code is reachable then emit a stop point (if generating |
1280 | // debug info). We have to do this ourselves because we are on the |
1281 | // "simple" statement path. |
1282 | if (HaveInsertPoint()) |
1283 | EmitStopPoint(&S); |
1284 | |
1285 | EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock); |
1286 | } |
1287 | |
1288 | void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { |
1289 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!" ); |
1290 | |
1291 | // If this code is reachable then emit a stop point (if generating |
1292 | // debug info). We have to do this ourselves because we are on the |
1293 | // "simple" statement path. |
1294 | if (HaveInsertPoint()) |
1295 | EmitStopPoint(&S); |
1296 | |
1297 | EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock); |
1298 | } |
1299 | |
1300 | /// EmitCaseStmtRange - If case statement range is not too big then |
1301 | /// add multiple cases to switch instruction, one for each value within |
1302 | /// the range. If range is too big then emit "if" condition check. |
1303 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S, |
1304 | ArrayRef<const Attr *> Attrs) { |
1305 | assert(S.getRHS() && "Expected RHS value in CaseStmt" ); |
1306 | |
1307 | llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); |
1308 | llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); |
1309 | |
1310 | // Emit the code for this case. We do this first to make sure it is |
1311 | // properly chained from our predecessor before generating the |
1312 | // switch machinery to enter this block. |
1313 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb" ); |
1314 | EmitBlockWithFallThrough(CaseDest, &S); |
1315 | EmitStmt(S.getSubStmt()); |
1316 | |
1317 | // If range is empty, do nothing. |
1318 | if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) |
1319 | return; |
1320 | |
1321 | Stmt::Likelihood LH = Stmt::getLikelihood(Attrs); |
1322 | llvm::APInt Range = RHS - LHS; |
1323 | // FIXME: parameters such as this should not be hardcoded. |
1324 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
1325 | // Range is small enough to add multiple switch instruction cases. |
1326 | uint64_t Total = getProfileCount(&S); |
1327 | unsigned NCases = Range.getZExtValue() + 1; |
1328 | // We only have one region counter for the entire set of cases here, so we |
1329 | // need to divide the weights evenly between the generated cases, ensuring |
1330 | // that the total weight is preserved. E.g., a weight of 5 over three cases |
1331 | // will be distributed as weights of 2, 2, and 1. |
1332 | uint64_t Weight = Total / NCases, Rem = Total % NCases; |
1333 | for (unsigned I = 0; I != NCases; ++I) { |
1334 | if (SwitchWeights) |
1335 | SwitchWeights->push_back(Weight + (Rem ? 1 : 0)); |
1336 | else if (SwitchLikelihood) |
1337 | SwitchLikelihood->push_back(LH); |
1338 | |
1339 | if (Rem) |
1340 | Rem--; |
1341 | SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); |
1342 | ++LHS; |
1343 | } |
1344 | return; |
1345 | } |
1346 | |
1347 | // The range is too big. Emit "if" condition into a new block, |
1348 | // making sure to save and restore the current insertion point. |
1349 | llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); |
1350 | |
1351 | // Push this test onto the chain of range checks (which terminates |
1352 | // in the default basic block). The switch's default will be changed |
1353 | // to the top of this chain after switch emission is complete. |
1354 | llvm::BasicBlock *FalseDest = CaseRangeBlock; |
1355 | CaseRangeBlock = createBasicBlock("sw.caserange" ); |
1356 | |
1357 | CurFn->getBasicBlockList().push_back(CaseRangeBlock); |
1358 | Builder.SetInsertPoint(CaseRangeBlock); |
1359 | |
1360 | // Emit range check. |
1361 | llvm::Value *Diff = |
1362 | Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); |
1363 | llvm::Value *Cond = |
1364 | Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds" ); |
1365 | |
1366 | llvm::MDNode *Weights = nullptr; |
1367 | if (SwitchWeights) { |
1368 | uint64_t ThisCount = getProfileCount(&S); |
1369 | uint64_t DefaultCount = (*SwitchWeights)[0]; |
1370 | Weights = createProfileWeights(ThisCount, DefaultCount); |
1371 | |
1372 | // Since we're chaining the switch default through each large case range, we |
1373 | // need to update the weight for the default, ie, the first case, to include |
1374 | // this case. |
1375 | (*SwitchWeights)[0] += ThisCount; |
1376 | } else if (SwitchLikelihood) |
1377 | Cond = emitCondLikelihoodViaExpectIntrinsic(Cond, LH); |
1378 | |
1379 | Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights); |
1380 | |
1381 | // Restore the appropriate insertion point. |
1382 | if (RestoreBB) |
1383 | Builder.SetInsertPoint(RestoreBB); |
1384 | else |
1385 | Builder.ClearInsertionPoint(); |
1386 | } |
1387 | |
1388 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S, |
1389 | ArrayRef<const Attr *> Attrs) { |
1390 | // If there is no enclosing switch instance that we're aware of, then this |
1391 | // case statement and its block can be elided. This situation only happens |
1392 | // when we've constant-folded the switch, are emitting the constant case, |
1393 | // and part of the constant case includes another case statement. For |
1394 | // instance: switch (4) { case 4: do { case 5: } while (1); } |
1395 | if (!SwitchInsn) { |
1396 | EmitStmt(S.getSubStmt()); |
1397 | return; |
1398 | } |
1399 | |
1400 | // Handle case ranges. |
1401 | if (S.getRHS()) { |
1402 | EmitCaseStmtRange(S, Attrs); |
1403 | return; |
1404 | } |
1405 | |
1406 | llvm::ConstantInt *CaseVal = |
1407 | Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); |
1408 | if (SwitchLikelihood) |
1409 | SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs)); |
1410 | |
1411 | // If the body of the case is just a 'break', try to not emit an empty block. |
1412 | // If we're profiling or we're not optimizing, leave the block in for better |
1413 | // debug and coverage analysis. |
1414 | if (!CGM.getCodeGenOpts().hasProfileClangInstr() && |
1415 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
1416 | isa<BreakStmt>(S.getSubStmt())) { |
1417 | JumpDest Block = BreakContinueStack.back().BreakBlock; |
1418 | |
1419 | // Only do this optimization if there are no cleanups that need emitting. |
1420 | if (isObviouslyBranchWithoutCleanups(Block)) { |
1421 | if (SwitchWeights) |
1422 | SwitchWeights->push_back(getProfileCount(&S)); |
1423 | SwitchInsn->addCase(CaseVal, Block.getBlock()); |
1424 | |
1425 | // If there was a fallthrough into this case, make sure to redirect it to |
1426 | // the end of the switch as well. |
1427 | if (Builder.GetInsertBlock()) { |
1428 | Builder.CreateBr(Block.getBlock()); |
1429 | Builder.ClearInsertionPoint(); |
1430 | } |
1431 | return; |
1432 | } |
1433 | } |
1434 | |
1435 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb" ); |
1436 | EmitBlockWithFallThrough(CaseDest, &S); |
1437 | if (SwitchWeights) |
1438 | SwitchWeights->push_back(getProfileCount(&S)); |
1439 | SwitchInsn->addCase(CaseVal, CaseDest); |
1440 | |
1441 | // Recursively emitting the statement is acceptable, but is not wonderful for |
1442 | // code where we have many case statements nested together, i.e.: |
1443 | // case 1: |
1444 | // case 2: |
1445 | // case 3: etc. |
1446 | // Handling this recursively will create a new block for each case statement |
1447 | // that falls through to the next case which is IR intensive. It also causes |
1448 | // deep recursion which can run into stack depth limitations. Handle |
1449 | // sequential non-range case statements specially. |
1450 | // |
1451 | // TODO When the next case has a likelihood attribute the code returns to the |
1452 | // recursive algorithm. Maybe improve this case if it becomes common practice |
1453 | // to use a lot of attributes. |
1454 | const CaseStmt *CurCase = &S; |
1455 | const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); |
1456 | |
1457 | // Otherwise, iteratively add consecutive cases to this switch stmt. |
1458 | while (NextCase && NextCase->getRHS() == nullptr) { |
1459 | CurCase = NextCase; |
1460 | llvm::ConstantInt *CaseVal = |
1461 | Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); |
1462 | |
1463 | if (SwitchWeights) |
1464 | SwitchWeights->push_back(getProfileCount(NextCase)); |
1465 | if (CGM.getCodeGenOpts().hasProfileClangInstr()) { |
1466 | CaseDest = createBasicBlock("sw.bb" ); |
1467 | EmitBlockWithFallThrough(CaseDest, CurCase); |
1468 | } |
1469 | // Since this loop is only executed when the CaseStmt has no attributes |
1470 | // use a hard-coded value. |
1471 | if (SwitchLikelihood) |
1472 | SwitchLikelihood->push_back(Stmt::LH_None); |
1473 | |
1474 | SwitchInsn->addCase(CaseVal, CaseDest); |
1475 | NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); |
1476 | } |
1477 | |
1478 | // Normal default recursion for non-cases. |
1479 | EmitStmt(CurCase->getSubStmt()); |
1480 | } |
1481 | |
1482 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S, |
1483 | ArrayRef<const Attr *> Attrs) { |
1484 | // If there is no enclosing switch instance that we're aware of, then this |
1485 | // default statement can be elided. This situation only happens when we've |
1486 | // constant-folded the switch. |
1487 | if (!SwitchInsn) { |
1488 | EmitStmt(S.getSubStmt()); |
1489 | return; |
1490 | } |
1491 | |
1492 | llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); |
1493 | assert(DefaultBlock->empty() && |
1494 | "EmitDefaultStmt: Default block already defined?" ); |
1495 | |
1496 | if (SwitchLikelihood) |
1497 | SwitchLikelihood->front() = Stmt::getLikelihood(Attrs); |
1498 | |
1499 | EmitBlockWithFallThrough(DefaultBlock, &S); |
1500 | |
1501 | EmitStmt(S.getSubStmt()); |
1502 | } |
1503 | |
1504 | /// CollectStatementsForCase - Given the body of a 'switch' statement and a |
1505 | /// constant value that is being switched on, see if we can dead code eliminate |
1506 | /// the body of the switch to a simple series of statements to emit. Basically, |
1507 | /// on a switch (5) we want to find these statements: |
1508 | /// case 5: |
1509 | /// printf(...); <-- |
1510 | /// ++i; <-- |
1511 | /// break; |
1512 | /// |
1513 | /// and add them to the ResultStmts vector. If it is unsafe to do this |
1514 | /// transformation (for example, one of the elided statements contains a label |
1515 | /// that might be jumped to), return CSFC_Failure. If we handled it and 'S' |
1516 | /// should include statements after it (e.g. the printf() line is a substmt of |
1517 | /// the case) then return CSFC_FallThrough. If we handled it and found a break |
1518 | /// statement, then return CSFC_Success. |
1519 | /// |
1520 | /// If Case is non-null, then we are looking for the specified case, checking |
1521 | /// that nothing we jump over contains labels. If Case is null, then we found |
1522 | /// the case and are looking for the break. |
1523 | /// |
1524 | /// If the recursive walk actually finds our Case, then we set FoundCase to |
1525 | /// true. |
1526 | /// |
1527 | enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; |
1528 | static CSFC_Result CollectStatementsForCase(const Stmt *S, |
1529 | const SwitchCase *Case, |
1530 | bool &FoundCase, |
1531 | SmallVectorImpl<const Stmt*> &ResultStmts) { |
1532 | // If this is a null statement, just succeed. |
1533 | if (!S) |
1534 | return Case ? CSFC_Success : CSFC_FallThrough; |
1535 | |
1536 | // If this is the switchcase (case 4: or default) that we're looking for, then |
1537 | // we're in business. Just add the substatement. |
1538 | if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { |
1539 | if (S == Case) { |
1540 | FoundCase = true; |
1541 | return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase, |
1542 | ResultStmts); |
1543 | } |
1544 | |
1545 | // Otherwise, this is some other case or default statement, just ignore it. |
1546 | return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, |
1547 | ResultStmts); |
1548 | } |
1549 | |
1550 | // If we are in the live part of the code and we found our break statement, |
1551 | // return a success! |
1552 | if (!Case && isa<BreakStmt>(S)) |
1553 | return CSFC_Success; |
1554 | |
1555 | // If this is a switch statement, then it might contain the SwitchCase, the |
1556 | // break, or neither. |
1557 | if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { |
1558 | // Handle this as two cases: we might be looking for the SwitchCase (if so |
1559 | // the skipped statements must be skippable) or we might already have it. |
1560 | CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); |
1561 | bool StartedInLiveCode = FoundCase; |
1562 | unsigned StartSize = ResultStmts.size(); |
1563 | |
1564 | // If we've not found the case yet, scan through looking for it. |
1565 | if (Case) { |
1566 | // Keep track of whether we see a skipped declaration. The code could be |
1567 | // using the declaration even if it is skipped, so we can't optimize out |
1568 | // the decl if the kept statements might refer to it. |
1569 | bool HadSkippedDecl = false; |
1570 | |
1571 | // If we're looking for the case, just see if we can skip each of the |
1572 | // substatements. |
1573 | for (; Case && I != E; ++I) { |
1574 | HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I); |
1575 | |
1576 | switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { |
1577 | case CSFC_Failure: return CSFC_Failure; |
1578 | case CSFC_Success: |
1579 | // A successful result means that either 1) that the statement doesn't |
1580 | // have the case and is skippable, or 2) does contain the case value |
1581 | // and also contains the break to exit the switch. In the later case, |
1582 | // we just verify the rest of the statements are elidable. |
1583 | if (FoundCase) { |
1584 | // If we found the case and skipped declarations, we can't do the |
1585 | // optimization. |
1586 | if (HadSkippedDecl) |
1587 | return CSFC_Failure; |
1588 | |
1589 | for (++I; I != E; ++I) |
1590 | if (CodeGenFunction::ContainsLabel(*I, true)) |
1591 | return CSFC_Failure; |
1592 | return CSFC_Success; |
1593 | } |
1594 | break; |
1595 | case CSFC_FallThrough: |
1596 | // If we have a fallthrough condition, then we must have found the |
1597 | // case started to include statements. Consider the rest of the |
1598 | // statements in the compound statement as candidates for inclusion. |
1599 | assert(FoundCase && "Didn't find case but returned fallthrough?" ); |
1600 | // We recursively found Case, so we're not looking for it anymore. |
1601 | Case = nullptr; |
1602 | |
1603 | // If we found the case and skipped declarations, we can't do the |
1604 | // optimization. |
1605 | if (HadSkippedDecl) |
1606 | return CSFC_Failure; |
1607 | break; |
1608 | } |
1609 | } |
1610 | |
1611 | if (!FoundCase) |
1612 | return CSFC_Success; |
1613 | |
1614 | assert(!HadSkippedDecl && "fallthrough after skipping decl" ); |
1615 | } |
1616 | |
1617 | // If we have statements in our range, then we know that the statements are |
1618 | // live and need to be added to the set of statements we're tracking. |
1619 | bool AnyDecls = false; |
1620 | for (; I != E; ++I) { |
1621 | AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I); |
1622 | |
1623 | switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) { |
1624 | case CSFC_Failure: return CSFC_Failure; |
1625 | case CSFC_FallThrough: |
1626 | // A fallthrough result means that the statement was simple and just |
1627 | // included in ResultStmt, keep adding them afterwards. |
1628 | break; |
1629 | case CSFC_Success: |
1630 | // A successful result means that we found the break statement and |
1631 | // stopped statement inclusion. We just ensure that any leftover stmts |
1632 | // are skippable and return success ourselves. |
1633 | for (++I; I != E; ++I) |
1634 | if (CodeGenFunction::ContainsLabel(*I, true)) |
1635 | return CSFC_Failure; |
1636 | return CSFC_Success; |
1637 | } |
1638 | } |
1639 | |
1640 | // If we're about to fall out of a scope without hitting a 'break;', we |
1641 | // can't perform the optimization if there were any decls in that scope |
1642 | // (we'd lose their end-of-lifetime). |
1643 | if (AnyDecls) { |
1644 | // If the entire compound statement was live, there's one more thing we |
1645 | // can try before giving up: emit the whole thing as a single statement. |
1646 | // We can do that unless the statement contains a 'break;'. |
1647 | // FIXME: Such a break must be at the end of a construct within this one. |
1648 | // We could emit this by just ignoring the BreakStmts entirely. |
1649 | if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) { |
1650 | ResultStmts.resize(StartSize); |
1651 | ResultStmts.push_back(S); |
1652 | } else { |
1653 | return CSFC_Failure; |
1654 | } |
1655 | } |
1656 | |
1657 | return CSFC_FallThrough; |
1658 | } |
1659 | |
1660 | // Okay, this is some other statement that we don't handle explicitly, like a |
1661 | // for statement or increment etc. If we are skipping over this statement, |
1662 | // just verify it doesn't have labels, which would make it invalid to elide. |
1663 | if (Case) { |
1664 | if (CodeGenFunction::ContainsLabel(S, true)) |
1665 | return CSFC_Failure; |
1666 | return CSFC_Success; |
1667 | } |
1668 | |
1669 | // Otherwise, we want to include this statement. Everything is cool with that |
1670 | // so long as it doesn't contain a break out of the switch we're in. |
1671 | if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; |
1672 | |
1673 | // Otherwise, everything is great. Include the statement and tell the caller |
1674 | // that we fall through and include the next statement as well. |
1675 | ResultStmts.push_back(S); |
1676 | return CSFC_FallThrough; |
1677 | } |
1678 | |
1679 | /// FindCaseStatementsForValue - Find the case statement being jumped to and |
1680 | /// then invoke CollectStatementsForCase to find the list of statements to emit |
1681 | /// for a switch on constant. See the comment above CollectStatementsForCase |
1682 | /// for more details. |
1683 | static bool FindCaseStatementsForValue(const SwitchStmt &S, |
1684 | const llvm::APSInt &ConstantCondValue, |
1685 | SmallVectorImpl<const Stmt*> &ResultStmts, |
1686 | ASTContext &C, |
1687 | const SwitchCase *&ResultCase) { |
1688 | // First step, find the switch case that is being branched to. We can do this |
1689 | // efficiently by scanning the SwitchCase list. |
1690 | const SwitchCase *Case = S.getSwitchCaseList(); |
1691 | const DefaultStmt *DefaultCase = nullptr; |
1692 | |
1693 | for (; Case; Case = Case->getNextSwitchCase()) { |
1694 | // It's either a default or case. Just remember the default statement in |
1695 | // case we're not jumping to any numbered cases. |
1696 | if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { |
1697 | DefaultCase = DS; |
1698 | continue; |
1699 | } |
1700 | |
1701 | // Check to see if this case is the one we're looking for. |
1702 | const CaseStmt *CS = cast<CaseStmt>(Case); |
1703 | // Don't handle case ranges yet. |
1704 | if (CS->getRHS()) return false; |
1705 | |
1706 | // If we found our case, remember it as 'case'. |
1707 | if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) |
1708 | break; |
1709 | } |
1710 | |
1711 | // If we didn't find a matching case, we use a default if it exists, or we |
1712 | // elide the whole switch body! |
1713 | if (!Case) { |
1714 | // It is safe to elide the body of the switch if it doesn't contain labels |
1715 | // etc. If it is safe, return successfully with an empty ResultStmts list. |
1716 | if (!DefaultCase) |
1717 | return !CodeGenFunction::ContainsLabel(&S); |
1718 | Case = DefaultCase; |
1719 | } |
1720 | |
1721 | // Ok, we know which case is being jumped to, try to collect all the |
1722 | // statements that follow it. This can fail for a variety of reasons. Also, |
1723 | // check to see that the recursive walk actually found our case statement. |
1724 | // Insane cases like this can fail to find it in the recursive walk since we |
1725 | // don't handle every stmt kind: |
1726 | // switch (4) { |
1727 | // while (1) { |
1728 | // case 4: ... |
1729 | bool FoundCase = false; |
1730 | ResultCase = Case; |
1731 | return CollectStatementsForCase(S.getBody(), Case, FoundCase, |
1732 | ResultStmts) != CSFC_Failure && |
1733 | FoundCase; |
1734 | } |
1735 | |
1736 | static Optional<SmallVector<uint64_t, 16>> |
1737 | getLikelihoodWeights(ArrayRef<Stmt::Likelihood> Likelihoods) { |
1738 | // Are there enough branches to weight them? |
1739 | if (Likelihoods.size() <= 1) |
1740 | return None; |
1741 | |
1742 | uint64_t NumUnlikely = 0; |
1743 | uint64_t NumNone = 0; |
1744 | uint64_t NumLikely = 0; |
1745 | for (const auto LH : Likelihoods) { |
1746 | switch (LH) { |
1747 | case Stmt::LH_Unlikely: |
1748 | ++NumUnlikely; |
1749 | break; |
1750 | case Stmt::LH_None: |
1751 | ++NumNone; |
1752 | break; |
1753 | case Stmt::LH_Likely: |
1754 | ++NumLikely; |
1755 | break; |
1756 | } |
1757 | } |
1758 | |
1759 | // Is there a likelihood attribute used? |
1760 | if (NumUnlikely == 0 && NumLikely == 0) |
1761 | return None; |
1762 | |
1763 | // When multiple cases share the same code they can be combined during |
1764 | // optimization. In that case the weights of the branch will be the sum of |
1765 | // the individual weights. Make sure the combined sum of all neutral cases |
1766 | // doesn't exceed the value of a single likely attribute. |
1767 | // The additions both avoid divisions by 0 and make sure the weights of None |
1768 | // don't exceed the weight of Likely. |
1769 | const uint64_t Likely = INT32_MAX / (NumLikely + 2); |
1770 | const uint64_t None = Likely / (NumNone + 1); |
1771 | const uint64_t Unlikely = 0; |
1772 | |
1773 | SmallVector<uint64_t, 16> Result; |
1774 | Result.reserve(Likelihoods.size()); |
1775 | for (const auto LH : Likelihoods) { |
1776 | switch (LH) { |
1777 | case Stmt::LH_Unlikely: |
1778 | Result.push_back(Unlikely); |
1779 | break; |
1780 | case Stmt::LH_None: |
1781 | Result.push_back(None); |
1782 | break; |
1783 | case Stmt::LH_Likely: |
1784 | Result.push_back(Likely); |
1785 | break; |
1786 | } |
1787 | } |
1788 | |
1789 | return Result; |
1790 | } |
1791 | |
1792 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
1793 | // Handle nested switch statements. |
1794 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
1795 | SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights; |
1796 | SmallVector<Stmt::Likelihood, 16> *SavedSwitchLikelihood = SwitchLikelihood; |
1797 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
1798 | |
1799 | // See if we can constant fold the condition of the switch and therefore only |
1800 | // emit the live case statement (if any) of the switch. |
1801 | llvm::APSInt ConstantCondValue; |
1802 | if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { |
1803 | SmallVector<const Stmt*, 4> CaseStmts; |
1804 | const SwitchCase *Case = nullptr; |
1805 | if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, |
1806 | getContext(), Case)) { |
1807 | if (Case) |
1808 | incrementProfileCounter(Case); |
1809 | RunCleanupsScope ExecutedScope(*this); |
1810 | |
1811 | if (S.getInit()) |
1812 | EmitStmt(S.getInit()); |
1813 | |
1814 | // Emit the condition variable if needed inside the entire cleanup scope |
1815 | // used by this special case for constant folded switches. |
1816 | if (S.getConditionVariable()) |
1817 | EmitDecl(*S.getConditionVariable()); |
1818 | |
1819 | // At this point, we are no longer "within" a switch instance, so |
1820 | // we can temporarily enforce this to ensure that any embedded case |
1821 | // statements are not emitted. |
1822 | SwitchInsn = nullptr; |
1823 | |
1824 | // Okay, we can dead code eliminate everything except this case. Emit the |
1825 | // specified series of statements and we're good. |
1826 | for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) |
1827 | EmitStmt(CaseStmts[i]); |
1828 | incrementProfileCounter(&S); |
1829 | |
1830 | // Now we want to restore the saved switch instance so that nested |
1831 | // switches continue to function properly |
1832 | SwitchInsn = SavedSwitchInsn; |
1833 | |
1834 | return; |
1835 | } |
1836 | } |
1837 | |
1838 | JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog" ); |
1839 | |
1840 | RunCleanupsScope ConditionScope(*this); |
1841 | |
1842 | if (S.getInit()) |
1843 | EmitStmt(S.getInit()); |
1844 | |
1845 | if (S.getConditionVariable()) |
1846 | EmitDecl(*S.getConditionVariable()); |
1847 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
1848 | |
1849 | // Create basic block to hold stuff that comes after switch |
1850 | // statement. We also need to create a default block now so that |
1851 | // explicit case ranges tests can have a place to jump to on |
1852 | // failure. |
1853 | llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default" ); |
1854 | SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); |
1855 | if (PGO.haveRegionCounts()) { |
1856 | // Walk the SwitchCase list to find how many there are. |
1857 | uint64_t DefaultCount = 0; |
1858 | unsigned NumCases = 0; |
1859 | for (const SwitchCase *Case = S.getSwitchCaseList(); |
1860 | Case; |
1861 | Case = Case->getNextSwitchCase()) { |
1862 | if (isa<DefaultStmt>(Case)) |
1863 | DefaultCount = getProfileCount(Case); |
1864 | NumCases += 1; |
1865 | } |
1866 | SwitchWeights = new SmallVector<uint64_t, 16>(); |
1867 | SwitchWeights->reserve(NumCases); |
1868 | // The default needs to be first. We store the edge count, so we already |
1869 | // know the right weight. |
1870 | SwitchWeights->push_back(DefaultCount); |
1871 | } else if (CGM.getCodeGenOpts().OptimizationLevel) { |
1872 | SwitchLikelihood = new SmallVector<Stmt::Likelihood, 16>(); |
1873 | // Initialize the default case. |
1874 | SwitchLikelihood->push_back(Stmt::LH_None); |
1875 | } |
1876 | |
1877 | CaseRangeBlock = DefaultBlock; |
1878 | |
1879 | // Clear the insertion point to indicate we are in unreachable code. |
1880 | Builder.ClearInsertionPoint(); |
1881 | |
1882 | // All break statements jump to NextBlock. If BreakContinueStack is non-empty |
1883 | // then reuse last ContinueBlock. |
1884 | JumpDest OuterContinue; |
1885 | if (!BreakContinueStack.empty()) |
1886 | OuterContinue = BreakContinueStack.back().ContinueBlock; |
1887 | |
1888 | BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); |
1889 | |
1890 | // Emit switch body. |
1891 | EmitStmt(S.getBody()); |
1892 | |
1893 | BreakContinueStack.pop_back(); |
1894 | |
1895 | // Update the default block in case explicit case range tests have |
1896 | // been chained on top. |
1897 | SwitchInsn->setDefaultDest(CaseRangeBlock); |
1898 | |
1899 | // If a default was never emitted: |
1900 | if (!DefaultBlock->getParent()) { |
1901 | // If we have cleanups, emit the default block so that there's a |
1902 | // place to jump through the cleanups from. |
1903 | if (ConditionScope.requiresCleanups()) { |
1904 | EmitBlock(DefaultBlock); |
1905 | |
1906 | // Otherwise, just forward the default block to the switch end. |
1907 | } else { |
1908 | DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); |
1909 | delete DefaultBlock; |
1910 | } |
1911 | } |
1912 | |
1913 | ConditionScope.ForceCleanup(); |
1914 | |
1915 | // Emit continuation. |
1916 | EmitBlock(SwitchExit.getBlock(), true); |
1917 | incrementProfileCounter(&S); |
1918 | |
1919 | // If the switch has a condition wrapped by __builtin_unpredictable, |
1920 | // create metadata that specifies that the switch is unpredictable. |
1921 | // Don't bother if not optimizing because that metadata would not be used. |
1922 | auto *Call = dyn_cast<CallExpr>(S.getCond()); |
1923 | if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { |
1924 | auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl()); |
1925 | if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { |
1926 | llvm::MDBuilder MDHelper(getLLVMContext()); |
1927 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable, |
1928 | MDHelper.createUnpredictable()); |
1929 | } |
1930 | } |
1931 | |
1932 | if (SwitchWeights) { |
1933 | assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && |
1934 | "switch weights do not match switch cases" ); |
1935 | // If there's only one jump destination there's no sense weighting it. |
1936 | if (SwitchWeights->size() > 1) |
1937 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, |
1938 | createProfileWeights(*SwitchWeights)); |
1939 | delete SwitchWeights; |
1940 | } else if (SwitchLikelihood) { |
1941 | assert(SwitchLikelihood->size() == 1 + SwitchInsn->getNumCases() && |
1942 | "switch likelihoods do not match switch cases" ); |
1943 | Optional<SmallVector<uint64_t, 16>> LHW = |
1944 | getLikelihoodWeights(*SwitchLikelihood); |
1945 | if (LHW) { |
1946 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
1947 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, |
1948 | createProfileWeights(*LHW)); |
1949 | } |
1950 | delete SwitchLikelihood; |
1951 | } |
1952 | SwitchInsn = SavedSwitchInsn; |
1953 | SwitchWeights = SavedSwitchWeights; |
1954 | SwitchLikelihood = SavedSwitchLikelihood; |
1955 | CaseRangeBlock = SavedCRBlock; |
1956 | } |
1957 | |
1958 | static std::string |
1959 | SimplifyConstraint(const char *Constraint, const TargetInfo &Target, |
1960 | SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) { |
1961 | std::string Result; |
1962 | |
1963 | while (*Constraint) { |
1964 | switch (*Constraint) { |
1965 | default: |
1966 | Result += Target.convertConstraint(Constraint); |
1967 | break; |
1968 | // Ignore these |
1969 | case '*': |
1970 | case '?': |
1971 | case '!': |
1972 | case '=': // Will see this and the following in mult-alt constraints. |
1973 | case '+': |
1974 | break; |
1975 | case '#': // Ignore the rest of the constraint alternative. |
1976 | while (Constraint[1] && Constraint[1] != ',') |
1977 | Constraint++; |
1978 | break; |
1979 | case '&': |
1980 | case '%': |
1981 | Result += *Constraint; |
1982 | while (Constraint[1] && Constraint[1] == *Constraint) |
1983 | Constraint++; |
1984 | break; |
1985 | case ',': |
1986 | Result += "|" ; |
1987 | break; |
1988 | case 'g': |
1989 | Result += "imr" ; |
1990 | break; |
1991 | case '[': { |
1992 | assert(OutCons && |
1993 | "Must pass output names to constraints with a symbolic name" ); |
1994 | unsigned Index; |
1995 | bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index); |
1996 | assert(result && "Could not resolve symbolic name" ); (void)result; |
1997 | Result += llvm::utostr(Index); |
1998 | break; |
1999 | } |
2000 | } |
2001 | |
2002 | Constraint++; |
2003 | } |
2004 | |
2005 | return Result; |
2006 | } |
2007 | |
2008 | /// AddVariableConstraints - Look at AsmExpr and if it is a variable declared |
2009 | /// as using a particular register add that as a constraint that will be used |
2010 | /// in this asm stmt. |
2011 | static std::string |
2012 | AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, |
2013 | const TargetInfo &Target, CodeGenModule &CGM, |
2014 | const AsmStmt &Stmt, const bool EarlyClobber, |
2015 | std::string *GCCReg = nullptr) { |
2016 | const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); |
2017 | if (!AsmDeclRef) |
2018 | return Constraint; |
2019 | const ValueDecl &Value = *AsmDeclRef->getDecl(); |
2020 | const VarDecl *Variable = dyn_cast<VarDecl>(&Value); |
2021 | if (!Variable) |
2022 | return Constraint; |
2023 | if (Variable->getStorageClass() != SC_Register) |
2024 | return Constraint; |
2025 | AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); |
2026 | if (!Attr) |
2027 | return Constraint; |
2028 | StringRef Register = Attr->getLabel(); |
2029 | assert(Target.isValidGCCRegisterName(Register)); |
2030 | // We're using validateOutputConstraint here because we only care if |
2031 | // this is a register constraint. |
2032 | TargetInfo::ConstraintInfo Info(Constraint, "" ); |
2033 | if (Target.validateOutputConstraint(Info) && |
2034 | !Info.allowsRegister()) { |
2035 | CGM.ErrorUnsupported(&Stmt, "__asm__" ); |
2036 | return Constraint; |
2037 | } |
2038 | // Canonicalize the register here before returning it. |
2039 | Register = Target.getNormalizedGCCRegisterName(Register); |
2040 | if (GCCReg != nullptr) |
2041 | *GCCReg = Register.str(); |
2042 | return (EarlyClobber ? "&{" : "{" ) + Register.str() + "}" ; |
2043 | } |
2044 | |
2045 | llvm::Value* |
2046 | CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, |
2047 | LValue InputValue, QualType InputType, |
2048 | std::string &ConstraintStr, |
2049 | SourceLocation Loc) { |
2050 | llvm::Value *Arg; |
2051 | if (Info.allowsRegister() || !Info.allowsMemory()) { |
2052 | if (CodeGenFunction::hasScalarEvaluationKind(InputType)) { |
2053 | Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal(); |
2054 | } else { |
2055 | llvm::Type *Ty = ConvertType(InputType); |
2056 | uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); |
2057 | if (Size <= 64 && llvm::isPowerOf2_64(Size)) { |
2058 | Ty = llvm::IntegerType::get(getLLVMContext(), Size); |
2059 | Ty = llvm::PointerType::getUnqual(Ty); |
2060 | |
2061 | Arg = Builder.CreateLoad( |
2062 | Builder.CreateBitCast(InputValue.getAddress(*this), Ty)); |
2063 | } else { |
2064 | Arg = InputValue.getPointer(*this); |
2065 | ConstraintStr += '*'; |
2066 | } |
2067 | } |
2068 | } else { |
2069 | Arg = InputValue.getPointer(*this); |
2070 | ConstraintStr += '*'; |
2071 | } |
2072 | |
2073 | return Arg; |
2074 | } |
2075 | |
2076 | llvm::Value* CodeGenFunction::EmitAsmInput( |
2077 | const TargetInfo::ConstraintInfo &Info, |
2078 | const Expr *InputExpr, |
2079 | std::string &ConstraintStr) { |
2080 | // If this can't be a register or memory, i.e., has to be a constant |
2081 | // (immediate or symbolic), try to emit it as such. |
2082 | if (!Info.allowsRegister() && !Info.allowsMemory()) { |
2083 | if (Info.requiresImmediateConstant()) { |
2084 | Expr::EvalResult EVResult; |
2085 | InputExpr->EvaluateAsRValue(EVResult, getContext(), true); |
2086 | |
2087 | llvm::APSInt IntResult; |
2088 | if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(), |
2089 | getContext())) |
2090 | return llvm::ConstantInt::get(getLLVMContext(), IntResult); |
2091 | } |
2092 | |
2093 | Expr::EvalResult Result; |
2094 | if (InputExpr->EvaluateAsInt(Result, getContext())) |
2095 | return llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt()); |
2096 | } |
2097 | |
2098 | if (Info.allowsRegister() || !Info.allowsMemory()) |
2099 | if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) |
2100 | return EmitScalarExpr(InputExpr); |
2101 | if (InputExpr->getStmtClass() == Expr::CXXThisExprClass) |
2102 | return EmitScalarExpr(InputExpr); |
2103 | InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); |
2104 | LValue Dest = EmitLValue(InputExpr); |
2105 | return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr, |
2106 | InputExpr->getExprLoc()); |
2107 | } |
2108 | |
2109 | /// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline |
2110 | /// asm call instruction. The !srcloc MDNode contains a list of constant |
2111 | /// integers which are the source locations of the start of each line in the |
2112 | /// asm. |
2113 | static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, |
2114 | CodeGenFunction &CGF) { |
2115 | SmallVector<llvm::Metadata *, 8> Locs; |
2116 | // Add the location of the first line to the MDNode. |
2117 | Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
2118 | CGF.Int32Ty, Str->getBeginLoc().getRawEncoding()))); |
2119 | StringRef StrVal = Str->getString(); |
2120 | if (!StrVal.empty()) { |
2121 | const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); |
2122 | const LangOptions &LangOpts = CGF.CGM.getLangOpts(); |
2123 | unsigned StartToken = 0; |
2124 | unsigned ByteOffset = 0; |
2125 | |
2126 | // Add the location of the start of each subsequent line of the asm to the |
2127 | // MDNode. |
2128 | for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) { |
2129 | if (StrVal[i] != '\n') continue; |
2130 | SourceLocation LineLoc = Str->getLocationOfByte( |
2131 | i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset); |
2132 | Locs.push_back(llvm::ConstantAsMetadata::get( |
2133 | llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding()))); |
2134 | } |
2135 | } |
2136 | |
2137 | return llvm::MDNode::get(CGF.getLLVMContext(), Locs); |
2138 | } |
2139 | |
2140 | static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect, |
2141 | bool ReadOnly, bool ReadNone, bool NoMerge, |
2142 | const AsmStmt &S, |
2143 | const std::vector<llvm::Type *> &ResultRegTypes, |
2144 | CodeGenFunction &CGF, |
2145 | std::vector<llvm::Value *> &RegResults) { |
2146 | Result.addAttribute(llvm::AttributeList::FunctionIndex, |
2147 | llvm::Attribute::NoUnwind); |
2148 | if (NoMerge) |
2149 | Result.addAttribute(llvm::AttributeList::FunctionIndex, |
2150 | llvm::Attribute::NoMerge); |
2151 | // Attach readnone and readonly attributes. |
2152 | if (!HasSideEffect) { |
2153 | if (ReadNone) |
2154 | Result.addAttribute(llvm::AttributeList::FunctionIndex, |
2155 | llvm::Attribute::ReadNone); |
2156 | else if (ReadOnly) |
2157 | Result.addAttribute(llvm::AttributeList::FunctionIndex, |
2158 | llvm::Attribute::ReadOnly); |
2159 | } |
2160 | |
2161 | // Slap the source location of the inline asm into a !srcloc metadata on the |
2162 | // call. |
2163 | if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) |
2164 | Result.setMetadata("srcloc" , |
2165 | getAsmSrcLocInfo(gccAsmStmt->getAsmString(), CGF)); |
2166 | else { |
2167 | // At least put the line number on MS inline asm blobs. |
2168 | llvm::Constant *Loc = llvm::ConstantInt::get(CGF.Int32Ty, |
2169 | S.getAsmLoc().getRawEncoding()); |
2170 | Result.setMetadata("srcloc" , |
2171 | llvm::MDNode::get(CGF.getLLVMContext(), |
2172 | llvm::ConstantAsMetadata::get(Loc))); |
2173 | } |
2174 | |
2175 | if (CGF.getLangOpts().assumeFunctionsAreConvergent()) |
2176 | // Conservatively, mark all inline asm blocks in CUDA or OpenCL as |
2177 | // convergent (meaning, they may call an intrinsically convergent op, such |
2178 | // as bar.sync, and so can't have certain optimizations applied around |
2179 | // them). |
2180 | Result.addAttribute(llvm::AttributeList::FunctionIndex, |
2181 | llvm::Attribute::Convergent); |
2182 | // Extract all of the register value results from the asm. |
2183 | if (ResultRegTypes.size() == 1) { |
2184 | RegResults.push_back(&Result); |
2185 | } else { |
2186 | for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { |
2187 | llvm::Value *Tmp = CGF.Builder.CreateExtractValue(&Result, i, "asmresult" ); |
2188 | RegResults.push_back(Tmp); |
2189 | } |
2190 | } |
2191 | } |
2192 | |
2193 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
2194 | // Assemble the final asm string. |
2195 | std::string AsmString = S.generateAsmString(getContext()); |
2196 | |
2197 | // Get all the output and input constraints together. |
2198 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
2199 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
2200 | |
2201 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
2202 | StringRef Name; |
2203 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
2204 | Name = GAS->getOutputName(i); |
2205 | TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name); |
2206 | bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; |
2207 | assert(IsValid && "Failed to parse output constraint" ); |
2208 | OutputConstraintInfos.push_back(Info); |
2209 | } |
2210 | |
2211 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
2212 | StringRef Name; |
2213 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
2214 | Name = GAS->getInputName(i); |
2215 | TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name); |
2216 | bool IsValid = |
2217 | getTarget().validateInputConstraint(OutputConstraintInfos, Info); |
2218 | assert(IsValid && "Failed to parse input constraint" ); (void)IsValid; |
2219 | InputConstraintInfos.push_back(Info); |
2220 | } |
2221 | |
2222 | std::string Constraints; |
2223 | |
2224 | std::vector<LValue> ResultRegDests; |
2225 | std::vector<QualType> ResultRegQualTys; |
2226 | std::vector<llvm::Type *> ResultRegTypes; |
2227 | std::vector<llvm::Type *> ResultTruncRegTypes; |
2228 | std::vector<llvm::Type *> ArgTypes; |
2229 | std::vector<llvm::Value*> Args; |
2230 | llvm::BitVector ResultTypeRequiresCast; |
2231 | |
2232 | // Keep track of inout constraints. |
2233 | std::string InOutConstraints; |
2234 | std::vector<llvm::Value*> InOutArgs; |
2235 | std::vector<llvm::Type*> InOutArgTypes; |
2236 | |
2237 | // Keep track of out constraints for tied input operand. |
2238 | std::vector<std::string> OutputConstraints; |
2239 | |
2240 | // Keep track of defined physregs. |
2241 | llvm::SmallSet<std::string, 8> PhysRegOutputs; |
2242 | |
2243 | // An inline asm can be marked readonly if it meets the following conditions: |
2244 | // - it doesn't have any sideeffects |
2245 | // - it doesn't clobber memory |
2246 | // - it doesn't return a value by-reference |
2247 | // It can be marked readnone if it doesn't have any input memory constraints |
2248 | // in addition to meeting the conditions listed above. |
2249 | bool ReadOnly = true, ReadNone = true; |
2250 | |
2251 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
2252 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
2253 | |
2254 | // Simplify the output constraint. |
2255 | std::string OutputConstraint(S.getOutputConstraint(i)); |
2256 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, |
2257 | getTarget(), &OutputConstraintInfos); |
2258 | |
2259 | const Expr *OutExpr = S.getOutputExpr(i); |
2260 | OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); |
2261 | |
2262 | std::string GCCReg; |
2263 | OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, |
2264 | getTarget(), CGM, S, |
2265 | Info.earlyClobber(), |
2266 | &GCCReg); |
2267 | // Give an error on multiple outputs to same physreg. |
2268 | if (!GCCReg.empty() && !PhysRegOutputs.insert(GCCReg).second) |
2269 | CGM.Error(S.getAsmLoc(), "multiple outputs to hard register: " + GCCReg); |
2270 | |
2271 | OutputConstraints.push_back(OutputConstraint); |
2272 | LValue Dest = EmitLValue(OutExpr); |
2273 | if (!Constraints.empty()) |
2274 | Constraints += ','; |
2275 | |
2276 | // If this is a register output, then make the inline asm return it |
2277 | // by-value. If this is a memory result, return the value by-reference. |
2278 | bool isScalarizableAggregate = |
2279 | hasAggregateEvaluationKind(OutExpr->getType()); |
2280 | if (!Info.allowsMemory() && (hasScalarEvaluationKind(OutExpr->getType()) || |
2281 | isScalarizableAggregate)) { |
2282 | Constraints += "=" + OutputConstraint; |
2283 | ResultRegQualTys.push_back(OutExpr->getType()); |
2284 | ResultRegDests.push_back(Dest); |
2285 | ResultTruncRegTypes.push_back(ConvertTypeForMem(OutExpr->getType())); |
2286 | if (Info.allowsRegister() && isScalarizableAggregate) { |
2287 | ResultTypeRequiresCast.push_back(true); |
2288 | unsigned Size = getContext().getTypeSize(OutExpr->getType()); |
2289 | llvm::Type *ConvTy = llvm::IntegerType::get(getLLVMContext(), Size); |
2290 | ResultRegTypes.push_back(ConvTy); |
2291 | } else { |
2292 | ResultTypeRequiresCast.push_back(false); |
2293 | ResultRegTypes.push_back(ResultTruncRegTypes.back()); |
2294 | } |
2295 | // If this output is tied to an input, and if the input is larger, then |
2296 | // we need to set the actual result type of the inline asm node to be the |
2297 | // same as the input type. |
2298 | if (Info.hasMatchingInput()) { |
2299 | unsigned InputNo; |
2300 | for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { |
2301 | TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; |
2302 | if (Input.hasTiedOperand() && Input.getTiedOperand() == i) |
2303 | break; |
2304 | } |
2305 | assert(InputNo != S.getNumInputs() && "Didn't find matching input!" ); |
2306 | |
2307 | QualType InputTy = S.getInputExpr(InputNo)->getType(); |
2308 | QualType OutputType = OutExpr->getType(); |
2309 | |
2310 | uint64_t InputSize = getContext().getTypeSize(InputTy); |
2311 | if (getContext().getTypeSize(OutputType) < InputSize) { |
2312 | // Form the asm to return the value as a larger integer or fp type. |
2313 | ResultRegTypes.back() = ConvertType(InputTy); |
2314 | } |
2315 | } |
2316 | if (llvm::Type* AdjTy = |
2317 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
2318 | ResultRegTypes.back())) |
2319 | ResultRegTypes.back() = AdjTy; |
2320 | else { |
2321 | CGM.getDiags().Report(S.getAsmLoc(), |
2322 | diag::err_asm_invalid_type_in_input) |
2323 | << OutExpr->getType() << OutputConstraint; |
2324 | } |
2325 | |
2326 | // Update largest vector width for any vector types. |
2327 | if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back())) |
2328 | LargestVectorWidth = |
2329 | std::max((uint64_t)LargestVectorWidth, |
2330 | VT->getPrimitiveSizeInBits().getKnownMinSize()); |
2331 | } else { |
2332 | llvm::Type *DestAddrTy = Dest.getAddress(*this).getType(); |
2333 | llvm::Value *DestPtr = Dest.getPointer(*this); |
2334 | // Matrix types in memory are represented by arrays, but accessed through |
2335 | // vector pointers, with the alignment specified on the access operation. |
2336 | // For inline assembly, update pointer arguments to use vector pointers. |
2337 | // Otherwise there will be a mis-match if the matrix is also an |
2338 | // input-argument which is represented as vector. |
2339 | if (isa<MatrixType>(OutExpr->getType().getCanonicalType())) { |
2340 | DestAddrTy = llvm::PointerType::get( |
2341 | ConvertType(OutExpr->getType()), |
2342 | cast<llvm::PointerType>(DestAddrTy)->getAddressSpace()); |
2343 | DestPtr = Builder.CreateBitCast(DestPtr, DestAddrTy); |
2344 | } |
2345 | ArgTypes.push_back(DestAddrTy); |
2346 | Args.push_back(DestPtr); |
2347 | Constraints += "=*" ; |
2348 | Constraints += OutputConstraint; |
2349 | ReadOnly = ReadNone = false; |
2350 | } |
2351 | |
2352 | if (Info.isReadWrite()) { |
2353 | InOutConstraints += ','; |
2354 | |
2355 | const Expr *InputExpr = S.getOutputExpr(i); |
2356 | llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(), |
2357 | InOutConstraints, |
2358 | InputExpr->getExprLoc()); |
2359 | |
2360 | if (llvm::Type* AdjTy = |
2361 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
2362 | Arg->getType())) |
2363 | Arg = Builder.CreateBitCast(Arg, AdjTy); |
2364 | |
2365 | // Update largest vector width for any vector types. |
2366 | if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType())) |
2367 | LargestVectorWidth = |
2368 | std::max((uint64_t)LargestVectorWidth, |
2369 | VT->getPrimitiveSizeInBits().getKnownMinSize()); |
2370 | // Only tie earlyclobber physregs. |
2371 | if (Info.allowsRegister() && (GCCReg.empty() || Info.earlyClobber())) |
2372 | InOutConstraints += llvm::utostr(i); |
2373 | else |
2374 | InOutConstraints += OutputConstraint; |
2375 | |
2376 | InOutArgTypes.push_back(Arg->getType()); |
2377 | InOutArgs.push_back(Arg); |
2378 | } |
2379 | } |
2380 | |
2381 | // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX) |
2382 | // to the return value slot. Only do this when returning in registers. |
2383 | if (isa<MSAsmStmt>(&S)) { |
2384 | const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); |
2385 | if (RetAI.isDirect() || RetAI.isExtend()) { |
2386 | // Make a fake lvalue for the return value slot. |
2387 | LValue ReturnSlot = |
---|