| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "CodeGenFunction.h" |
| 14 | #include "CGDebugInfo.h" |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "TargetInfo.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
| 18 | #include "clang/Basic/Builtins.h" |
| 19 | #include "clang/Basic/PrettyStackTrace.h" |
| 20 | #include "clang/Basic/TargetInfo.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/InlineAsm.h" |
| 24 | #include "llvm/IR/Intrinsics.h" |
| 25 | #include "llvm/IR/MDBuilder.h" |
| 26 | |
| 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | void CodeGenFunction::EmitStopPoint(const Stmt *S) { |
| 35 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 36 | SourceLocation Loc; |
| 37 | Loc = S->getBeginLoc(); |
| 38 | DI->EmitLocation(Builder, Loc); |
| 39 | |
| 40 | LastStopPoint = Loc; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) { |
| 45 | (0) . __assert_fail ("S && \"Null statement?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 45, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S && "Null statement?"); |
| 46 | PGO.setCurrentStmt(S); |
| 47 | |
| 48 | |
| 49 | if (EmitSimpleStmt(S)) |
| 50 | return; |
| 51 | |
| 52 | |
| 53 | if (!HaveInsertPoint()) { |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | if (!ContainsLabel(S)) { |
| 60 | |
| 61 | |
| 62 | (0) . __assert_fail ("!isa(*S) && \"Unexpected DeclStmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 62, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!"); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | EnsureInsertPoint(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | EmitStopPoint(S); |
| 72 | |
| 73 | |
| 74 | |
| 75 | if (getLangOpts().OpenMP && getLangOpts().OpenMPSimd) { |
| 76 | if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) { |
| 77 | EmitSimpleOMPExecutableDirective(*D); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | switch (S->getStmtClass()) { |
| 83 | case Stmt::NoStmtClass: |
| 84 | case Stmt::CXXCatchStmtClass: |
| 85 | case Stmt::SEHExceptStmtClass: |
| 86 | case Stmt::SEHFinallyStmtClass: |
| 87 | case Stmt::MSDependentExistsStmtClass: |
| 88 | llvm_unreachable("invalid statement class to emit generically"); |
| 89 | case Stmt::NullStmtClass: |
| 90 | case Stmt::CompoundStmtClass: |
| 91 | case Stmt::DeclStmtClass: |
| 92 | case Stmt::LabelStmtClass: |
| 93 | case Stmt::AttributedStmtClass: |
| 94 | case Stmt::GotoStmtClass: |
| 95 | case Stmt::BreakStmtClass: |
| 96 | case Stmt::ContinueStmtClass: |
| 97 | case Stmt::DefaultStmtClass: |
| 98 | case Stmt::CaseStmtClass: |
| 99 | case Stmt::SEHLeaveStmtClass: |
| 100 | llvm_unreachable("should have emitted these statements as simple"); |
| 101 | |
| 102 | #define STMT(Type, Base) |
| 103 | #define ABSTRACT_STMT(Op) |
| 104 | #define EXPR(Type, Base) \ |
| 105 | case Stmt::Type##Class: |
| 106 | #include "clang/AST/StmtNodes.inc" |
| 107 | { |
| 108 | |
| 109 | llvm::BasicBlock *incoming = Builder.GetInsertBlock(); |
| 110 | (0) . __assert_fail ("incoming && \"expression emission must have an insertion point\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 110, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(incoming && "expression emission must have an insertion point"); |
| 111 | |
| 112 | EmitIgnoredExpr(cast<Expr>(S)); |
| 113 | |
| 114 | llvm::BasicBlock *outgoing = Builder.GetInsertBlock(); |
| 115 | (0) . __assert_fail ("outgoing && \"expression emission cleared block!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 115, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(outgoing && "expression emission cleared block!"); |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | if (incoming != outgoing && outgoing->use_empty()) { |
| 130 | outgoing->eraseFromParent(); |
| 131 | Builder.ClearInsertionPoint(); |
| 132 | } |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | case Stmt::IndirectGotoStmtClass: |
| 137 | EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break; |
| 138 | |
| 139 | case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break; |
| 140 | case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break; |
| 141 | case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break; |
| 142 | case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break; |
| 143 | |
| 144 | case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break; |
| 145 | |
| 146 | case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break; |
| 147 | case Stmt::GCCAsmStmtClass: |
| 148 | case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break; |
| 149 | case Stmt::CoroutineBodyStmtClass: |
| 150 | EmitCoroutineBody(cast<CoroutineBodyStmt>(*S)); |
| 151 | break; |
| 152 | case Stmt::CoreturnStmtClass: |
| 153 | EmitCoreturnStmt(cast<CoreturnStmt>(*S)); |
| 154 | break; |
| 155 | case Stmt::CapturedStmtClass: { |
| 156 | const CapturedStmt *CS = cast<CapturedStmt>(S); |
| 157 | EmitCapturedStmt(*CS, CS->getCapturedRegionKind()); |
| 158 | } |
| 159 | break; |
| 160 | case Stmt::ObjCAtTryStmtClass: |
| 161 | EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S)); |
| 162 | break; |
| 163 | case Stmt::ObjCAtCatchStmtClass: |
| 164 | llvm_unreachable( |
| 165 | "@catch statements should be handled by EmitObjCAtTryStmt"); |
| 166 | case Stmt::ObjCAtFinallyStmtClass: |
| 167 | llvm_unreachable( |
| 168 | "@finally statements should be handled by EmitObjCAtTryStmt"); |
| 169 | case Stmt::ObjCAtThrowStmtClass: |
| 170 | EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S)); |
| 171 | break; |
| 172 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 173 | EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S)); |
| 174 | break; |
| 175 | case Stmt::ObjCForCollectionStmtClass: |
| 176 | EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S)); |
| 177 | break; |
| 178 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 179 | EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S)); |
| 180 | break; |
| 181 | |
| 182 | case Stmt::CXXTryStmtClass: |
| 183 | EmitCXXTryStmt(cast<CXXTryStmt>(*S)); |
| 184 | break; |
| 185 | case Stmt::CXXForRangeStmtClass: |
| 186 | EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs); |
| 187 | break; |
| 188 | case Stmt::SEHTryStmtClass: |
| 189 | EmitSEHTryStmt(cast<SEHTryStmt>(*S)); |
| 190 | break; |
| 191 | case Stmt::OMPParallelDirectiveClass: |
| 192 | EmitOMPParallelDirective(cast<OMPParallelDirective>(*S)); |
| 193 | break; |
| 194 | case Stmt::OMPSimdDirectiveClass: |
| 195 | EmitOMPSimdDirective(cast<OMPSimdDirective>(*S)); |
| 196 | break; |
| 197 | case Stmt::OMPForDirectiveClass: |
| 198 | EmitOMPForDirective(cast<OMPForDirective>(*S)); |
| 199 | break; |
| 200 | case Stmt::OMPForSimdDirectiveClass: |
| 201 | EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S)); |
| 202 | break; |
| 203 | case Stmt::OMPSectionsDirectiveClass: |
| 204 | EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S)); |
| 205 | break; |
| 206 | case Stmt::OMPSectionDirectiveClass: |
| 207 | EmitOMPSectionDirective(cast<OMPSectionDirective>(*S)); |
| 208 | break; |
| 209 | case Stmt::OMPSingleDirectiveClass: |
| 210 | EmitOMPSingleDirective(cast<OMPSingleDirective>(*S)); |
| 211 | break; |
| 212 | case Stmt::OMPMasterDirectiveClass: |
| 213 | EmitOMPMasterDirective(cast<OMPMasterDirective>(*S)); |
| 214 | break; |
| 215 | case Stmt::OMPCriticalDirectiveClass: |
| 216 | EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S)); |
| 217 | break; |
| 218 | case Stmt::OMPParallelForDirectiveClass: |
| 219 | EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S)); |
| 220 | break; |
| 221 | case Stmt::OMPParallelForSimdDirectiveClass: |
| 222 | EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S)); |
| 223 | break; |
| 224 | case Stmt::OMPParallelSectionsDirectiveClass: |
| 225 | EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S)); |
| 226 | break; |
| 227 | case Stmt::OMPTaskDirectiveClass: |
| 228 | EmitOMPTaskDirective(cast<OMPTaskDirective>(*S)); |
| 229 | break; |
| 230 | case Stmt::OMPTaskyieldDirectiveClass: |
| 231 | EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S)); |
| 232 | break; |
| 233 | case Stmt::OMPBarrierDirectiveClass: |
| 234 | EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S)); |
| 235 | break; |
| 236 | case Stmt::OMPTaskwaitDirectiveClass: |
| 237 | EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S)); |
| 238 | break; |
| 239 | case Stmt::OMPTaskgroupDirectiveClass: |
| 240 | EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S)); |
| 241 | break; |
| 242 | case Stmt::OMPFlushDirectiveClass: |
| 243 | EmitOMPFlushDirective(cast<OMPFlushDirective>(*S)); |
| 244 | break; |
| 245 | case Stmt::OMPOrderedDirectiveClass: |
| 246 | EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S)); |
| 247 | break; |
| 248 | case Stmt::OMPAtomicDirectiveClass: |
| 249 | EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S)); |
| 250 | break; |
| 251 | case Stmt::OMPTargetDirectiveClass: |
| 252 | EmitOMPTargetDirective(cast<OMPTargetDirective>(*S)); |
| 253 | break; |
| 254 | case Stmt::OMPTeamsDirectiveClass: |
| 255 | EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S)); |
| 256 | break; |
| 257 | case Stmt::OMPCancellationPointDirectiveClass: |
| 258 | EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S)); |
| 259 | break; |
| 260 | case Stmt::OMPCancelDirectiveClass: |
| 261 | EmitOMPCancelDirective(cast<OMPCancelDirective>(*S)); |
| 262 | break; |
| 263 | case Stmt::OMPTargetDataDirectiveClass: |
| 264 | EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S)); |
| 265 | break; |
| 266 | case Stmt::OMPTargetEnterDataDirectiveClass: |
| 267 | EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S)); |
| 268 | break; |
| 269 | case Stmt::OMPTargetExitDataDirectiveClass: |
| 270 | EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S)); |
| 271 | break; |
| 272 | case Stmt::OMPTargetParallelDirectiveClass: |
| 273 | EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S)); |
| 274 | break; |
| 275 | case Stmt::OMPTargetParallelForDirectiveClass: |
| 276 | EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S)); |
| 277 | break; |
| 278 | case Stmt::OMPTaskLoopDirectiveClass: |
| 279 | EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S)); |
| 280 | break; |
| 281 | case Stmt::OMPTaskLoopSimdDirectiveClass: |
| 282 | EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S)); |
| 283 | break; |
| 284 | case Stmt::OMPDistributeDirectiveClass: |
| 285 | EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S)); |
| 286 | break; |
| 287 | case Stmt::OMPTargetUpdateDirectiveClass: |
| 288 | EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S)); |
| 289 | break; |
| 290 | case Stmt::OMPDistributeParallelForDirectiveClass: |
| 291 | EmitOMPDistributeParallelForDirective( |
| 292 | cast<OMPDistributeParallelForDirective>(*S)); |
| 293 | break; |
| 294 | case Stmt::OMPDistributeParallelForSimdDirectiveClass: |
| 295 | EmitOMPDistributeParallelForSimdDirective( |
| 296 | cast<OMPDistributeParallelForSimdDirective>(*S)); |
| 297 | break; |
| 298 | case Stmt::OMPDistributeSimdDirectiveClass: |
| 299 | EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S)); |
| 300 | break; |
| 301 | case Stmt::OMPTargetParallelForSimdDirectiveClass: |
| 302 | EmitOMPTargetParallelForSimdDirective( |
| 303 | cast<OMPTargetParallelForSimdDirective>(*S)); |
| 304 | break; |
| 305 | case Stmt::OMPTargetSimdDirectiveClass: |
| 306 | EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S)); |
| 307 | break; |
| 308 | case Stmt::OMPTeamsDistributeDirectiveClass: |
| 309 | EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S)); |
| 310 | break; |
| 311 | case Stmt::OMPTeamsDistributeSimdDirectiveClass: |
| 312 | EmitOMPTeamsDistributeSimdDirective( |
| 313 | cast<OMPTeamsDistributeSimdDirective>(*S)); |
| 314 | break; |
| 315 | case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass: |
| 316 | EmitOMPTeamsDistributeParallelForSimdDirective( |
| 317 | cast<OMPTeamsDistributeParallelForSimdDirective>(*S)); |
| 318 | break; |
| 319 | case Stmt::OMPTeamsDistributeParallelForDirectiveClass: |
| 320 | EmitOMPTeamsDistributeParallelForDirective( |
| 321 | cast<OMPTeamsDistributeParallelForDirective>(*S)); |
| 322 | break; |
| 323 | case Stmt::OMPTargetTeamsDirectiveClass: |
| 324 | EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S)); |
| 325 | break; |
| 326 | case Stmt::OMPTargetTeamsDistributeDirectiveClass: |
| 327 | EmitOMPTargetTeamsDistributeDirective( |
| 328 | cast<OMPTargetTeamsDistributeDirective>(*S)); |
| 329 | break; |
| 330 | case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass: |
| 331 | EmitOMPTargetTeamsDistributeParallelForDirective( |
| 332 | cast<OMPTargetTeamsDistributeParallelForDirective>(*S)); |
| 333 | break; |
| 334 | case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass: |
| 335 | EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 336 | cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S)); |
| 337 | break; |
| 338 | case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass: |
| 339 | EmitOMPTargetTeamsDistributeSimdDirective( |
| 340 | cast<OMPTargetTeamsDistributeSimdDirective>(*S)); |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) { |
| 346 | switch (S->getStmtClass()) { |
| 347 | default: return false; |
| 348 | case Stmt::NullStmtClass: break; |
| 349 | case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break; |
| 350 | case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break; |
| 351 | case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break; |
| 352 | case Stmt::AttributedStmtClass: |
| 353 | EmitAttributedStmt(cast<AttributedStmt>(*S)); break; |
| 354 | case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break; |
| 355 | case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break; |
| 356 | case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break; |
| 357 | case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break; |
| 358 | case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break; |
| 359 | case Stmt::SEHLeaveStmtClass: EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); break; |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 369 | AggValueSlot AggSlot) { |
| 370 | PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(), |
| 371 | "LLVM IR generation of compound statement ('{}')"); |
| 372 | |
| 373 | |
| 374 | LexicalScope Scope(*this, S.getSourceRange()); |
| 375 | |
| 376 | return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot); |
| 377 | } |
| 378 | |
| 379 | Address |
| 380 | CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S, |
| 381 | bool GetLast, |
| 382 | AggValueSlot AggSlot) { |
| 383 | |
| 384 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 385 | E = S.body_end()-GetLast; I != E; ++I) |
| 386 | EmitStmt(*I); |
| 387 | |
| 388 | Address RetAlloca = Address::invalid(); |
| 389 | if (GetLast) { |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | const Stmt *LastStmt = S.body_back(); |
| 396 | while (!isa<Expr>(LastStmt)) { |
| 397 | if (const auto *LS = dyn_cast<LabelStmt>(LastStmt)) { |
| 398 | EmitLabel(LS->getDecl()); |
| 399 | LastStmt = LS->getSubStmt(); |
| 400 | } else if (const auto *AS = dyn_cast<AttributedStmt>(LastStmt)) { |
| 401 | |
| 402 | |
| 403 | LastStmt = AS->getSubStmt(); |
| 404 | } else { |
| 405 | llvm_unreachable("unknown value statement"); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | EnsureInsertPoint(); |
| 410 | |
| 411 | const Expr *E = cast<Expr>(LastStmt); |
| 412 | QualType ExprTy = E->getType(); |
| 413 | if (hasAggregateEvaluationKind(ExprTy)) { |
| 414 | EmitAggExpr(E, AggSlot); |
| 415 | } else { |
| 416 | |
| 417 | |
| 418 | |
| 419 | RetAlloca = CreateMemTemp(ExprTy); |
| 420 | EmitAnyExprToMem(E, RetAlloca, Qualifiers(), |
| 421 | false); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return RetAlloca; |
| 426 | } |
| 427 | |
| 428 | void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) { |
| 429 | llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator()); |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | if (!EHStack.empty()) |
| 435 | return; |
| 436 | |
| 437 | |
| 438 | if (!BI || !BI->isUnconditional()) |
| 439 | return; |
| 440 | |
| 441 | |
| 442 | if (BI->getIterator() != BB->begin()) |
| 443 | return; |
| 444 | |
| 445 | BB->replaceAllUsesWith(BI->getSuccessor(0)); |
| 446 | BI->eraseFromParent(); |
| 447 | BB->eraseFromParent(); |
| 448 | } |
| 449 | |
| 450 | void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) { |
| 451 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 452 | |
| 453 | |
| 454 | EmitBranch(BB); |
| 455 | |
| 456 | if (IsFinished && BB->use_empty()) { |
| 457 | delete BB; |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | if (CurBB && CurBB->getParent()) |
| 464 | CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB); |
| 465 | else |
| 466 | CurFn->getBasicBlockList().push_back(BB); |
| 467 | Builder.SetInsertPoint(BB); |
| 468 | } |
| 469 | |
| 470 | void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) { |
| 471 | |
| 472 | |
| 473 | |
| 474 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 475 | |
| 476 | if (!CurBB || CurBB->getTerminator()) { |
| 477 | |
| 478 | |
| 479 | } else { |
| 480 | |
| 481 | Builder.CreateBr(Target); |
| 482 | } |
| 483 | |
| 484 | Builder.ClearInsertionPoint(); |
| 485 | } |
| 486 | |
| 487 | void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) { |
| 488 | bool inserted = false; |
| 489 | for (llvm::User *u : block->users()) { |
| 490 | if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) { |
| 491 | CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(), |
| 492 | block); |
| 493 | inserted = true; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (!inserted) |
| 499 | CurFn->getBasicBlockList().push_back(block); |
| 500 | |
| 501 | Builder.SetInsertPoint(block); |
| 502 | } |
| 503 | |
| 504 | CodeGenFunction::JumpDest |
| 505 | CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) { |
| 506 | JumpDest &Dest = LabelMap[D]; |
| 507 | if (Dest.isValid()) return Dest; |
| 508 | |
| 509 | |
| 510 | Dest = JumpDest(createBasicBlock(D->getName()), |
| 511 | EHScopeStack::stable_iterator::invalid(), |
| 512 | NextCleanupDestIndex++); |
| 513 | return Dest; |
| 514 | } |
| 515 | |
| 516 | void CodeGenFunction::EmitLabel(const LabelDecl *D) { |
| 517 | |
| 518 | |
| 519 | |
| 520 | if (EHStack.hasNormalCleanups() && CurLexicalScope) |
| 521 | CurLexicalScope->addLabel(D); |
| 522 | |
| 523 | JumpDest &Dest = LabelMap[D]; |
| 524 | |
| 525 | |
| 526 | |
| 527 | if (!Dest.isValid()) { |
| 528 | Dest = getJumpDestInCurrentScope(D->getName()); |
| 529 | |
| 530 | |
| 531 | |
| 532 | } else { |
| 533 | (0) . __assert_fail ("!Dest.getScopeDepth().isValid() && \"already emitted label!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 533, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Dest.getScopeDepth().isValid() && "already emitted label!"); |
| 534 | Dest.setScopeDepth(EHStack.stable_begin()); |
| 535 | ResolveBranchFixups(Dest.getBlock()); |
| 536 | } |
| 537 | |
| 538 | EmitBlock(Dest.getBlock()); |
| 539 | |
| 540 | |
| 541 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 542 | if (CGM.getCodeGenOpts().getDebugInfo() >= |
| 543 | codegenoptions::LimitedDebugInfo) { |
| 544 | DI->setLocation(D->getLocation()); |
| 545 | DI->EmitLabel(D, Builder); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | incrementProfileCounter(D->getStmt()); |
| 550 | } |
| 551 | |
| 552 | |
| 553 | |
| 554 | void CodeGenFunction::LexicalScope::rescopeLabels() { |
| 555 | assert(!Labels.empty()); |
| 556 | EHScopeStack::stable_iterator innermostScope |
| 557 | = CGF.EHStack.getInnermostNormalCleanup(); |
| 558 | |
| 559 | |
| 560 | for (SmallVectorImpl<const LabelDecl*>::const_iterator |
| 561 | i = Labels.begin(), e = Labels.end(); i != e; ++i) { |
| 562 | assert(CGF.LabelMap.count(*i)); |
| 563 | JumpDest &dest = CGF.LabelMap.find(*i)->second; |
| 564 | assert(dest.getScopeDepth().isValid()); |
| 565 | assert(innermostScope.encloses(dest.getScopeDepth())); |
| 566 | dest.setScopeDepth(innermostScope); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | if (innermostScope != EHScopeStack::stable_end() && ParentScope) { |
| 571 | ParentScope->Labels.append(Labels.begin(), Labels.end()); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 577 | EmitLabel(S.getDecl()); |
| 578 | EmitStmt(S.getSubStmt()); |
| 579 | } |
| 580 | |
| 581 | void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) { |
| 582 | EmitStmt(S.getSubStmt(), S.getAttrs()); |
| 583 | } |
| 584 | |
| 585 | void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) { |
| 586 | |
| 587 | |
| 588 | |
| 589 | if (HaveInsertPoint()) |
| 590 | EmitStopPoint(&S); |
| 591 | |
| 592 | EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel())); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) { |
| 597 | if (const LabelDecl *Target = S.getConstantTarget()) { |
| 598 | EmitBranchThroughCleanup(getJumpDestForLabel(Target)); |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()), |
| 604 | Int8PtrTy, "addr"); |
| 605 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 606 | |
| 607 | |
| 608 | llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock(); |
| 609 | |
| 610 | |
| 611 | |
| 612 | cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB); |
| 613 | |
| 614 | EmitBranch(IndGotoBB); |
| 615 | } |
| 616 | |
| 617 | void CodeGenFunction::EmitIfStmt(const IfStmt &S) { |
| 618 | |
| 619 | |
| 620 | LexicalScope ConditionScope(*this, S.getCond()->getSourceRange()); |
| 621 | |
| 622 | if (S.getInit()) |
| 623 | EmitStmt(S.getInit()); |
| 624 | |
| 625 | if (S.getConditionVariable()) |
| 626 | EmitDecl(*S.getConditionVariable()); |
| 627 | |
| 628 | |
| 629 | |
| 630 | bool CondConstant; |
| 631 | if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant, |
| 632 | S.isConstexpr())) { |
| 633 | |
| 634 | const Stmt *Executed = S.getThen(); |
| 635 | const Stmt *Skipped = S.getElse(); |
| 636 | if (!CondConstant) |
| 637 | std::swap(Executed, Skipped); |
| 638 | |
| 639 | |
| 640 | |
| 641 | if (S.isConstexpr() || !ContainsLabel(Skipped)) { |
| 642 | if (CondConstant) |
| 643 | incrementProfileCounter(&S); |
| 644 | if (Executed) { |
| 645 | RunCleanupsScope ExecutedScope(*this); |
| 646 | EmitStmt(Executed); |
| 647 | } |
| 648 | return; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |
| 654 | llvm::BasicBlock *ThenBlock = createBasicBlock("if.then"); |
| 655 | llvm::BasicBlock *ContBlock = createBasicBlock("if.end"); |
| 656 | llvm::BasicBlock *ElseBlock = ContBlock; |
| 657 | if (S.getElse()) |
| 658 | ElseBlock = createBasicBlock("if.else"); |
| 659 | |
| 660 | EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock, |
| 661 | getProfileCount(S.getThen())); |
| 662 | |
| 663 | |
| 664 | EmitBlock(ThenBlock); |
| 665 | incrementProfileCounter(&S); |
| 666 | { |
| 667 | RunCleanupsScope ThenScope(*this); |
| 668 | EmitStmt(S.getThen()); |
| 669 | } |
| 670 | EmitBranch(ContBlock); |
| 671 | |
| 672 | |
| 673 | if (const Stmt *Else = S.getElse()) { |
| 674 | { |
| 675 | |
| 676 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
| 677 | EmitBlock(ElseBlock); |
| 678 | } |
| 679 | { |
| 680 | RunCleanupsScope ElseScope(*this); |
| 681 | EmitStmt(Else); |
| 682 | } |
| 683 | { |
| 684 | |
| 685 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
| 686 | EmitBranch(ContBlock); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | |
| 691 | EmitBlock(ContBlock, true); |
| 692 | } |
| 693 | |
| 694 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S, |
| 695 | ArrayRef<const Attr *> WhileAttrs) { |
| 696 | |
| 697 | |
| 698 | JumpDest = getJumpDestInCurrentScope("while.cond"); |
| 699 | EmitBlock(LoopHeader.getBlock()); |
| 700 | |
| 701 | const SourceRange &R = S.getSourceRange(); |
| 702 | LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), WhileAttrs, |
| 703 | SourceLocToDebugLoc(R.getBegin()), |
| 704 | SourceLocToDebugLoc(R.getEnd())); |
| 705 | |
| 706 | |
| 707 | |
| 708 | JumpDest LoopExit = getJumpDestInCurrentScope("while.end"); |
| 709 | |
| 710 | |
| 711 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader)); |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | RunCleanupsScope ConditionScope(*this); |
| 721 | |
| 722 | if (S.getConditionVariable()) |
| 723 | EmitDecl(*S.getConditionVariable()); |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 729 | |
| 730 | |
| 731 | |
| 732 | bool EmitBoolCondBranch = true; |
| 733 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 734 | if (C->isOne()) |
| 735 | EmitBoolCondBranch = false; |
| 736 | |
| 737 | |
| 738 | llvm::BasicBlock *LoopBody = createBasicBlock("while.body"); |
| 739 | if (EmitBoolCondBranch) { |
| 740 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
| 741 | if (ConditionScope.requiresCleanups()) |
| 742 | ExitBlock = createBasicBlock("while.exit"); |
| 743 | Builder.CreateCondBr( |
| 744 | BoolCondVal, LoopBody, ExitBlock, |
| 745 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
| 746 | |
| 747 | if (ExitBlock != LoopExit.getBlock()) { |
| 748 | EmitBlock(ExitBlock); |
| 749 | EmitBranchThroughCleanup(LoopExit); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
| 754 | |
| 755 | { |
| 756 | RunCleanupsScope BodyScope(*this); |
| 757 | EmitBlock(LoopBody); |
| 758 | incrementProfileCounter(&S); |
| 759 | EmitStmt(S.getBody()); |
| 760 | } |
| 761 | |
| 762 | BreakContinueStack.pop_back(); |
| 763 | |
| 764 | |
| 765 | ConditionScope.ForceCleanup(); |
| 766 | |
| 767 | EmitStopPoint(&S); |
| 768 | |
| 769 | EmitBranch(LoopHeader.getBlock()); |
| 770 | |
| 771 | LoopStack.pop(); |
| 772 | |
| 773 | |
| 774 | EmitBlock(LoopExit.getBlock(), true); |
| 775 | |
| 776 | |
| 777 | |
| 778 | if (!EmitBoolCondBranch) |
| 779 | SimplifyForwardingBlocks(LoopHeader.getBlock()); |
| 780 | } |
| 781 | |
| 782 | void CodeGenFunction::EmitDoStmt(const DoStmt &S, |
| 783 | ArrayRef<const Attr *> DoAttrs) { |
| 784 | JumpDest LoopExit = getJumpDestInCurrentScope("do.end"); |
| 785 | JumpDest LoopCond = getJumpDestInCurrentScope("do.cond"); |
| 786 | |
| 787 | uint64_t ParentCount = getCurrentProfileCount(); |
| 788 | |
| 789 | |
| 790 | BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond)); |
| 791 | |
| 792 | |
| 793 | llvm::BasicBlock *LoopBody = createBasicBlock("do.body"); |
| 794 | |
| 795 | EmitBlockWithFallThrough(LoopBody, &S); |
| 796 | { |
| 797 | RunCleanupsScope BodyScope(*this); |
| 798 | EmitStmt(S.getBody()); |
| 799 | } |
| 800 | |
| 801 | EmitBlock(LoopCond.getBlock()); |
| 802 | |
| 803 | const SourceRange &R = S.getSourceRange(); |
| 804 | LoopStack.push(LoopBody, CGM.getContext(), DoAttrs, |
| 805 | SourceLocToDebugLoc(R.getBegin()), |
| 806 | SourceLocToDebugLoc(R.getEnd())); |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 815 | |
| 816 | BreakContinueStack.pop_back(); |
| 817 | |
| 818 | |
| 819 | |
| 820 | bool EmitBoolCondBranch = true; |
| 821 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 822 | if (C->isZero()) |
| 823 | EmitBoolCondBranch = false; |
| 824 | |
| 825 | |
| 826 | if (EmitBoolCondBranch) { |
| 827 | uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount; |
| 828 | Builder.CreateCondBr( |
| 829 | BoolCondVal, LoopBody, LoopExit.getBlock(), |
| 830 | createProfileWeightsForLoop(S.getCond(), BackedgeCount)); |
| 831 | } |
| 832 | |
| 833 | LoopStack.pop(); |
| 834 | |
| 835 | |
| 836 | EmitBlock(LoopExit.getBlock()); |
| 837 | |
| 838 | |
| 839 | |
| 840 | if (!EmitBoolCondBranch) |
| 841 | SimplifyForwardingBlocks(LoopCond.getBlock()); |
| 842 | } |
| 843 | |
| 844 | void CodeGenFunction::EmitForStmt(const ForStmt &S, |
| 845 | ArrayRef<const Attr *> ForAttrs) { |
| 846 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); |
| 847 | |
| 848 | LexicalScope ForScope(*this, S.getSourceRange()); |
| 849 | |
| 850 | |
| 851 | if (S.getInit()) |
| 852 | EmitStmt(S.getInit()); |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | JumpDest Continue = getJumpDestInCurrentScope("for.cond"); |
| 858 | llvm::BasicBlock *CondBlock = Continue.getBlock(); |
| 859 | EmitBlock(CondBlock); |
| 860 | |
| 861 | const SourceRange &R = S.getSourceRange(); |
| 862 | LoopStack.push(CondBlock, CGM.getContext(), ForAttrs, |
| 863 | SourceLocToDebugLoc(R.getBegin()), |
| 864 | SourceLocToDebugLoc(R.getEnd())); |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | if (S.getInc()) |
| 871 | Continue = getJumpDestInCurrentScope("for.inc"); |
| 872 | |
| 873 | |
| 874 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 875 | |
| 876 | |
| 877 | LexicalScope ConditionScope(*this, S.getSourceRange()); |
| 878 | |
| 879 | if (S.getCond()) { |
| 880 | |
| 881 | |
| 882 | if (S.getConditionVariable()) { |
| 883 | EmitDecl(*S.getConditionVariable()); |
| 884 | } |
| 885 | |
| 886 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
| 887 | |
| 888 | |
| 889 | if (ForScope.requiresCleanups()) |
| 890 | ExitBlock = createBasicBlock("for.cond.cleanup"); |
| 891 | |
| 892 | |
| 893 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 894 | |
| 895 | |
| 896 | |
| 897 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 898 | Builder.CreateCondBr( |
| 899 | BoolCondVal, ForBody, ExitBlock, |
| 900 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
| 901 | |
| 902 | if (ExitBlock != LoopExit.getBlock()) { |
| 903 | EmitBlock(ExitBlock); |
| 904 | EmitBranchThroughCleanup(LoopExit); |
| 905 | } |
| 906 | |
| 907 | EmitBlock(ForBody); |
| 908 | } else { |
| 909 | |
| 910 | |
| 911 | } |
| 912 | incrementProfileCounter(&S); |
| 913 | |
| 914 | { |
| 915 | |
| 916 | |
| 917 | RunCleanupsScope BodyScope(*this); |
| 918 | EmitStmt(S.getBody()); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | if (S.getInc()) { |
| 923 | EmitBlock(Continue.getBlock()); |
| 924 | EmitStmt(S.getInc()); |
| 925 | } |
| 926 | |
| 927 | BreakContinueStack.pop_back(); |
| 928 | |
| 929 | ConditionScope.ForceCleanup(); |
| 930 | |
| 931 | EmitStopPoint(&S); |
| 932 | EmitBranch(CondBlock); |
| 933 | |
| 934 | ForScope.ForceCleanup(); |
| 935 | |
| 936 | LoopStack.pop(); |
| 937 | |
| 938 | |
| 939 | EmitBlock(LoopExit.getBlock(), true); |
| 940 | } |
| 941 | |
| 942 | void |
| 943 | CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S, |
| 944 | ArrayRef<const Attr *> ForAttrs) { |
| 945 | JumpDest LoopExit = getJumpDestInCurrentScope("for.end"); |
| 946 | |
| 947 | LexicalScope ForScope(*this, S.getSourceRange()); |
| 948 | |
| 949 | |
| 950 | if (S.getInit()) |
| 951 | EmitStmt(S.getInit()); |
| 952 | EmitStmt(S.getRangeStmt()); |
| 953 | EmitStmt(S.getBeginStmt()); |
| 954 | EmitStmt(S.getEndStmt()); |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 960 | EmitBlock(CondBlock); |
| 961 | |
| 962 | const SourceRange &R = S.getSourceRange(); |
| 963 | LoopStack.push(CondBlock, CGM.getContext(), ForAttrs, |
| 964 | SourceLocToDebugLoc(R.getBegin()), |
| 965 | SourceLocToDebugLoc(R.getEnd())); |
| 966 | |
| 967 | |
| 968 | |
| 969 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
| 970 | if (ForScope.requiresCleanups()) |
| 971 | ExitBlock = createBasicBlock("for.cond.cleanup"); |
| 972 | |
| 973 | |
| 974 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 975 | |
| 976 | |
| 977 | |
| 978 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 979 | Builder.CreateCondBr( |
| 980 | BoolCondVal, ForBody, ExitBlock, |
| 981 | createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody()))); |
| 982 | |
| 983 | if (ExitBlock != LoopExit.getBlock()) { |
| 984 | EmitBlock(ExitBlock); |
| 985 | EmitBranchThroughCleanup(LoopExit); |
| 986 | } |
| 987 | |
| 988 | EmitBlock(ForBody); |
| 989 | incrementProfileCounter(&S); |
| 990 | |
| 991 | |
| 992 | JumpDest Continue = getJumpDestInCurrentScope("for.inc"); |
| 993 | |
| 994 | |
| 995 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 996 | |
| 997 | { |
| 998 | |
| 999 | LexicalScope BodyScope(*this, S.getSourceRange()); |
| 1000 | EmitStmt(S.getLoopVarStmt()); |
| 1001 | EmitStmt(S.getBody()); |
| 1002 | } |
| 1003 | |
| 1004 | EmitStopPoint(&S); |
| 1005 | |
| 1006 | EmitBlock(Continue.getBlock()); |
| 1007 | EmitStmt(S.getInc()); |
| 1008 | |
| 1009 | BreakContinueStack.pop_back(); |
| 1010 | |
| 1011 | EmitBranch(CondBlock); |
| 1012 | |
| 1013 | ForScope.ForceCleanup(); |
| 1014 | |
| 1015 | LoopStack.pop(); |
| 1016 | |
| 1017 | |
| 1018 | EmitBlock(LoopExit.getBlock(), true); |
| 1019 | } |
| 1020 | |
| 1021 | void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) { |
| 1022 | if (RV.isScalar()) { |
| 1023 | Builder.CreateStore(RV.getScalarVal(), ReturnValue); |
| 1024 | } else if (RV.isAggregate()) { |
| 1025 | LValue Dest = MakeAddrLValue(ReturnValue, Ty); |
| 1026 | LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty); |
| 1027 | EmitAggregateCopy(Dest, Src, Ty, overlapForReturnValue()); |
| 1028 | } else { |
| 1029 | EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty), |
| 1030 | true); |
| 1031 | } |
| 1032 | EmitBranchThroughCleanup(ReturnBlock); |
| 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
| 1039 | if (requiresReturnValueCheck()) { |
| 1040 | llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc()); |
| 1041 | auto *SLocPtr = |
| 1042 | new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false, |
| 1043 | llvm::GlobalVariable::PrivateLinkage, SLoc); |
| 1044 | SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 1045 | CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr); |
| 1046 | (0) . __assert_fail ("ReturnLocation.isValid() && \"No valid return location\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1046, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ReturnLocation.isValid() && "No valid return location"); |
| 1047 | Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy), |
| 1048 | ReturnLocation); |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | if (IsOutlinedSEHHelper) { |
| 1053 | Builder.CreateUnreachable(); |
| 1054 | Builder.ClearInsertionPoint(); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | const Expr *RV = S.getRetValue(); |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | RunCleanupsScope cleanupScope(*this); |
| 1065 | if (const FullExpr *fe = dyn_cast_or_null<FullExpr>(RV)) { |
| 1066 | enterFullExpression(fe); |
| 1067 | RV = fe->getSubExpr(); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | if (getLangOpts().ElideConstructors && |
| 1073 | S.getNRVOCandidate() && S.getNRVOCandidate()->isNRVOVariable()) { |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()]) |
| 1081 | Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag); |
| 1082 | } else if (!ReturnValue.isValid() || (RV && RV->getType()->isVoidType())) { |
| 1083 | |
| 1084 | |
| 1085 | if (RV) |
| 1086 | EmitAnyExpr(RV); |
| 1087 | } else if (!RV) { |
| 1088 | |
| 1089 | } else if (FnRetTy->isReferenceType()) { |
| 1090 | |
| 1091 | |
| 1092 | RValue Result = EmitReferenceBindingToExpr(RV); |
| 1093 | Builder.CreateStore(Result.getScalarVal(), ReturnValue); |
| 1094 | } else { |
| 1095 | switch (getEvaluationKind(RV->getType())) { |
| 1096 | case TEK_Scalar: |
| 1097 | Builder.CreateStore(EmitScalarExpr(RV), ReturnValue); |
| 1098 | break; |
| 1099 | case TEK_Complex: |
| 1100 | EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()), |
| 1101 | true); |
| 1102 | break; |
| 1103 | case TEK_Aggregate: |
| 1104 | EmitAggExpr(RV, AggValueSlot::forAddr( |
| 1105 | ReturnValue, Qualifiers(), |
| 1106 | AggValueSlot::IsDestructed, |
| 1107 | AggValueSlot::DoesNotNeedGCBarriers, |
| 1108 | AggValueSlot::IsNotAliased, |
| 1109 | overlapForReturnValue())); |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | ++NumReturnExprs; |
| 1115 | if (!RV || RV->isEvaluatable(getContext())) |
| 1116 | ++NumSimpleReturnExprs; |
| 1117 | |
| 1118 | cleanupScope.ForceCleanup(); |
| 1119 | EmitBranchThroughCleanup(ReturnBlock); |
| 1120 | } |
| 1121 | |
| 1122 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
| 1123 | |
| 1124 | |
| 1125 | if (HaveInsertPoint()) |
| 1126 | EmitStopPoint(&S); |
| 1127 | |
| 1128 | for (const auto *I : S.decls()) |
| 1129 | EmitDecl(*I); |
| 1130 | } |
| 1131 | |
| 1132 | void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) { |
| 1133 | (0) . __assert_fail ("!BreakContinueStack.empty() && \"break stmt not in a loop or switch!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1133, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | if (HaveInsertPoint()) |
| 1139 | EmitStopPoint(&S); |
| 1140 | |
| 1141 | EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock); |
| 1142 | } |
| 1143 | |
| 1144 | void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) { |
| 1145 | (0) . __assert_fail ("!BreakContinueStack.empty() && \"continue stmt not in a loop!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1145, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | if (HaveInsertPoint()) |
| 1151 | EmitStopPoint(&S); |
| 1152 | |
| 1153 | EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
| 1160 | (0) . __assert_fail ("S.getRHS() && \"Expected RHS value in CaseStmt\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1160, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S.getRHS() && "Expected RHS value in CaseStmt"); |
| 1161 | |
| 1162 | llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext()); |
| 1163 | llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext()); |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); |
| 1169 | EmitBlockWithFallThrough(CaseDest, &S); |
| 1170 | EmitStmt(S.getSubStmt()); |
| 1171 | |
| 1172 | |
| 1173 | if (LHS.isSigned() ? RHS.slt(LHS) : RHS.ult(LHS)) |
| 1174 | return; |
| 1175 | |
| 1176 | llvm::APInt Range = RHS - LHS; |
| 1177 | |
| 1178 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 1179 | |
| 1180 | uint64_t Total = getProfileCount(&S); |
| 1181 | unsigned NCases = Range.getZExtValue() + 1; |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | uint64_t Weight = Total / NCases, Rem = Total % NCases; |
| 1187 | for (unsigned I = 0; I != NCases; ++I) { |
| 1188 | if (SwitchWeights) |
| 1189 | SwitchWeights->push_back(Weight + (Rem ? 1 : 0)); |
| 1190 | if (Rem) |
| 1191 | Rem--; |
| 1192 | SwitchInsn->addCase(Builder.getInt(LHS), CaseDest); |
| 1193 | ++LHS; |
| 1194 | } |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock(); |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | llvm::BasicBlock *FalseDest = CaseRangeBlock; |
| 1206 | CaseRangeBlock = createBasicBlock("sw.caserange"); |
| 1207 | |
| 1208 | CurFn->getBasicBlockList().push_back(CaseRangeBlock); |
| 1209 | Builder.SetInsertPoint(CaseRangeBlock); |
| 1210 | |
| 1211 | |
| 1212 | llvm::Value *Diff = |
| 1213 | Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS)); |
| 1214 | llvm::Value *Cond = |
| 1215 | Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds"); |
| 1216 | |
| 1217 | llvm::MDNode *Weights = nullptr; |
| 1218 | if (SwitchWeights) { |
| 1219 | uint64_t ThisCount = getProfileCount(&S); |
| 1220 | uint64_t DefaultCount = (*SwitchWeights)[0]; |
| 1221 | Weights = createProfileWeights(ThisCount, DefaultCount); |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | (*SwitchWeights)[0] += ThisCount; |
| 1227 | } |
| 1228 | Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights); |
| 1229 | |
| 1230 | |
| 1231 | if (RestoreBB) |
| 1232 | Builder.SetInsertPoint(RestoreBB); |
| 1233 | else |
| 1234 | Builder.ClearInsertionPoint(); |
| 1235 | } |
| 1236 | |
| 1237 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | if (!SwitchInsn) { |
| 1244 | EmitStmt(S.getSubStmt()); |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | if (S.getRHS()) { |
| 1250 | EmitCaseStmtRange(S); |
| 1251 | return; |
| 1252 | } |
| 1253 | |
| 1254 | llvm::ConstantInt *CaseVal = |
| 1255 | Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext())); |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | if (!CGM.getCodeGenOpts().hasProfileClangInstr() && |
| 1261 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 1262 | isa<BreakStmt>(S.getSubStmt())) { |
| 1263 | JumpDest Block = BreakContinueStack.back().BreakBlock; |
| 1264 | |
| 1265 | |
| 1266 | if (isObviouslyBranchWithoutCleanups(Block)) { |
| 1267 | if (SwitchWeights) |
| 1268 | SwitchWeights->push_back(getProfileCount(&S)); |
| 1269 | SwitchInsn->addCase(CaseVal, Block.getBlock()); |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | if (Builder.GetInsertBlock()) { |
| 1274 | Builder.CreateBr(Block.getBlock()); |
| 1275 | Builder.ClearInsertionPoint(); |
| 1276 | } |
| 1277 | return; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb"); |
| 1282 | EmitBlockWithFallThrough(CaseDest, &S); |
| 1283 | if (SwitchWeights) |
| 1284 | SwitchWeights->push_back(getProfileCount(&S)); |
| 1285 | SwitchInsn->addCase(CaseVal, CaseDest); |
| 1286 | |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | const CaseStmt *CurCase = &S; |
| 1297 | const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt()); |
| 1298 | |
| 1299 | |
| 1300 | while (NextCase && NextCase->getRHS() == nullptr) { |
| 1301 | CurCase = NextCase; |
| 1302 | llvm::ConstantInt *CaseVal = |
| 1303 | Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext())); |
| 1304 | |
| 1305 | if (SwitchWeights) |
| 1306 | SwitchWeights->push_back(getProfileCount(NextCase)); |
| 1307 | if (CGM.getCodeGenOpts().hasProfileClangInstr()) { |
| 1308 | CaseDest = createBasicBlock("sw.bb"); |
| 1309 | EmitBlockWithFallThrough(CaseDest, &S); |
| 1310 | } |
| 1311 | |
| 1312 | SwitchInsn->addCase(CaseVal, CaseDest); |
| 1313 | NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt()); |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | EmitStmt(CurCase->getSubStmt()); |
| 1318 | } |
| 1319 | |
| 1320 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | if (!SwitchInsn) { |
| 1325 | EmitStmt(S.getSubStmt()); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest(); |
| 1330 | (0) . __assert_fail ("DefaultBlock->empty() && \"EmitDefaultStmt. Default block already defined?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1331, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DefaultBlock->empty() && |
| 1331 | (0) . __assert_fail ("DefaultBlock->empty() && \"EmitDefaultStmt. Default block already defined?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1331, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "EmitDefaultStmt: Default block already defined?"); |
| 1332 | |
| 1333 | EmitBlockWithFallThrough(DefaultBlock, &S); |
| 1334 | |
| 1335 | EmitStmt(S.getSubStmt()); |
| 1336 | } |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success }; |
| 1362 | static CSFC_Result CollectStatementsForCase(const Stmt *S, |
| 1363 | const SwitchCase *Case, |
| 1364 | bool &FoundCase, |
| 1365 | SmallVectorImpl<const Stmt*> &ResultStmts) { |
| 1366 | |
| 1367 | if (!S) |
| 1368 | return Case ? CSFC_Success : CSFC_FallThrough; |
| 1369 | |
| 1370 | |
| 1371 | |
| 1372 | if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) { |
| 1373 | if (S == Case) { |
| 1374 | FoundCase = true; |
| 1375 | return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase, |
| 1376 | ResultStmts); |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase, |
| 1381 | ResultStmts); |
| 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | if (!Case && isa<BreakStmt>(S)) |
| 1387 | return CSFC_Success; |
| 1388 | |
| 1389 | |
| 1390 | |
| 1391 | if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) { |
| 1392 | |
| 1393 | |
| 1394 | CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end(); |
| 1395 | bool StartedInLiveCode = FoundCase; |
| 1396 | unsigned StartSize = ResultStmts.size(); |
| 1397 | |
| 1398 | |
| 1399 | if (Case) { |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | bool HadSkippedDecl = false; |
| 1404 | |
| 1405 | |
| 1406 | |
| 1407 | for (; Case && I != E; ++I) { |
| 1408 | HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I); |
| 1409 | |
| 1410 | switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) { |
| 1411 | case CSFC_Failure: return CSFC_Failure; |
| 1412 | case CSFC_Success: |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | |
| 1417 | if (FoundCase) { |
| 1418 | |
| 1419 | |
| 1420 | if (HadSkippedDecl) |
| 1421 | return CSFC_Failure; |
| 1422 | |
| 1423 | for (++I; I != E; ++I) |
| 1424 | if (CodeGenFunction::ContainsLabel(*I, true)) |
| 1425 | return CSFC_Failure; |
| 1426 | return CSFC_Success; |
| 1427 | } |
| 1428 | break; |
| 1429 | case CSFC_FallThrough: |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | (0) . __assert_fail ("FoundCase && \"Didn't find case but returned fallthrough?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1433, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FoundCase && "Didn't find case but returned fallthrough?"); |
| 1434 | |
| 1435 | Case = nullptr; |
| 1436 | |
| 1437 | |
| 1438 | |
| 1439 | if (HadSkippedDecl) |
| 1440 | return CSFC_Failure; |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (!FoundCase) |
| 1446 | return CSFC_Success; |
| 1447 | |
| 1448 | (0) . __assert_fail ("!HadSkippedDecl && \"fallthrough after skipping decl\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1448, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!HadSkippedDecl && "fallthrough after skipping decl"); |
| 1449 | } |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | bool AnyDecls = false; |
| 1454 | for (; I != E; ++I) { |
| 1455 | AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I); |
| 1456 | |
| 1457 | switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) { |
| 1458 | case CSFC_Failure: return CSFC_Failure; |
| 1459 | case CSFC_FallThrough: |
| 1460 | |
| 1461 | |
| 1462 | break; |
| 1463 | case CSFC_Success: |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | for (++I; I != E; ++I) |
| 1468 | if (CodeGenFunction::ContainsLabel(*I, true)) |
| 1469 | return CSFC_Failure; |
| 1470 | return CSFC_Success; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 | if (AnyDecls) { |
| 1478 | |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) { |
| 1484 | ResultStmts.resize(StartSize); |
| 1485 | ResultStmts.push_back(S); |
| 1486 | } else { |
| 1487 | return CSFC_Failure; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | return CSFC_FallThrough; |
| 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | if (Case) { |
| 1498 | if (CodeGenFunction::ContainsLabel(S, true)) |
| 1499 | return CSFC_Failure; |
| 1500 | return CSFC_Success; |
| 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | if (CodeGenFunction::containsBreak(S)) return CSFC_Failure; |
| 1506 | |
| 1507 | |
| 1508 | |
| 1509 | ResultStmts.push_back(S); |
| 1510 | return CSFC_FallThrough; |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | |
| 1517 | static bool FindCaseStatementsForValue(const SwitchStmt &S, |
| 1518 | const llvm::APSInt &ConstantCondValue, |
| 1519 | SmallVectorImpl<const Stmt*> &ResultStmts, |
| 1520 | ASTContext &C, |
| 1521 | const SwitchCase *&ResultCase) { |
| 1522 | |
| 1523 | |
| 1524 | const SwitchCase *Case = S.getSwitchCaseList(); |
| 1525 | const DefaultStmt *DefaultCase = nullptr; |
| 1526 | |
| 1527 | for (; Case; Case = Case->getNextSwitchCase()) { |
| 1528 | |
| 1529 | |
| 1530 | if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) { |
| 1531 | DefaultCase = DS; |
| 1532 | continue; |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | const CaseStmt *CS = cast<CaseStmt>(Case); |
| 1537 | |
| 1538 | if (CS->getRHS()) return false; |
| 1539 | |
| 1540 | |
| 1541 | if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue) |
| 1542 | break; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | if (!Case) { |
| 1548 | |
| 1549 | |
| 1550 | if (!DefaultCase) |
| 1551 | return !CodeGenFunction::ContainsLabel(&S); |
| 1552 | Case = DefaultCase; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | bool FoundCase = false; |
| 1564 | ResultCase = Case; |
| 1565 | return CollectStatementsForCase(S.getBody(), Case, FoundCase, |
| 1566 | ResultStmts) != CSFC_Failure && |
| 1567 | FoundCase; |
| 1568 | } |
| 1569 | |
| 1570 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 1571 | |
| 1572 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
| 1573 | SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights; |
| 1574 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | llvm::APSInt ConstantCondValue; |
| 1579 | if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) { |
| 1580 | SmallVector<const Stmt*, 4> CaseStmts; |
| 1581 | const SwitchCase *Case = nullptr; |
| 1582 | if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts, |
| 1583 | getContext(), Case)) { |
| 1584 | if (Case) |
| 1585 | incrementProfileCounter(Case); |
| 1586 | RunCleanupsScope ExecutedScope(*this); |
| 1587 | |
| 1588 | if (S.getInit()) |
| 1589 | EmitStmt(S.getInit()); |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | if (S.getConditionVariable()) |
| 1594 | EmitDecl(*S.getConditionVariable()); |
| 1595 | |
| 1596 | |
| 1597 | |
| 1598 | |
| 1599 | SwitchInsn = nullptr; |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | for (unsigned i = 0, e = CaseStmts.size(); i != e; ++i) |
| 1604 | EmitStmt(CaseStmts[i]); |
| 1605 | incrementProfileCounter(&S); |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | SwitchInsn = SavedSwitchInsn; |
| 1610 | |
| 1611 | return; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog"); |
| 1616 | |
| 1617 | RunCleanupsScope ConditionScope(*this); |
| 1618 | |
| 1619 | if (S.getInit()) |
| 1620 | EmitStmt(S.getInit()); |
| 1621 | |
| 1622 | if (S.getConditionVariable()) |
| 1623 | EmitDecl(*S.getConditionVariable()); |
| 1624 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default"); |
| 1631 | SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock); |
| 1632 | if (PGO.haveRegionCounts()) { |
| 1633 | |
| 1634 | uint64_t DefaultCount = 0; |
| 1635 | unsigned NumCases = 0; |
| 1636 | for (const SwitchCase *Case = S.getSwitchCaseList(); |
| 1637 | Case; |
| 1638 | Case = Case->getNextSwitchCase()) { |
| 1639 | if (isa<DefaultStmt>(Case)) |
| 1640 | DefaultCount = getProfileCount(Case); |
| 1641 | NumCases += 1; |
| 1642 | } |
| 1643 | SwitchWeights = new SmallVector<uint64_t, 16>(); |
| 1644 | SwitchWeights->reserve(NumCases); |
| 1645 | |
| 1646 | |
| 1647 | SwitchWeights->push_back(DefaultCount); |
| 1648 | } |
| 1649 | CaseRangeBlock = DefaultBlock; |
| 1650 | |
| 1651 | |
| 1652 | Builder.ClearInsertionPoint(); |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | JumpDest OuterContinue; |
| 1657 | if (!BreakContinueStack.empty()) |
| 1658 | OuterContinue = BreakContinueStack.back().ContinueBlock; |
| 1659 | |
| 1660 | BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue)); |
| 1661 | |
| 1662 | |
| 1663 | EmitStmt(S.getBody()); |
| 1664 | |
| 1665 | BreakContinueStack.pop_back(); |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | SwitchInsn->setDefaultDest(CaseRangeBlock); |
| 1670 | |
| 1671 | |
| 1672 | if (!DefaultBlock->getParent()) { |
| 1673 | |
| 1674 | |
| 1675 | if (ConditionScope.requiresCleanups()) { |
| 1676 | EmitBlock(DefaultBlock); |
| 1677 | |
| 1678 | |
| 1679 | } else { |
| 1680 | DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock()); |
| 1681 | delete DefaultBlock; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | ConditionScope.ForceCleanup(); |
| 1686 | |
| 1687 | |
| 1688 | EmitBlock(SwitchExit.getBlock(), true); |
| 1689 | incrementProfileCounter(&S); |
| 1690 | |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | auto *Call = dyn_cast<CallExpr>(S.getCond()); |
| 1695 | if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { |
| 1696 | auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl()); |
| 1697 | if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { |
| 1698 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 1699 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable, |
| 1700 | MDHelper.createUnpredictable()); |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | if (SwitchWeights) { |
| 1705 | (0) . __assert_fail ("SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && \"switch weights do not match switch cases\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1706, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && |
| 1706 | (0) . __assert_fail ("SwitchWeights->size() == 1 + SwitchInsn->getNumCases() && \"switch weights do not match switch cases\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1706, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "switch weights do not match switch cases"); |
| 1707 | |
| 1708 | if (SwitchWeights->size() > 1) |
| 1709 | SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof, |
| 1710 | createProfileWeights(*SwitchWeights)); |
| 1711 | delete SwitchWeights; |
| 1712 | } |
| 1713 | SwitchInsn = SavedSwitchInsn; |
| 1714 | SwitchWeights = SavedSwitchWeights; |
| 1715 | CaseRangeBlock = SavedCRBlock; |
| 1716 | } |
| 1717 | |
| 1718 | static std::string |
| 1719 | SimplifyConstraint(const char *Constraint, const TargetInfo &Target, |
| 1720 | SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) { |
| 1721 | std::string Result; |
| 1722 | |
| 1723 | while (*Constraint) { |
| 1724 | switch (*Constraint) { |
| 1725 | default: |
| 1726 | Result += Target.convertConstraint(Constraint); |
| 1727 | break; |
| 1728 | |
| 1729 | case '*': |
| 1730 | case '?': |
| 1731 | case '!': |
| 1732 | case '=': |
| 1733 | case '+': |
| 1734 | break; |
| 1735 | case '#': |
| 1736 | while (Constraint[1] && Constraint[1] != ',') |
| 1737 | Constraint++; |
| 1738 | break; |
| 1739 | case '&': |
| 1740 | case '%': |
| 1741 | Result += *Constraint; |
| 1742 | while (Constraint[1] && Constraint[1] == *Constraint) |
| 1743 | Constraint++; |
| 1744 | break; |
| 1745 | case ',': |
| 1746 | Result += "|"; |
| 1747 | break; |
| 1748 | case 'g': |
| 1749 | Result += "imr"; |
| 1750 | break; |
| 1751 | case '[': { |
| 1752 | (0) . __assert_fail ("OutCons && \"Must pass output names to constraints with a symbolic name\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1753, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OutCons && |
| 1753 | (0) . __assert_fail ("OutCons && \"Must pass output names to constraints with a symbolic name\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1753, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must pass output names to constraints with a symbolic name"); |
| 1754 | unsigned Index; |
| 1755 | bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index); |
| 1756 | (0) . __assert_fail ("result && \"Could not resolve symbolic name\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1756, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(result && "Could not resolve symbolic name"); (void)result; |
| 1757 | Result += llvm::utostr(Index); |
| 1758 | break; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | Constraint++; |
| 1763 | } |
| 1764 | |
| 1765 | return Result; |
| 1766 | } |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | static std::string |
| 1772 | AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr, |
| 1773 | const TargetInfo &Target, CodeGenModule &CGM, |
| 1774 | const AsmStmt &Stmt, const bool EarlyClobber) { |
| 1775 | const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr); |
| 1776 | if (!AsmDeclRef) |
| 1777 | return Constraint; |
| 1778 | const ValueDecl &Value = *AsmDeclRef->getDecl(); |
| 1779 | const VarDecl *Variable = dyn_cast<VarDecl>(&Value); |
| 1780 | if (!Variable) |
| 1781 | return Constraint; |
| 1782 | if (Variable->getStorageClass() != SC_Register) |
| 1783 | return Constraint; |
| 1784 | AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>(); |
| 1785 | if (!Attr) |
| 1786 | return Constraint; |
| 1787 | StringRef Register = Attr->getLabel(); |
| 1788 | assert(Target.isValidGCCRegisterName(Register)); |
| 1789 | |
| 1790 | |
| 1791 | TargetInfo::ConstraintInfo Info(Constraint, ""); |
| 1792 | if (Target.validateOutputConstraint(Info) && |
| 1793 | !Info.allowsRegister()) { |
| 1794 | CGM.ErrorUnsupported(&Stmt, "__asm__"); |
| 1795 | return Constraint; |
| 1796 | } |
| 1797 | |
| 1798 | Register = Target.getNormalizedGCCRegisterName(Register); |
| 1799 | return (EarlyClobber ? "&{" : "{") + Register.str() + "}"; |
| 1800 | } |
| 1801 | |
| 1802 | llvm::Value* |
| 1803 | CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info, |
| 1804 | LValue InputValue, QualType InputType, |
| 1805 | std::string &ConstraintStr, |
| 1806 | SourceLocation Loc) { |
| 1807 | llvm::Value *Arg; |
| 1808 | if (Info.allowsRegister() || !Info.allowsMemory()) { |
| 1809 | if (CodeGenFunction::hasScalarEvaluationKind(InputType)) { |
| 1810 | Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal(); |
| 1811 | } else { |
| 1812 | llvm::Type *Ty = ConvertType(InputType); |
| 1813 | uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty); |
| 1814 | if (Size <= 64 && llvm::isPowerOf2_64(Size)) { |
| 1815 | Ty = llvm::IntegerType::get(getLLVMContext(), Size); |
| 1816 | Ty = llvm::PointerType::getUnqual(Ty); |
| 1817 | |
| 1818 | Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(), |
| 1819 | Ty)); |
| 1820 | } else { |
| 1821 | Arg = InputValue.getPointer(); |
| 1822 | ConstraintStr += '*'; |
| 1823 | } |
| 1824 | } |
| 1825 | } else { |
| 1826 | Arg = InputValue.getPointer(); |
| 1827 | ConstraintStr += '*'; |
| 1828 | } |
| 1829 | |
| 1830 | return Arg; |
| 1831 | } |
| 1832 | |
| 1833 | llvm::Value* CodeGenFunction::EmitAsmInput( |
| 1834 | const TargetInfo::ConstraintInfo &Info, |
| 1835 | const Expr *InputExpr, |
| 1836 | std::string &ConstraintStr) { |
| 1837 | |
| 1838 | |
| 1839 | if (!Info.allowsRegister() && !Info.allowsMemory()) { |
| 1840 | if (Info.requiresImmediateConstant()) { |
| 1841 | Expr::EvalResult EVResult; |
| 1842 | InputExpr->EvaluateAsRValue(EVResult, getContext(), true); |
| 1843 | |
| 1844 | llvm::APSInt IntResult; |
| 1845 | if (!EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(), |
| 1846 | getContext())) |
| 1847 | llvm_unreachable("Invalid immediate constant!"); |
| 1848 | |
| 1849 | return llvm::ConstantInt::get(getLLVMContext(), IntResult); |
| 1850 | } |
| 1851 | |
| 1852 | Expr::EvalResult Result; |
| 1853 | if (InputExpr->EvaluateAsInt(Result, getContext())) |
| 1854 | return llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt()); |
| 1855 | } |
| 1856 | |
| 1857 | if (Info.allowsRegister() || !Info.allowsMemory()) |
| 1858 | if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType())) |
| 1859 | return EmitScalarExpr(InputExpr); |
| 1860 | if (InputExpr->getStmtClass() == Expr::CXXThisExprClass) |
| 1861 | return EmitScalarExpr(InputExpr); |
| 1862 | InputExpr = InputExpr->IgnoreParenNoopCasts(getContext()); |
| 1863 | LValue Dest = EmitLValue(InputExpr); |
| 1864 | return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr, |
| 1865 | InputExpr->getExprLoc()); |
| 1866 | } |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str, |
| 1873 | CodeGenFunction &CGF) { |
| 1874 | SmallVector<llvm::Metadata *, 8> Locs; |
| 1875 | |
| 1876 | Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 1877 | CGF.Int32Ty, Str->getBeginLoc().getRawEncoding()))); |
| 1878 | StringRef StrVal = Str->getString(); |
| 1879 | if (!StrVal.empty()) { |
| 1880 | const SourceManager &SM = CGF.CGM.getContext().getSourceManager(); |
| 1881 | const LangOptions &LangOpts = CGF.CGM.getLangOpts(); |
| 1882 | unsigned StartToken = 0; |
| 1883 | unsigned ByteOffset = 0; |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) { |
| 1888 | if (StrVal[i] != '\n') continue; |
| 1889 | SourceLocation LineLoc = Str->getLocationOfByte( |
| 1890 | i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset); |
| 1891 | Locs.push_back(llvm::ConstantAsMetadata::get( |
| 1892 | llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding()))); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | return llvm::MDNode::get(CGF.getLLVMContext(), Locs); |
| 1897 | } |
| 1898 | |
| 1899 | void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { |
| 1900 | |
| 1901 | std::string AsmString = S.generateAsmString(getContext()); |
| 1902 | |
| 1903 | |
| 1904 | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
| 1905 | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
| 1906 | |
| 1907 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 1908 | StringRef Name; |
| 1909 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
| 1910 | Name = GAS->getOutputName(i); |
| 1911 | TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name); |
| 1912 | bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid; |
| 1913 | (0) . __assert_fail ("IsValid && \"Failed to parse output constraint\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1913, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsValid && "Failed to parse output constraint"); |
| 1914 | OutputConstraintInfos.push_back(Info); |
| 1915 | } |
| 1916 | |
| 1917 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 1918 | StringRef Name; |
| 1919 | if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S)) |
| 1920 | Name = GAS->getInputName(i); |
| 1921 | TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name); |
| 1922 | bool IsValid = |
| 1923 | getTarget().validateInputConstraint(OutputConstraintInfos, Info); |
| 1924 | (0) . __assert_fail ("IsValid && \"Failed to parse input constraint\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1924, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsValid && "Failed to parse input constraint"); (void)IsValid; |
| 1925 | InputConstraintInfos.push_back(Info); |
| 1926 | } |
| 1927 | |
| 1928 | std::string Constraints; |
| 1929 | |
| 1930 | std::vector<LValue> ResultRegDests; |
| 1931 | std::vector<QualType> ResultRegQualTys; |
| 1932 | std::vector<llvm::Type *> ResultRegTypes; |
| 1933 | std::vector<llvm::Type *> ResultTruncRegTypes; |
| 1934 | std::vector<llvm::Type *> ArgTypes; |
| 1935 | std::vector<llvm::Value*> Args; |
| 1936 | |
| 1937 | |
| 1938 | std::string InOutConstraints; |
| 1939 | std::vector<llvm::Value*> InOutArgs; |
| 1940 | std::vector<llvm::Type*> InOutArgTypes; |
| 1941 | |
| 1942 | |
| 1943 | std::vector<std::string> OutputConstraints; |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | bool ReadOnly = true, ReadNone = true; |
| 1952 | |
| 1953 | for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) { |
| 1954 | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
| 1955 | |
| 1956 | |
| 1957 | std::string OutputConstraint(S.getOutputConstraint(i)); |
| 1958 | OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1, |
| 1959 | getTarget(), &OutputConstraintInfos); |
| 1960 | |
| 1961 | const Expr *OutExpr = S.getOutputExpr(i); |
| 1962 | OutExpr = OutExpr->IgnoreParenNoopCasts(getContext()); |
| 1963 | |
| 1964 | OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr, |
| 1965 | getTarget(), CGM, S, |
| 1966 | Info.earlyClobber()); |
| 1967 | OutputConstraints.push_back(OutputConstraint); |
| 1968 | LValue Dest = EmitLValue(OutExpr); |
| 1969 | if (!Constraints.empty()) |
| 1970 | Constraints += ','; |
| 1971 | |
| 1972 | |
| 1973 | |
| 1974 | if (!Info.allowsMemory() && hasScalarEvaluationKind(OutExpr->getType())) { |
| 1975 | Constraints += "=" + OutputConstraint; |
| 1976 | ResultRegQualTys.push_back(OutExpr->getType()); |
| 1977 | ResultRegDests.push_back(Dest); |
| 1978 | ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType())); |
| 1979 | ResultTruncRegTypes.push_back(ResultRegTypes.back()); |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | |
| 1984 | if (Info.hasMatchingInput()) { |
| 1985 | unsigned InputNo; |
| 1986 | for (InputNo = 0; InputNo != S.getNumInputs(); ++InputNo) { |
| 1987 | TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo]; |
| 1988 | if (Input.hasTiedOperand() && Input.getTiedOperand() == i) |
| 1989 | break; |
| 1990 | } |
| 1991 | (0) . __assert_fail ("InputNo != S.getNumInputs() && \"Didn't find matching input!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 1991, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(InputNo != S.getNumInputs() && "Didn't find matching input!"); |
| 1992 | |
| 1993 | QualType InputTy = S.getInputExpr(InputNo)->getType(); |
| 1994 | QualType OutputType = OutExpr->getType(); |
| 1995 | |
| 1996 | uint64_t InputSize = getContext().getTypeSize(InputTy); |
| 1997 | if (getContext().getTypeSize(OutputType) < InputSize) { |
| 1998 | |
| 1999 | ResultRegTypes.back() = ConvertType(InputTy); |
| 2000 | } |
| 2001 | } |
| 2002 | if (llvm::Type* AdjTy = |
| 2003 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
| 2004 | ResultRegTypes.back())) |
| 2005 | ResultRegTypes.back() = AdjTy; |
| 2006 | else { |
| 2007 | CGM.getDiags().Report(S.getAsmLoc(), |
| 2008 | diag::err_asm_invalid_type_in_input) |
| 2009 | << OutExpr->getType() << OutputConstraint; |
| 2010 | } |
| 2011 | |
| 2012 | |
| 2013 | if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back())) |
| 2014 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 2015 | VT->getPrimitiveSizeInBits()); |
| 2016 | } else { |
| 2017 | ArgTypes.push_back(Dest.getAddress().getType()); |
| 2018 | Args.push_back(Dest.getPointer()); |
| 2019 | Constraints += "=*"; |
| 2020 | Constraints += OutputConstraint; |
| 2021 | ReadOnly = ReadNone = false; |
| 2022 | } |
| 2023 | |
| 2024 | if (Info.isReadWrite()) { |
| 2025 | InOutConstraints += ','; |
| 2026 | |
| 2027 | const Expr *InputExpr = S.getOutputExpr(i); |
| 2028 | llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(), |
| 2029 | InOutConstraints, |
| 2030 | InputExpr->getExprLoc()); |
| 2031 | |
| 2032 | if (llvm::Type* AdjTy = |
| 2033 | getTargetHooks().adjustInlineAsmType(*this, OutputConstraint, |
| 2034 | Arg->getType())) |
| 2035 | Arg = Builder.CreateBitCast(Arg, AdjTy); |
| 2036 | |
| 2037 | |
| 2038 | if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType())) |
| 2039 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 2040 | VT->getPrimitiveSizeInBits()); |
| 2041 | if (Info.allowsRegister()) |
| 2042 | InOutConstraints += llvm::utostr(i); |
| 2043 | else |
| 2044 | InOutConstraints += OutputConstraint; |
| 2045 | |
| 2046 | InOutArgTypes.push_back(Arg->getType()); |
| 2047 | InOutArgs.push_back(Arg); |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | if (isa<MSAsmStmt>(&S)) { |
| 2054 | const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo(); |
| 2055 | if (RetAI.isDirect() || RetAI.isExtend()) { |
| 2056 | |
| 2057 | LValue ReturnSlot = MakeAddrLValue(ReturnValue, FnRetTy); |
| 2058 | CGM.getTargetCodeGenInfo().addReturnRegisterOutputs( |
| 2059 | *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes, |
| 2060 | ResultRegDests, AsmString, S.getNumOutputs()); |
| 2061 | SawAsmBlock = true; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | for (unsigned i = 0, e = S.getNumInputs(); i != e; i++) { |
| 2066 | const Expr *InputExpr = S.getInputExpr(i); |
| 2067 | |
| 2068 | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
| 2069 | |
| 2070 | if (Info.allowsMemory()) |
| 2071 | ReadNone = false; |
| 2072 | |
| 2073 | if (!Constraints.empty()) |
| 2074 | Constraints += ','; |
| 2075 | |
| 2076 | |
| 2077 | std::string InputConstraint(S.getInputConstraint(i)); |
| 2078 | InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(), |
| 2079 | &OutputConstraintInfos); |
| 2080 | |
| 2081 | InputConstraint = AddVariableConstraints( |
| 2082 | InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()), |
| 2083 | getTarget(), CGM, S, false ); |
| 2084 | |
| 2085 | std::string ReplaceConstraint (InputConstraint); |
| 2086 | llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints); |
| 2087 | |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | if (Info.hasTiedOperand()) { |
| 2094 | unsigned Output = Info.getTiedOperand(); |
| 2095 | QualType OutputType = S.getOutputExpr(Output)->getType(); |
| 2096 | QualType InputTy = InputExpr->getType(); |
| 2097 | |
| 2098 | if (getContext().getTypeSize(OutputType) > |
| 2099 | getContext().getTypeSize(InputTy)) { |
| 2100 | |
| 2101 | if (isa<llvm::PointerType>(Arg->getType())) |
| 2102 | Arg = Builder.CreatePtrToInt(Arg, IntPtrTy); |
| 2103 | llvm::Type *OutputTy = ConvertType(OutputType); |
| 2104 | if (isa<llvm::IntegerType>(OutputTy)) |
| 2105 | Arg = Builder.CreateZExt(Arg, OutputTy); |
| 2106 | else if (isa<llvm::PointerType>(OutputTy)) |
| 2107 | Arg = Builder.CreateZExt(Arg, IntPtrTy); |
| 2108 | else { |
| 2109 | (0) . __assert_fail ("OutputTy->isFloatingPointTy() && \"Unexpected output type\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 2109, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OutputTy->isFloatingPointTy() && "Unexpected output type"); |
| 2110 | Arg = Builder.CreateFPExt(Arg, OutputTy); |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | ReplaceConstraint = OutputConstraints[Output]; |
| 2115 | } |
| 2116 | if (llvm::Type* AdjTy = |
| 2117 | getTargetHooks().adjustInlineAsmType(*this, ReplaceConstraint, |
| 2118 | Arg->getType())) |
| 2119 | Arg = Builder.CreateBitCast(Arg, AdjTy); |
| 2120 | else |
| 2121 | CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input) |
| 2122 | << InputExpr->getType() << InputConstraint; |
| 2123 | |
| 2124 | |
| 2125 | if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType())) |
| 2126 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 2127 | VT->getPrimitiveSizeInBits()); |
| 2128 | |
| 2129 | ArgTypes.push_back(Arg->getType()); |
| 2130 | Args.push_back(Arg); |
| 2131 | Constraints += InputConstraint; |
| 2132 | } |
| 2133 | |
| 2134 | |
| 2135 | for (unsigned i = 0, e = InOutArgs.size(); i != e; i++) { |
| 2136 | ArgTypes.push_back(InOutArgTypes[i]); |
| 2137 | Args.push_back(InOutArgs[i]); |
| 2138 | } |
| 2139 | Constraints += InOutConstraints; |
| 2140 | |
| 2141 | |
| 2142 | for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) { |
| 2143 | StringRef Clobber = S.getClobber(i); |
| 2144 | |
| 2145 | if (Clobber == "memory") |
| 2146 | ReadOnly = ReadNone = false; |
| 2147 | else if (Clobber != "cc") |
| 2148 | Clobber = getTarget().getNormalizedGCCRegisterName(Clobber); |
| 2149 | |
| 2150 | if (!Constraints.empty()) |
| 2151 | Constraints += ','; |
| 2152 | |
| 2153 | Constraints += "~{"; |
| 2154 | Constraints += Clobber; |
| 2155 | Constraints += '}'; |
| 2156 | } |
| 2157 | |
| 2158 | |
| 2159 | std::string MachineClobbers = getTarget().getClobbers(); |
| 2160 | if (!MachineClobbers.empty()) { |
| 2161 | if (!Constraints.empty()) |
| 2162 | Constraints += ','; |
| 2163 | Constraints += MachineClobbers; |
| 2164 | } |
| 2165 | |
| 2166 | llvm::Type *ResultType; |
| 2167 | if (ResultRegTypes.empty()) |
| 2168 | ResultType = VoidTy; |
| 2169 | else if (ResultRegTypes.size() == 1) |
| 2170 | ResultType = ResultRegTypes[0]; |
| 2171 | else |
| 2172 | ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes); |
| 2173 | |
| 2174 | llvm::FunctionType *FTy = |
| 2175 | llvm::FunctionType::get(ResultType, ArgTypes, false); |
| 2176 | |
| 2177 | bool HasSideEffect = S.isVolatile() || S.getNumOutputs() == 0; |
| 2178 | llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ? |
| 2179 | llvm::InlineAsm::AD_Intel : llvm::InlineAsm::AD_ATT; |
| 2180 | llvm::InlineAsm *IA = |
| 2181 | llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect, |
| 2182 | false, AsmDialect); |
| 2183 | llvm::CallInst *Result = |
| 2184 | Builder.CreateCall(IA, Args, getBundlesForFunclet(IA)); |
| 2185 | Result->addAttribute(llvm::AttributeList::FunctionIndex, |
| 2186 | llvm::Attribute::NoUnwind); |
| 2187 | |
| 2188 | |
| 2189 | if (!HasSideEffect) { |
| 2190 | if (ReadNone) |
| 2191 | Result->addAttribute(llvm::AttributeList::FunctionIndex, |
| 2192 | llvm::Attribute::ReadNone); |
| 2193 | else if (ReadOnly) |
| 2194 | Result->addAttribute(llvm::AttributeList::FunctionIndex, |
| 2195 | llvm::Attribute::ReadOnly); |
| 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | |
| 2200 | if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) { |
| 2201 | Result->setMetadata("srcloc", getAsmSrcLocInfo(gccAsmStmt->getAsmString(), |
| 2202 | *this)); |
| 2203 | } else { |
| 2204 | |
| 2205 | auto Loc = llvm::ConstantInt::get(Int32Ty, S.getAsmLoc().getRawEncoding()); |
| 2206 | Result->setMetadata("srcloc", |
| 2207 | llvm::MDNode::get(getLLVMContext(), |
| 2208 | llvm::ConstantAsMetadata::get(Loc))); |
| 2209 | } |
| 2210 | |
| 2211 | if (getLangOpts().assumeFunctionsAreConvergent()) { |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | Result->addAttribute(llvm::AttributeList::FunctionIndex, |
| 2217 | llvm::Attribute::Convergent); |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | std::vector<llvm::Value*> RegResults; |
| 2222 | if (ResultRegTypes.size() == 1) { |
| 2223 | RegResults.push_back(Result); |
| 2224 | } else { |
| 2225 | for (unsigned i = 0, e = ResultRegTypes.size(); i != e; ++i) { |
| 2226 | llvm::Value *Tmp = Builder.CreateExtractValue(Result, i, "asmresult"); |
| 2227 | RegResults.push_back(Tmp); |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | assert(RegResults.size() == ResultRegTypes.size()); |
| 2232 | assert(RegResults.size() == ResultTruncRegTypes.size()); |
| 2233 | assert(RegResults.size() == ResultRegDests.size()); |
| 2234 | for (unsigned i = 0, e = RegResults.size(); i != e; ++i) { |
| 2235 | llvm::Value *Tmp = RegResults[i]; |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | if (ResultRegTypes[i] != ResultTruncRegTypes[i]) { |
| 2240 | llvm::Type *TruncTy = ResultTruncRegTypes[i]; |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | if (TruncTy->isFloatingPointTy()) |
| 2245 | Tmp = Builder.CreateFPTrunc(Tmp, TruncTy); |
| 2246 | else if (TruncTy->isPointerTy() && Tmp->getType()->isIntegerTy()) { |
| 2247 | uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy); |
| 2248 | Tmp = Builder.CreateTrunc(Tmp, |
| 2249 | llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize)); |
| 2250 | Tmp = Builder.CreateIntToPtr(Tmp, TruncTy); |
| 2251 | } else if (Tmp->getType()->isPointerTy() && TruncTy->isIntegerTy()) { |
| 2252 | uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType()); |
| 2253 | Tmp = Builder.CreatePtrToInt(Tmp, |
| 2254 | llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize)); |
| 2255 | Tmp = Builder.CreateTrunc(Tmp, TruncTy); |
| 2256 | } else if (TruncTy->isIntegerTy()) { |
| 2257 | Tmp = Builder.CreateZExtOrTrunc(Tmp, TruncTy); |
| 2258 | } else if (TruncTy->isVectorTy()) { |
| 2259 | Tmp = Builder.CreateBitCast(Tmp, TruncTy); |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]); |
| 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) { |
| 2268 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 2269 | QualType RecordTy = getContext().getRecordType(RD); |
| 2270 | |
| 2271 | |
| 2272 | LValue SlotLV = |
| 2273 | MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy); |
| 2274 | |
| 2275 | RecordDecl::field_iterator CurField = RD->field_begin(); |
| 2276 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 2277 | E = S.capture_init_end(); |
| 2278 | I != E; ++I, ++CurField) { |
| 2279 | LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField); |
| 2280 | if (CurField->hasCapturedVLAType()) { |
| 2281 | auto VAT = CurField->getCapturedVLAType(); |
| 2282 | EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV); |
| 2283 | } else { |
| 2284 | EmitInitializerForField(*CurField, LV, *I); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | return SlotLV; |
| 2289 | } |
| 2290 | |
| 2291 | |
| 2292 | |
| 2293 | llvm::Function * |
| 2294 | CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) { |
| 2295 | LValue CapStruct = InitCapturedStruct(S); |
| 2296 | |
| 2297 | |
| 2298 | CodeGenFunction CGF(CGM, true); |
| 2299 | CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K)); |
| 2300 | llvm::Function *F = CGF.GenerateCapturedStmtFunction(S); |
| 2301 | delete CGF.CapturedStmtInfo; |
| 2302 | |
| 2303 | |
| 2304 | EmitCallOrInvoke(F, CapStruct.getPointer()); |
| 2305 | |
| 2306 | return F; |
| 2307 | } |
| 2308 | |
| 2309 | Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) { |
| 2310 | LValue CapStruct = InitCapturedStruct(S); |
| 2311 | return CapStruct.getAddress(); |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | llvm::Function * |
| 2316 | CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) { |
| 2317 | (0) . __assert_fail ("CapturedStmtInfo && \"CapturedStmtInfo should be set when generating the captured function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 2318, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CapturedStmtInfo && |
| 2318 | (0) . __assert_fail ("CapturedStmtInfo && \"CapturedStmtInfo should be set when generating the captured function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 2318, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "CapturedStmtInfo should be set when generating the captured function"); |
| 2319 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 2320 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 2321 | SourceLocation Loc = S.getBeginLoc(); |
| 2322 | (0) . __assert_fail ("CD->hasBody() && \"missing CapturedDecl body\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CGStmt.cpp", 2322, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CD->hasBody() && "missing CapturedDecl body"); |
| 2323 | |
| 2324 | |
| 2325 | ASTContext &Ctx = CGM.getContext(); |
| 2326 | FunctionArgList Args; |
| 2327 | Args.append(CD->param_begin(), CD->param_end()); |
| 2328 | |
| 2329 | |
| 2330 | const CGFunctionInfo &FuncInfo = |
| 2331 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args); |
| 2332 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 2333 | |
| 2334 | llvm::Function *F = |
| 2335 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 2336 | CapturedStmtInfo->getHelperName(), &CGM.getModule()); |
| 2337 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 2338 | if (CD->isNothrow()) |
| 2339 | F->addFnAttr(llvm::Attribute::NoUnwind); |
| 2340 | |
| 2341 | |
| 2342 | StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(), |
| 2343 | CD->getBody()->getBeginLoc()); |
| 2344 | |
| 2345 | Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam()); |
| 2346 | CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr)); |
| 2347 | |
| 2348 | |
| 2349 | LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(), |
| 2350 | Ctx.getTagDeclType(RD)); |
| 2351 | for (auto *FD : RD->fields()) { |
| 2352 | if (FD->hasCapturedVLAType()) { |
| 2353 | auto *ExprArg = |
| 2354 | EmitLoadOfLValue(EmitLValueForField(Base, FD), S.getBeginLoc()) |
| 2355 | .getScalarVal(); |
| 2356 | auto VAT = FD->getCapturedVLAType(); |
| 2357 | VLASizeMap[VAT->getSizeExpr()] = ExprArg; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | if (CapturedStmtInfo->isCXXThisExprCaptured()) { |
| 2363 | FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl(); |
| 2364 | LValue ThisLValue = EmitLValueForField(Base, FD); |
| 2365 | CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal(); |
| 2366 | } |
| 2367 | |
| 2368 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
| 2369 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 2370 | FinishFunction(CD->getBodyRBrace()); |
| 2371 | |
| 2372 | return F; |
| 2373 | } |
| 2374 | |