| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "CodeGenFunction.h" |
| 14 | #include "CGBlocks.h" |
| 15 | #include "CGCleanup.h" |
| 16 | #include "CGCUDARuntime.h" |
| 17 | #include "CGCXXABI.h" |
| 18 | #include "CGDebugInfo.h" |
| 19 | #include "CGOpenMPRuntime.h" |
| 20 | #include "CodeGenModule.h" |
| 21 | #include "CodeGenPGO.h" |
| 22 | #include "TargetInfo.h" |
| 23 | #include "clang/AST/ASTContext.h" |
| 24 | #include "clang/AST/ASTLambda.h" |
| 25 | #include "clang/AST/Decl.h" |
| 26 | #include "clang/AST/DeclCXX.h" |
| 27 | #include "clang/AST/StmtCXX.h" |
| 28 | #include "clang/AST/StmtObjC.h" |
| 29 | #include "clang/Basic/Builtins.h" |
| 30 | #include "clang/Basic/CodeGenOptions.h" |
| 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/CodeGen/CGFunctionInfo.h" |
| 33 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 34 | #include "llvm/IR/DataLayout.h" |
| 35 | #include "llvm/IR/Dominators.h" |
| 36 | #include "llvm/IR/Intrinsics.h" |
| 37 | #include "llvm/IR/MDBuilder.h" |
| 38 | #include "llvm/IR/Operator.h" |
| 39 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 40 | using namespace clang; |
| 41 | using namespace CodeGen; |
| 42 | |
| 43 | |
| 44 | |
| 45 | static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts, |
| 46 | const LangOptions &LangOpts) { |
| 47 | if (CGOpts.DisableLifetimeMarkers) |
| 48 | return false; |
| 49 | |
| 50 | |
| 51 | |
| 52 | if (LangOpts.Sanitize.has(SanitizerKind::Memory)) |
| 53 | return false; |
| 54 | |
| 55 | |
| 56 | if (CGOpts.SanitizeAddressUseAfterScope) |
| 57 | return true; |
| 58 | |
| 59 | |
| 60 | return CGOpts.OptimizationLevel != 0; |
| 61 | } |
| 62 | |
| 63 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext) |
| 64 | : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()), |
| 65 | Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(), |
| 66 | CGBuilderInserterTy(this)), |
| 67 | SanOpts(CGM.getLangOpts().Sanitize), DebugInfo(CGM.getModuleDebugInfo()), |
| 68 | PGO(cgm), ShouldEmitLifetimeMarkers(shouldEmitLifetimeMarkers( |
| 69 | CGM.getCodeGenOpts(), CGM.getLangOpts())) { |
| 70 | if (!suppressNewContext) |
| 71 | CGM.getCXXABI().getMangleContext().startNewFunction(); |
| 72 | |
| 73 | llvm::FastMathFlags FMF; |
| 74 | if (CGM.getLangOpts().FastMath) |
| 75 | FMF.setFast(); |
| 76 | if (CGM.getLangOpts().FiniteMathOnly) { |
| 77 | FMF.setNoNaNs(); |
| 78 | FMF.setNoInfs(); |
| 79 | } |
| 80 | if (CGM.getCodeGenOpts().NoNaNsFPMath) { |
| 81 | FMF.setNoNaNs(); |
| 82 | } |
| 83 | if (CGM.getCodeGenOpts().NoSignedZeros) { |
| 84 | FMF.setNoSignedZeros(); |
| 85 | } |
| 86 | if (CGM.getCodeGenOpts().ReciprocalMath) { |
| 87 | FMF.setAllowReciprocal(); |
| 88 | } |
| 89 | if (CGM.getCodeGenOpts().Reassociate) { |
| 90 | FMF.setAllowReassoc(); |
| 91 | } |
| 92 | Builder.setFastMathFlags(FMF); |
| 93 | } |
| 94 | |
| 95 | CodeGenFunction::~CodeGenFunction() { |
| 96 | (0) . __assert_fail ("LifetimeExtendedCleanupStack.empty() && \"failed to emit a cleanup\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 96, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup"); |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | if (FirstBlockInfo) |
| 102 | destroyBlockInfos(FirstBlockInfo); |
| 103 | |
| 104 | if (getLangOpts().OpenMP && CurFn) |
| 105 | CGM.getOpenMPRuntime().functionFinished(*this); |
| 106 | } |
| 107 | |
| 108 | CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T, |
| 109 | LValueBaseInfo *BaseInfo, |
| 110 | TBAAAccessInfo *TBAAInfo) { |
| 111 | return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo, |
| 112 | true); |
| 113 | } |
| 114 | |
| 115 | CharUnits CodeGenFunction::getNaturalTypeAlignment(QualType T, |
| 116 | LValueBaseInfo *BaseInfo, |
| 117 | TBAAAccessInfo *TBAAInfo, |
| 118 | bool forPointeeType) { |
| 119 | if (TBAAInfo) |
| 120 | *TBAAInfo = CGM.getTBAAAccessInfo(T); |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | if (auto TT = T->getAs<TypedefType>()) { |
| 126 | if (auto Align = TT->getDecl()->getMaxAlignment()) { |
| 127 | if (BaseInfo) |
| 128 | *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType); |
| 129 | return getContext().toCharUnitsFromBits(Align); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (BaseInfo) |
| 134 | *BaseInfo = LValueBaseInfo(AlignmentSource::Type); |
| 135 | |
| 136 | CharUnits Alignment; |
| 137 | if (T->isIncompleteType()) { |
| 138 | Alignment = CharUnits::One(); |
| 139 | } else { |
| 140 | |
| 141 | |
| 142 | |
| 143 | const CXXRecordDecl *RD; |
| 144 | if (forPointeeType && (RD = T->getAsCXXRecordDecl())) { |
| 145 | Alignment = CGM.getClassPointerAlignment(RD); |
| 146 | } else { |
| 147 | Alignment = getContext().getTypeAlignInChars(T); |
| 148 | if (T.getQualifiers().hasUnaligned()) |
| 149 | Alignment = CharUnits::One(); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) { |
| 155 | if (Alignment.getQuantity() > MaxAlign && |
| 156 | !getContext().isAlignmentRequired(T)) |
| 157 | Alignment = CharUnits::fromQuantity(MaxAlign); |
| 158 | } |
| 159 | } |
| 160 | return Alignment; |
| 161 | } |
| 162 | |
| 163 | LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { |
| 164 | LValueBaseInfo BaseInfo; |
| 165 | TBAAAccessInfo TBAAInfo; |
| 166 | CharUnits Alignment = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo); |
| 167 | return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo, |
| 168 | TBAAInfo); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | LValue |
| 174 | CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) { |
| 175 | LValueBaseInfo BaseInfo; |
| 176 | TBAAAccessInfo TBAAInfo; |
| 177 | CharUnits Align = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo, |
| 178 | true); |
| 179 | return MakeAddrLValue(Address(V, Align), T, BaseInfo, TBAAInfo); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
| 184 | return CGM.getTypes().ConvertTypeForMem(T); |
| 185 | } |
| 186 | |
| 187 | llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 188 | return CGM.getTypes().ConvertType(T); |
| 189 | } |
| 190 | |
| 191 | TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) { |
| 192 | type = type.getCanonicalType(); |
| 193 | while (true) { |
| 194 | switch (type->getTypeClass()) { |
| 195 | #define TYPE(name, parent) |
| 196 | #define ABSTRACT_TYPE(name, parent) |
| 197 | #define NON_CANONICAL_TYPE(name, parent) case Type::name: |
| 198 | #define DEPENDENT_TYPE(name, parent) case Type::name: |
| 199 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name: |
| 200 | #include "clang/AST/TypeNodes.def" |
| 201 | llvm_unreachable("non-canonical or dependent type in IR-generation"); |
| 202 | |
| 203 | case Type::Auto: |
| 204 | case Type::DeducedTemplateSpecialization: |
| 205 | llvm_unreachable("undeduced type in IR-generation"); |
| 206 | |
| 207 | |
| 208 | case Type::Builtin: |
| 209 | case Type::Pointer: |
| 210 | case Type::BlockPointer: |
| 211 | case Type::LValueReference: |
| 212 | case Type::RValueReference: |
| 213 | case Type::MemberPointer: |
| 214 | case Type::Vector: |
| 215 | case Type::ExtVector: |
| 216 | case Type::FunctionProto: |
| 217 | case Type::FunctionNoProto: |
| 218 | case Type::Enum: |
| 219 | case Type::ObjCObjectPointer: |
| 220 | case Type::Pipe: |
| 221 | return TEK_Scalar; |
| 222 | |
| 223 | |
| 224 | case Type::Complex: |
| 225 | return TEK_Complex; |
| 226 | |
| 227 | |
| 228 | case Type::ConstantArray: |
| 229 | case Type::IncompleteArray: |
| 230 | case Type::VariableArray: |
| 231 | case Type::Record: |
| 232 | case Type::ObjCObject: |
| 233 | case Type::ObjCInterface: |
| 234 | return TEK_Aggregate; |
| 235 | |
| 236 | |
| 237 | case Type::Atomic: |
| 238 | type = cast<AtomicType>(type)->getValueType(); |
| 239 | continue; |
| 240 | } |
| 241 | llvm_unreachable("unknown type kind!"); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | llvm::DebugLoc CodeGenFunction::EmitReturnBlock() { |
| 246 | |
| 247 | |
| 248 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 249 | |
| 250 | if (CurBB) { |
| 251 | (0) . __assert_fail ("!CurBB->getTerminator() && \"Unexpected terminated block.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 251, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 252 | |
| 253 | |
| 254 | |
| 255 | if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { |
| 256 | ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); |
| 257 | delete ReturnBlock.getBlock(); |
| 258 | } else |
| 259 | EmitBlock(ReturnBlock.getBlock()); |
| 260 | return llvm::DebugLoc(); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | if (ReturnBlock.getBlock()->hasOneUse()) { |
| 267 | llvm::BranchInst *BI = |
| 268 | dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->user_begin()); |
| 269 | if (BI && BI->isUnconditional() && |
| 270 | BI->getSuccessor(0) == ReturnBlock.getBlock()) { |
| 271 | |
| 272 | |
| 273 | llvm::DebugLoc Loc = BI->getDebugLoc(); |
| 274 | Builder.SetInsertPoint(BI->getParent()); |
| 275 | BI->eraseFromParent(); |
| 276 | delete ReturnBlock.getBlock(); |
| 277 | return Loc; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | EmitBlock(ReturnBlock.getBlock()); |
| 286 | return llvm::DebugLoc(); |
| 287 | } |
| 288 | |
| 289 | static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { |
| 290 | if (!BB) return; |
| 291 | if (!BB->use_empty()) |
| 292 | return CGF.CurFn->getBasicBlockList().push_back(BB); |
| 293 | delete BB; |
| 294 | } |
| 295 | |
| 296 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
| 297 | (0) . __assert_fail ("BreakContinueStack.empty() && \"mismatched push/pop in break/continue stack!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 298, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BreakContinueStack.empty() && |
| 298 | (0) . __assert_fail ("BreakContinueStack.empty() && \"mismatched push/pop in break/continue stack!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 298, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "mismatched push/pop in break/continue stack!"); |
| 299 | |
| 300 | bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0 |
| 301 | && NumSimpleReturnExprs == NumReturnExprs |
| 302 | && ReturnBlock.getBlock()->use_empty(); |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 316 | if (OnlySimpleReturnStmts) |
| 317 | DI->EmitLocation(Builder, LastStopPoint); |
| 318 | else |
| 319 | DI->EmitLocation(Builder, EndLoc); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth; |
| 327 | bool HasOnlyLifetimeMarkers = |
| 328 | HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth); |
| 329 | bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers; |
| 330 | if (HasCleanups) { |
| 331 | |
| 332 | |
| 333 | if (CGDebugInfo *DI = getDebugInfo()) |
| 334 | if (OnlySimpleReturnStmts) |
| 335 | DI->EmitLocation(Builder, EndLoc); |
| 336 | |
| 337 | PopCleanupBlocks(PrologueCleanupDepth); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | llvm::DebugLoc Loc = EmitReturnBlock(); |
| 342 | |
| 343 | if (ShouldInstrumentFunction()) { |
| 344 | if (CGM.getCodeGenOpts().InstrumentFunctions) |
| 345 | CurFn->addFnAttr("instrument-function-exit", "__cyg_profile_func_exit"); |
| 346 | if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) |
| 347 | CurFn->addFnAttr("instrument-function-exit-inlined", |
| 348 | "__cyg_profile_func_exit"); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | if (CGDebugInfo *DI = getDebugInfo()) |
| 353 | DI->EmitFunctionEnd(Builder, CurFn); |
| 354 | |
| 355 | |
| 356 | |
| 357 | ApplyDebugLocation AL(*this, Loc); |
| 358 | EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc); |
| 359 | EmitEndEHSpec(CurCodeDecl); |
| 360 | |
| 361 | (0) . __assert_fail ("EHStack.empty() && \"did not remove all scopes from cleanup stack!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 362, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EHStack.empty() && |
| 362 | (0) . __assert_fail ("EHStack.empty() && \"did not remove all scopes from cleanup stack!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 362, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "did not remove all scopes from cleanup stack!"); |
| 363 | |
| 364 | |
| 365 | |
| 366 | if (IndirectBranch) { |
| 367 | EmitBlock(IndirectBranch->getParent()); |
| 368 | Builder.ClearInsertionPoint(); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | if (!EscapedLocals.empty()) { |
| 374 | |
| 375 | |
| 376 | SmallVector<llvm::Value *, 4> EscapeArgs; |
| 377 | EscapeArgs.resize(EscapedLocals.size()); |
| 378 | for (auto &Pair : EscapedLocals) |
| 379 | EscapeArgs[Pair.second] = Pair.first; |
| 380 | llvm::Function *FrameEscapeFn = llvm::Intrinsic::getDeclaration( |
| 381 | &CGM.getModule(), llvm::Intrinsic::localescape); |
| 382 | CGBuilderTy(*this, AllocaInsertPt).CreateCall(FrameEscapeFn, EscapeArgs); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | llvm::Instruction *Ptr = AllocaInsertPt; |
| 387 | AllocaInsertPt = nullptr; |
| 388 | Ptr->eraseFromParent(); |
| 389 | |
| 390 | |
| 391 | |
| 392 | if (IndirectBranch) { |
| 393 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 394 | if (PN->getNumIncomingValues() == 0) { |
| 395 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 396 | PN->eraseFromParent(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | EmitIfUsed(*this, EHResumeBlock); |
| 401 | EmitIfUsed(*this, TerminateLandingPad); |
| 402 | EmitIfUsed(*this, TerminateHandler); |
| 403 | EmitIfUsed(*this, UnreachableBlock); |
| 404 | |
| 405 | for (const auto &FuncletAndParent : TerminateFunclets) |
| 406 | EmitIfUsed(*this, FuncletAndParent.second); |
| 407 | |
| 408 | if (CGM.getCodeGenOpts().EmitDeclMetadata) |
| 409 | EmitDeclMetadata(); |
| 410 | |
| 411 | for (SmallVectorImpl<std::pair<llvm::Instruction *, llvm::Value *> >::iterator |
| 412 | I = DeferredReplacements.begin(), |
| 413 | E = DeferredReplacements.end(); |
| 414 | I != E; ++I) { |
| 415 | I->first->replaceAllUsesWith(I->second); |
| 416 | I->first->eraseFromParent(); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | if (NormalCleanupDest.isValid() && isCoroutine()) { |
| 426 | llvm::DominatorTree DT(*CurFn); |
| 427 | llvm::PromoteMemToReg( |
| 428 | cast<llvm::AllocaInst>(NormalCleanupDest.getPointer()), DT); |
| 429 | NormalCleanupDest = Address::invalid(); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | for (llvm::Argument &A : CurFn->args()) |
| 434 | if (auto *VT = dyn_cast<llvm::VectorType>(A.getType())) |
| 435 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 436 | VT->getPrimitiveSizeInBits()); |
| 437 | |
| 438 | |
| 439 | if (auto *VT = dyn_cast<llvm::VectorType>(CurFn->getReturnType())) |
| 440 | LargestVectorWidth = std::max(LargestVectorWidth, |
| 441 | VT->getPrimitiveSizeInBits()); |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | CurFn->addFnAttr("min-legal-vector-width", llvm::utostr(LargestVectorWidth)); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | |
| 455 | bool CodeGenFunction::ShouldInstrumentFunction() { |
| 456 | if (!CGM.getCodeGenOpts().InstrumentFunctions && |
| 457 | !CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining && |
| 458 | !CGM.getCodeGenOpts().InstrumentFunctionEntryBare) |
| 459 | return false; |
| 460 | if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) |
| 461 | return false; |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | |
| 467 | bool CodeGenFunction::ShouldXRayInstrumentFunction() const { |
| 468 | return CGM.getCodeGenOpts().XRayInstrumentFunctions; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | |
| 473 | bool CodeGenFunction::AlwaysEmitXRayCustomEvents() const { |
| 474 | return CGM.getCodeGenOpts().XRayInstrumentFunctions && |
| 475 | (CGM.getCodeGenOpts().XRayAlwaysEmitCustomEvents || |
| 476 | CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask == |
| 477 | XRayInstrKind::Custom); |
| 478 | } |
| 479 | |
| 480 | bool CodeGenFunction::AlwaysEmitXRayTypedEvents() const { |
| 481 | return CGM.getCodeGenOpts().XRayInstrumentFunctions && |
| 482 | (CGM.getCodeGenOpts().XRayAlwaysEmitTypedEvents || |
| 483 | CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask == |
| 484 | XRayInstrKind::Typed); |
| 485 | } |
| 486 | |
| 487 | llvm::Constant * |
| 488 | CodeGenFunction::EncodeAddrForUseInPrologue(llvm::Function *F, |
| 489 | llvm::Constant *Addr) { |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | auto *GV = new llvm::GlobalVariable(CGM.getModule(), Addr->getType(), |
| 498 | , |
| 499 | llvm::GlobalValue::PrivateLinkage, Addr); |
| 500 | |
| 501 | |
| 502 | auto *GOTAsInt = llvm::ConstantExpr::getPtrToInt(GV, IntPtrTy); |
| 503 | auto *FuncAsInt = llvm::ConstantExpr::getPtrToInt(F, IntPtrTy); |
| 504 | auto *PCRelAsInt = llvm::ConstantExpr::getSub(GOTAsInt, FuncAsInt); |
| 505 | return (IntPtrTy == Int32Ty) |
| 506 | ? PCRelAsInt |
| 507 | : llvm::ConstantExpr::getTrunc(PCRelAsInt, Int32Ty); |
| 508 | } |
| 509 | |
| 510 | llvm::Value * |
| 511 | CodeGenFunction::DecodeAddrUsedInPrologue(llvm::Value *F, |
| 512 | llvm::Value *EncodedAddr) { |
| 513 | |
| 514 | auto *PCRelAsInt = Builder.CreateSExt(EncodedAddr, IntPtrTy); |
| 515 | auto *FuncAsInt = Builder.CreatePtrToInt(F, IntPtrTy, "func_addr.int"); |
| 516 | auto *GOTAsInt = Builder.CreateAdd(PCRelAsInt, FuncAsInt, "global_addr.int"); |
| 517 | auto *GOTAddr = Builder.CreateIntToPtr(GOTAsInt, Int8PtrPtrTy, "global_addr"); |
| 518 | |
| 519 | |
| 520 | return Builder.CreateLoad(Address(GOTAddr, getPointerAlign()), |
| 521 | "decoded_addr"); |
| 522 | } |
| 523 | |
| 524 | static void removeImageAccessQualifier(std::string& TyName) { |
| 525 | std::string ReadOnlyQual("__read_only"); |
| 526 | std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual); |
| 527 | if (ReadOnlyPos != std::string::npos) |
| 528 | |
| 529 | TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1); |
| 530 | else { |
| 531 | std::string WriteOnlyQual("__write_only"); |
| 532 | std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual); |
| 533 | if (WriteOnlyPos != std::string::npos) |
| 534 | TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1); |
| 535 | else { |
| 536 | std::string ReadWriteQual("__read_write"); |
| 537 | std::string::size_type ReadWritePos = TyName.find(ReadWriteQual); |
| 538 | if (ReadWritePos != std::string::npos) |
| 539 | TyName.erase(ReadWritePos, ReadWriteQual.size() + 1); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | static unsigned ArgInfoAddressSpace(LangAS AS) { |
| 551 | switch (AS) { |
| 552 | case LangAS::opencl_global: return 1; |
| 553 | case LangAS::opencl_constant: return 2; |
| 554 | case LangAS::opencl_local: return 3; |
| 555 | case LangAS::opencl_generic: return 4; |
| 556 | default: |
| 557 | return 0; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn, |
| 565 | CodeGenModule &CGM, llvm::LLVMContext &Context, |
| 566 | CGBuilderTy &Builder, ASTContext &ASTCtx) { |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | const PrintingPolicy &Policy = ASTCtx.getPrintingPolicy(); |
| 572 | |
| 573 | |
| 574 | SmallVector<llvm::Metadata *, 8> addressQuals; |
| 575 | |
| 576 | |
| 577 | SmallVector<llvm::Metadata *, 8> accessQuals; |
| 578 | |
| 579 | |
| 580 | SmallVector<llvm::Metadata *, 8> argTypeNames; |
| 581 | |
| 582 | |
| 583 | SmallVector<llvm::Metadata *, 8> argBaseTypeNames; |
| 584 | |
| 585 | |
| 586 | SmallVector<llvm::Metadata *, 8> argTypeQuals; |
| 587 | |
| 588 | |
| 589 | SmallVector<llvm::Metadata *, 8> argNames; |
| 590 | |
| 591 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 592 | const ParmVarDecl *parm = FD->getParamDecl(i); |
| 593 | QualType ty = parm->getType(); |
| 594 | std::string typeQuals; |
| 595 | |
| 596 | if (ty->isPointerType()) { |
| 597 | QualType pointeeTy = ty->getPointeeType(); |
| 598 | |
| 599 | |
| 600 | addressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32( |
| 601 | ArgInfoAddressSpace(pointeeTy.getAddressSpace())))); |
| 602 | |
| 603 | |
| 604 | std::string typeName = |
| 605 | pointeeTy.getUnqualifiedType().getAsString(Policy) + "*"; |
| 606 | |
| 607 | |
| 608 | std::string::size_type pos = typeName.find("unsigned"); |
| 609 | if (pointeeTy.isCanonical() && pos != std::string::npos) |
| 610 | typeName.erase(pos+1, 8); |
| 611 | |
| 612 | argTypeNames.push_back(llvm::MDString::get(Context, typeName)); |
| 613 | |
| 614 | std::string baseTypeName = |
| 615 | pointeeTy.getUnqualifiedType().getCanonicalType().getAsString( |
| 616 | Policy) + |
| 617 | "*"; |
| 618 | |
| 619 | |
| 620 | pos = baseTypeName.find("unsigned"); |
| 621 | if (pos != std::string::npos) |
| 622 | baseTypeName.erase(pos+1, 8); |
| 623 | |
| 624 | argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName)); |
| 625 | |
| 626 | |
| 627 | if (ty.isRestrictQualified()) |
| 628 | typeQuals = "restrict"; |
| 629 | if (pointeeTy.isConstQualified() || |
| 630 | (pointeeTy.getAddressSpace() == LangAS::opencl_constant)) |
| 631 | typeQuals += typeQuals.empty() ? "const" : " const"; |
| 632 | if (pointeeTy.isVolatileQualified()) |
| 633 | typeQuals += typeQuals.empty() ? "volatile" : " volatile"; |
| 634 | } else { |
| 635 | uint32_t AddrSpc = 0; |
| 636 | bool isPipe = ty->isPipeType(); |
| 637 | if (ty->isImageType() || isPipe) |
| 638 | AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global); |
| 639 | |
| 640 | addressQuals.push_back( |
| 641 | llvm::ConstantAsMetadata::get(Builder.getInt32(AddrSpc))); |
| 642 | |
| 643 | |
| 644 | std::string typeName; |
| 645 | if (isPipe) |
| 646 | typeName = ty.getCanonicalType()->getAs<PipeType>()->getElementType() |
| 647 | .getAsString(Policy); |
| 648 | else |
| 649 | typeName = ty.getUnqualifiedType().getAsString(Policy); |
| 650 | |
| 651 | |
| 652 | std::string::size_type pos = typeName.find("unsigned"); |
| 653 | if (ty.isCanonical() && pos != std::string::npos) |
| 654 | typeName.erase(pos+1, 8); |
| 655 | |
| 656 | std::string baseTypeName; |
| 657 | if (isPipe) |
| 658 | baseTypeName = ty.getCanonicalType()->getAs<PipeType>() |
| 659 | ->getElementType().getCanonicalType() |
| 660 | .getAsString(Policy); |
| 661 | else |
| 662 | baseTypeName = |
| 663 | ty.getUnqualifiedType().getCanonicalType().getAsString(Policy); |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | if (ty->isImageType()) { |
| 670 | removeImageAccessQualifier(typeName); |
| 671 | removeImageAccessQualifier(baseTypeName); |
| 672 | } |
| 673 | |
| 674 | argTypeNames.push_back(llvm::MDString::get(Context, typeName)); |
| 675 | |
| 676 | |
| 677 | pos = baseTypeName.find("unsigned"); |
| 678 | if (pos != std::string::npos) |
| 679 | baseTypeName.erase(pos+1, 8); |
| 680 | |
| 681 | argBaseTypeNames.push_back(llvm::MDString::get(Context, baseTypeName)); |
| 682 | |
| 683 | if (isPipe) |
| 684 | typeQuals = "pipe"; |
| 685 | } |
| 686 | |
| 687 | argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals)); |
| 688 | |
| 689 | |
| 690 | if (ty->isImageType()|| ty->isPipeType()) { |
| 691 | const Decl *PDecl = parm; |
| 692 | if (auto *TD = dyn_cast<TypedefType>(ty)) |
| 693 | PDecl = TD->getDecl(); |
| 694 | const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>(); |
| 695 | if (A && A->isWriteOnly()) |
| 696 | accessQuals.push_back(llvm::MDString::get(Context, "write_only")); |
| 697 | else if (A && A->isReadWrite()) |
| 698 | accessQuals.push_back(llvm::MDString::get(Context, "read_write")); |
| 699 | else |
| 700 | accessQuals.push_back(llvm::MDString::get(Context, "read_only")); |
| 701 | } else |
| 702 | accessQuals.push_back(llvm::MDString::get(Context, "none")); |
| 703 | |
| 704 | |
| 705 | argNames.push_back(llvm::MDString::get(Context, parm->getName())); |
| 706 | } |
| 707 | |
| 708 | Fn->setMetadata("kernel_arg_addr_space", |
| 709 | llvm::MDNode::get(Context, addressQuals)); |
| 710 | Fn->setMetadata("kernel_arg_access_qual", |
| 711 | llvm::MDNode::get(Context, accessQuals)); |
| 712 | Fn->setMetadata("kernel_arg_type", |
| 713 | llvm::MDNode::get(Context, argTypeNames)); |
| 714 | Fn->setMetadata("kernel_arg_base_type", |
| 715 | llvm::MDNode::get(Context, argBaseTypeNames)); |
| 716 | Fn->setMetadata("kernel_arg_type_qual", |
| 717 | llvm::MDNode::get(Context, argTypeQuals)); |
| 718 | if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 719 | Fn->setMetadata("kernel_arg_name", |
| 720 | llvm::MDNode::get(Context, argNames)); |
| 721 | } |
| 722 | |
| 723 | void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD, |
| 724 | llvm::Function *Fn) |
| 725 | { |
| 726 | if (!FD->hasAttr<OpenCLKernelAttr>()) |
| 727 | return; |
| 728 | |
| 729 | llvm::LLVMContext &Context = getLLVMContext(); |
| 730 | |
| 731 | GenOpenCLArgMetadata(FD, Fn, CGM, Context, Builder, getContext()); |
| 732 | |
| 733 | if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) { |
| 734 | QualType HintQTy = A->getTypeHint(); |
| 735 | const ExtVectorType *HintEltQTy = HintQTy->getAs<ExtVectorType>(); |
| 736 | bool IsSignedInteger = |
| 737 | HintQTy->isSignedIntegerType() || |
| 738 | (HintEltQTy && HintEltQTy->getElementType()->isSignedIntegerType()); |
| 739 | llvm::Metadata *AttrMDArgs[] = { |
| 740 | llvm::ConstantAsMetadata::get(llvm::UndefValue::get( |
| 741 | CGM.getTypes().ConvertType(A->getTypeHint()))), |
| 742 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 743 | llvm::IntegerType::get(Context, 32), |
| 744 | llvm::APInt(32, (uint64_t)(IsSignedInteger ? 1 : 0))))}; |
| 745 | Fn->setMetadata("vec_type_hint", llvm::MDNode::get(Context, AttrMDArgs)); |
| 746 | } |
| 747 | |
| 748 | if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) { |
| 749 | llvm::Metadata *AttrMDArgs[] = { |
| 750 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())), |
| 751 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())), |
| 752 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))}; |
| 753 | Fn->setMetadata("work_group_size_hint", llvm::MDNode::get(Context, AttrMDArgs)); |
| 754 | } |
| 755 | |
| 756 | if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 757 | llvm::Metadata *AttrMDArgs[] = { |
| 758 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())), |
| 759 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())), |
| 760 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))}; |
| 761 | Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, AttrMDArgs)); |
| 762 | } |
| 763 | |
| 764 | if (const OpenCLIntelReqdSubGroupSizeAttr *A = |
| 765 | FD->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
| 766 | llvm::Metadata *AttrMDArgs[] = { |
| 767 | llvm::ConstantAsMetadata::get(Builder.getInt32(A->getSubGroupSize()))}; |
| 768 | Fn->setMetadata("intel_reqd_sub_group_size", |
| 769 | llvm::MDNode::get(Context, AttrMDArgs)); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | |
| 774 | static bool endsWithReturn(const Decl* F) { |
| 775 | const Stmt *Body = nullptr; |
| 776 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(F)) |
| 777 | Body = FD->getBody(); |
| 778 | else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F)) |
| 779 | Body = OMD->getBody(); |
| 780 | |
| 781 | if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) { |
| 782 | auto LastStmt = CS->body_rbegin(); |
| 783 | if (LastStmt != CS->body_rend()) |
| 784 | return isa<ReturnStmt>(*LastStmt); |
| 785 | } |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | void CodeGenFunction::markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn) { |
| 790 | if (SanOpts.has(SanitizerKind::Thread)) { |
| 791 | Fn->addFnAttr("sanitize_thread_no_checking_at_run_time"); |
| 792 | Fn->removeFnAttr(llvm::Attribute::SanitizeThread); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) { |
| 797 | auto *MD = dyn_cast_or_null<CXXMethodDecl>(D); |
| 798 | if (!MD || !MD->getDeclName().getAsIdentifierInfo() || |
| 799 | !MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") || |
| 800 | (MD->getNumParams() != 1 && MD->getNumParams() != 2)) |
| 801 | return false; |
| 802 | |
| 803 | if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getSizeType()) |
| 804 | return false; |
| 805 | |
| 806 | if (MD->getNumParams() == 2) { |
| 807 | auto *PT = MD->parameters()[1]->getType()->getAs<PointerType>(); |
| 808 | if (!PT || !PT->isVoidPointerType() || |
| 809 | !PT->getPointeeType().isConstQualified()) |
| 810 | return false; |
| 811 | } |
| 812 | |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | static llvm::Constant *getPrologueSignature(CodeGenModule &CGM, |
| 818 | const FunctionDecl *FD) { |
| 819 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 820 | if (!MD->isStatic()) |
| 821 | return nullptr; |
| 822 | return CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM); |
| 823 | } |
| 824 | |
| 825 | void CodeGenFunction::StartFunction(GlobalDecl GD, |
| 826 | QualType RetTy, |
| 827 | llvm::Function *Fn, |
| 828 | const CGFunctionInfo &FnInfo, |
| 829 | const FunctionArgList &Args, |
| 830 | SourceLocation Loc, |
| 831 | SourceLocation StartLoc) { |
| 832 | (0) . __assert_fail ("!CurFn && \"Do not use a CodeGenFunction object for more than one function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 833, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CurFn && |
| 833 | (0) . __assert_fail ("!CurFn && \"Do not use a CodeGenFunction object for more than one function\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 833, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Do not use a CodeGenFunction object for more than one function"); |
| 834 | |
| 835 | const Decl *D = GD.getDecl(); |
| 836 | |
| 837 | DidCallStackSave = false; |
| 838 | CurCodeDecl = D; |
| 839 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 840 | if (FD->usesSEHTry()) |
| 841 | CurSEHParent = FD; |
| 842 | CurFuncDecl = (D ? D->getNonClosureContext() : nullptr); |
| 843 | FnRetTy = RetTy; |
| 844 | CurFn = Fn; |
| 845 | CurFnInfo = &FnInfo; |
| 846 | (0) . __assert_fail ("CurFn->isDeclaration() && \"Function already has body?\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 846, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurFn->isDeclaration() && "Function already has body?"); |
| 847 | |
| 848 | |
| 849 | |
| 850 | do { |
| 851 | #define SANITIZER(NAME, ID) \ |
| 852 | if (SanOpts.empty()) \ |
| 853 | break; \ |
| 854 | if (SanOpts.has(SanitizerKind::ID)) \ |
| 855 | if (CGM.isInSanitizerBlacklist(SanitizerKind::ID, Fn, Loc)) \ |
| 856 | SanOpts.set(SanitizerKind::ID, false); |
| 857 | |
| 858 | #include "clang/Basic/Sanitizers.def" |
| 859 | #undef SANITIZER |
| 860 | } while (0); |
| 861 | |
| 862 | if (D) { |
| 863 | |
| 864 | for (auto Attr : D->specific_attrs<NoSanitizeAttr>()) { |
| 865 | SanitizerMask mask = Attr->getMask(); |
| 866 | SanOpts.Mask &= ~mask; |
| 867 | if (mask & SanitizerKind::Address) |
| 868 | SanOpts.set(SanitizerKind::KernelAddress, false); |
| 869 | if (mask & SanitizerKind::KernelAddress) |
| 870 | SanOpts.set(SanitizerKind::Address, false); |
| 871 | if (mask & SanitizerKind::HWAddress) |
| 872 | SanOpts.set(SanitizerKind::KernelHWAddress, false); |
| 873 | if (mask & SanitizerKind::KernelHWAddress) |
| 874 | SanOpts.set(SanitizerKind::HWAddress, false); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | |
| 879 | if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress)) |
| 880 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
| 881 | if (SanOpts.hasOneOf(SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress)) |
| 882 | Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress); |
| 883 | if (SanOpts.has(SanitizerKind::Thread)) |
| 884 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
| 885 | if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory)) |
| 886 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
| 887 | if (SanOpts.has(SanitizerKind::SafeStack)) |
| 888 | Fn->addFnAttr(llvm::Attribute::SafeStack); |
| 889 | if (SanOpts.has(SanitizerKind::ShadowCallStack)) |
| 890 | Fn->addFnAttr(llvm::Attribute::ShadowCallStack); |
| 891 | |
| 892 | |
| 893 | if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink)) |
| 894 | Fn->addFnAttr(llvm::Attribute::OptForFuzzing); |
| 895 | |
| 896 | |
| 897 | |
| 898 | if (SanOpts.has(SanitizerKind::Thread)) { |
| 899 | if (const auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(D)) { |
| 900 | IdentifierInfo *II = OMD->getSelector().getIdentifierInfoForSlot(0); |
| 901 | if (OMD->getMethodFamily() == OMF_dealloc || |
| 902 | OMD->getMethodFamily() == OMF_initialize || |
| 903 | (OMD->getSelector().isUnarySelector() && II->isStr(".cxx_destruct"))) { |
| 904 | markAsIgnoreThreadCheckingAtRuntime(Fn); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) { |
| 913 | if (matchesStlAllocatorFn(D, getContext())) |
| 914 | SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | if (D) { |
| 919 | if (const auto *XRayAttr = D->getAttr<XRayInstrumentAttr>()) { |
| 920 | if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has( |
| 921 | XRayInstrKind::Function)) { |
| 922 | if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) |
| 923 | Fn->addFnAttr("function-instrument", "xray-always"); |
| 924 | if (XRayAttr->neverXRayInstrument()) |
| 925 | Fn->addFnAttr("function-instrument", "xray-never"); |
| 926 | if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>()) |
| 927 | if (ShouldXRayInstrumentFunction()) |
| 928 | Fn->addFnAttr("xray-log-args", |
| 929 | llvm::utostr(LogArgs->getArgumentCount())); |
| 930 | } |
| 931 | } else { |
| 932 | if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc)) |
| 933 | Fn->addFnAttr( |
| 934 | "xray-instruction-threshold", |
| 935 | llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold)); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | |
| 940 | Fn->addFnAttr("no-jump-tables", |
| 941 | llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); |
| 942 | |
| 943 | |
| 944 | if (CGM.getCodeGenOpts().ProfileSampleAccurate) |
| 945 | Fn->addFnAttr("profile-sample-accurate"); |
| 946 | |
| 947 | if (getLangOpts().OpenCL) { |
| 948 | |
| 949 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 950 | EmitOpenCLKernelMetadata(FD, Fn); |
| 951 | } |
| 952 | |
| 953 | |
| 954 | |
| 955 | if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) { |
| 956 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 957 | if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) { |
| 958 | |
| 959 | |
| 960 | auto ProtoTy = |
| 961 | getContext().getFunctionTypeWithExceptionSpec(FD->getType(), |
| 962 | EST_None); |
| 963 | llvm::Constant *FTRTTIConst = |
| 964 | CGM.GetAddrOfRTTIDescriptor(ProtoTy, ); |
| 965 | llvm::Constant *FTRTTIConstEncoded = |
| 966 | EncodeAddrForUseInPrologue(Fn, FTRTTIConst); |
| 967 | llvm::Constant *PrologueStructElems[] = {PrologueSig, |
| 968 | FTRTTIConstEncoded}; |
| 969 | llvm::Constant *PrologueStructConst = |
| 970 | llvm::ConstantStruct::getAnon(PrologueStructElems, ); |
| 971 | Fn->setPrologueData(PrologueStructConst); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | |
| 977 | |
| 978 | if (SanOpts.has(SanitizerKind::NullabilityReturn)) { |
| 979 | auto Nullability = FnRetTy->getNullability(getContext()); |
| 980 | if (Nullability && *Nullability == NullabilityKind::NonNull) { |
| 981 | if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && |
| 982 | CurCodeDecl && CurCodeDecl->getAttr<ReturnsNonNullAttr>())) |
| 983 | RetValNullabilityPrecondition = |
| 984 | llvm::ConstantInt::getTrue(getLLVMContext()); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | if (getLangOpts().CPlusPlus) |
| 992 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 993 | if (FD->isMain()) |
| 994 | Fn->addFnAttr(llvm::Attribute::NoRecurse); |
| 995 | |
| 996 | |
| 997 | |
| 998 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 999 | if ((FD->isMain() || FD->isMSVCRTEntryPoint()) && |
| 1000 | CGM.getCodeGenOpts().StackAlignment) |
| 1001 | Fn->addFnAttr("stackrealign"); |
| 1002 | |
| 1003 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); |
| 1009 | AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "allocapt", EntryBB); |
| 1010 | |
| 1011 | ReturnBlock = getJumpDestInCurrentScope("return"); |
| 1012 | |
| 1013 | Builder.SetInsertPoint(EntryBB); |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | if (requiresReturnValueCheck()) { |
| 1018 | ReturnLocation = CreateDefaultAlignTempAlloca(Int8PtrTy, "return.sloc.ptr"); |
| 1019 | InitTempAlloca(ReturnLocation, llvm::ConstantPointerNull::get(Int8PtrTy)); |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | if (CGDebugInfo *DI = getDebugInfo()) { |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | CallingConv CC = CallingConv::CC_C; |
| 1028 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 1029 | if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>()) |
| 1030 | CC = SrcFnTy->getCallConv(); |
| 1031 | SmallVector<QualType, 16> ArgTypes; |
| 1032 | for (const VarDecl *VD : Args) |
| 1033 | ArgTypes.push_back(VD->getType()); |
| 1034 | QualType FnType = getContext().getFunctionType( |
| 1035 | RetTy, ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); |
| 1036 | DI->EmitFunctionStart(GD, Loc, StartLoc, FnType, CurFn, CurFuncIsThunk, |
| 1037 | Builder); |
| 1038 | } |
| 1039 | |
| 1040 | if (ShouldInstrumentFunction()) { |
| 1041 | if (CGM.getCodeGenOpts().InstrumentFunctions) |
| 1042 | CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter"); |
| 1043 | if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) |
| 1044 | CurFn->addFnAttr("instrument-function-entry-inlined", |
| 1045 | "__cyg_profile_func_enter"); |
| 1046 | if (CGM.getCodeGenOpts().InstrumentFunctionEntryBare) |
| 1047 | CurFn->addFnAttr("instrument-function-entry-inlined", |
| 1048 | "__cyg_profile_func_enter_bare"); |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | if (CGM.getCodeGenOpts().InstrumentForProfiling) { |
| 1056 | |
| 1057 | |
| 1058 | if (!CurFuncDecl || !CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) { |
| 1059 | if (CGM.getCodeGenOpts().CallFEntry) |
| 1060 | Fn->addFnAttr("fentry-call", "true"); |
| 1061 | else { |
| 1062 | Fn->addFnAttr("instrument-function-entry-inlined", |
| 1063 | getTarget().getMCountName()); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (RetTy->isVoidType()) { |
| 1069 | |
| 1070 | ReturnValue = Address::invalid(); |
| 1071 | |
| 1072 | |
| 1073 | if (!endsWithReturn(D)) |
| 1074 | ++NumReturnExprs; |
| 1075 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect) { |
| 1076 | |
| 1077 | |
| 1078 | auto AI = CurFn->arg_begin(); |
| 1079 | if (CurFnInfo->getReturnInfo().isSRetAfterThis()) |
| 1080 | ++AI; |
| 1081 | ReturnValue = Address(&*AI, CurFnInfo->getReturnInfo().getIndirectAlign()); |
| 1082 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca && |
| 1083 | !hasScalarEvaluationKind(CurFnInfo->getReturnType())) { |
| 1084 | |
| 1085 | unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex(); |
| 1086 | llvm::Function::arg_iterator EI = CurFn->arg_end(); |
| 1087 | --EI; |
| 1088 | llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx); |
| 1089 | Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result"); |
| 1090 | ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy)); |
| 1091 | } else { |
| 1092 | ReturnValue = CreateIRTemp(RetTy, "retval"); |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | if (getLangOpts().ObjCAutoRefCount && |
| 1098 | !CurFnInfo->isReturnsRetained() && |
| 1099 | RetTy->isObjCRetainableType()) |
| 1100 | AutoreleaseResult = true; |
| 1101 | } |
| 1102 | |
| 1103 | EmitStartEHSpec(CurCodeDecl); |
| 1104 | |
| 1105 | PrologueCleanupDepth = EHStack.stable_begin(); |
| 1106 | |
| 1107 | |
| 1108 | if (getLangOpts().OpenMP && CurCodeDecl) |
| 1109 | CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl); |
| 1110 | |
| 1111 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
| 1112 | |
| 1113 | if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) { |
| 1114 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
| 1115 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); |
| 1116 | if (MD->getParent()->isLambda() && |
| 1117 | MD->getOverloadedOperator() == OO_Call) { |
| 1118 | |
| 1119 | MD->getParent()->getCaptureFields(LambdaCaptureFields, |
| 1120 | LambdaThisCaptureField); |
| 1121 | if (LambdaThisCaptureField) { |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | LValue ThisFieldLValue = EmitLValueForLambdaField(LambdaThisCaptureField); |
| 1129 | if (!LambdaThisCaptureField->getType()->isPointerType()) { |
| 1130 | |
| 1131 | CXXThisValue = ThisFieldLValue.getAddress().getPointer(); |
| 1132 | } else { |
| 1133 | |
| 1134 | |
| 1135 | CXXThisValue = |
| 1136 | EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal(); |
| 1137 | } |
| 1138 | } |
| 1139 | for (auto *FD : MD->getParent()->fields()) { |
| 1140 | if (FD->hasCapturedVLAType()) { |
| 1141 | auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD), |
| 1142 | SourceLocation()).getScalarVal(); |
| 1143 | auto VAT = FD->getCapturedVLAType(); |
| 1144 | VLASizeMap[VAT->getSizeExpr()] = ExprArg; |
| 1145 | } |
| 1146 | } |
| 1147 | } else { |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | CXXThisValue = CXXABIThisValue; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | if (CXXABIThisValue) { |
| 1156 | SanitizerSet SkippedChecks; |
| 1157 | SkippedChecks.set(SanitizerKind::ObjectSize, true); |
| 1158 | QualType ThisTy = MD->getThisType(); |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | if (isLambdaCallOperator(MD) && |
| 1164 | MD->getParent()->getLambdaCaptureDefault() == LCD_None) |
| 1165 | SkippedChecks.set(SanitizerKind::Null, true); |
| 1166 | |
| 1167 | EmitTypeCheck(isa<CXXConstructorDecl>(MD) ? TCK_ConstructorCall |
| 1168 | : TCK_MemberCall, |
| 1169 | Loc, CXXABIThisValue, ThisTy, |
| 1170 | getContext().getTypeAlignInChars(ThisTy->getPointeeType()), |
| 1171 | SkippedChecks); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 1178 | i != e; ++i) { |
| 1179 | const VarDecl *VD = *i; |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | QualType Ty; |
| 1185 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) |
| 1186 | Ty = PVD->getOriginalType(); |
| 1187 | else |
| 1188 | Ty = VD->getType(); |
| 1189 | |
| 1190 | if (Ty->isVariablyModifiedType()) |
| 1191 | EmitVariablyModifiedType(Ty); |
| 1192 | } |
| 1193 | |
| 1194 | if (CGDebugInfo *DI = getDebugInfo()) |
| 1195 | DI->EmitLocation(Builder, StartLoc); |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | if (CurFuncDecl) |
| 1200 | if (const auto *VecWidth = CurFuncDecl->getAttr<MinVectorWidthAttr>()) |
| 1201 | LargestVectorWidth = VecWidth->getVectorWidth(); |
| 1202 | } |
| 1203 | |
| 1204 | void CodeGenFunction::EmitFunctionBody(const Stmt *Body) { |
| 1205 | incrementProfileCounter(Body); |
| 1206 | if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body)) |
| 1207 | EmitCompoundStmtWithoutScope(*S); |
| 1208 | else |
| 1209 | EmitStmt(Body); |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB, |
| 1217 | const Stmt *S) { |
| 1218 | llvm::BasicBlock *SkipCountBB = nullptr; |
| 1219 | if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) { |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | SkipCountBB = createBasicBlock("skipcount"); |
| 1224 | EmitBranch(SkipCountBB); |
| 1225 | } |
| 1226 | EmitBlock(BB); |
| 1227 | uint64_t CurrentCount = getCurrentProfileCount(); |
| 1228 | incrementProfileCounter(S); |
| 1229 | setCurrentProfileCount(getCurrentProfileCount() + CurrentCount); |
| 1230 | if (SkipCountBB) |
| 1231 | EmitBlock(SkipCountBB); |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | static void TryMarkNoThrow(llvm::Function *F) { |
| 1238 | |
| 1239 | |
| 1240 | if (F->isInterposable()) return; |
| 1241 | |
| 1242 | for (llvm::BasicBlock &BB : *F) |
| 1243 | for (llvm::Instruction &I : BB) |
| 1244 | if (I.mayThrow()) |
| 1245 | return; |
| 1246 | |
| 1247 | F->setDoesNotThrow(); |
| 1248 | } |
| 1249 | |
| 1250 | QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD, |
| 1251 | FunctionArgList &Args) { |
| 1252 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 1253 | QualType ResTy = FD->getReturnType(); |
| 1254 | |
| 1255 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 1256 | if (MD && MD->isInstance()) { |
| 1257 | if (CGM.getCXXABI().HasThisReturn(GD)) |
| 1258 | ResTy = MD->getThisType(); |
| 1259 | else if (CGM.getCXXABI().hasMostDerivedReturn(GD)) |
| 1260 | ResTy = CGM.getContext().VoidPtrTy; |
| 1261 | CGM.getCXXABI().buildThisParam(*this, Args); |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | bool PassedParams = true; |
| 1268 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 1269 | if (auto Inherited = CD->getInheritedConstructor()) |
| 1270 | PassedParams = |
| 1271 | getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType()); |
| 1272 | |
| 1273 | if (PassedParams) { |
| 1274 | for (auto *Param : FD->parameters()) { |
| 1275 | Args.push_back(Param); |
| 1276 | if (!Param->hasAttr<PassObjectSizeAttr>()) |
| 1277 | continue; |
| 1278 | |
| 1279 | auto *Implicit = ImplicitParamDecl::Create( |
| 1280 | getContext(), Param->getDeclContext(), Param->getLocation(), |
| 1281 | , getContext().getSizeType(), ImplicitParamDecl::Other); |
| 1282 | SizeArguments[Param] = Implicit; |
| 1283 | Args.push_back(Implicit); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD))) |
| 1288 | CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args); |
| 1289 | |
| 1290 | return ResTy; |
| 1291 | } |
| 1292 | |
| 1293 | static bool |
| 1294 | shouldUseUndefinedBehaviorReturnOptimization(const FunctionDecl *FD, |
| 1295 | const ASTContext &Context) { |
| 1296 | QualType T = FD->getReturnType(); |
| 1297 | |
| 1298 | |
| 1299 | if (const RecordType *RT = T.getCanonicalType()->getAs<RecordType>()) { |
| 1300 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 1301 | return !ClassDecl->hasTrivialDestructor(); |
| 1302 | } |
| 1303 | return !T.isTriviallyCopyableType(Context); |
| 1304 | } |
| 1305 | |
| 1306 | void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, |
| 1307 | const CGFunctionInfo &FnInfo) { |
| 1308 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 1309 | CurGD = GD; |
| 1310 | |
| 1311 | FunctionArgList Args; |
| 1312 | QualType ResTy = BuildFunctionArgList(GD, Args); |
| 1313 | |
| 1314 | |
| 1315 | if (FD->hasAttr<NoDebugAttr>()) |
| 1316 | DebugInfo = nullptr; |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | SourceRange BodyRange; |
| 1321 | if (Stmt *Body = FD->getBody()) |
| 1322 | BodyRange = Body->getSourceRange(); |
| 1323 | else |
| 1324 | BodyRange = FD->getLocation(); |
| 1325 | CurEHLocation = BodyRange.getEnd(); |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | SourceLocation Loc = FD->getLocation(); |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern()) |
| 1337 | if (SpecDecl->hasBody(SpecDecl)) |
| 1338 | Loc = SpecDecl->getLocation(); |
| 1339 | |
| 1340 | Stmt *Body = FD->getBody(); |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | if (Body && ShouldEmitLifetimeMarkers) |
| 1345 | Bypasses.Init(Body); |
| 1346 | |
| 1347 | |
| 1348 | StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin()); |
| 1349 | |
| 1350 | |
| 1351 | PGO.assignRegionCounters(GD, CurFn); |
| 1352 | if (isa<CXXDestructorDecl>(FD)) |
| 1353 | EmitDestructorBody(Args); |
| 1354 | else if (isa<CXXConstructorDecl>(FD)) |
| 1355 | EmitConstructorBody(Args); |
| 1356 | else if (getLangOpts().CUDA && |
| 1357 | !getLangOpts().CUDAIsDevice && |
| 1358 | FD->hasAttr<CUDAGlobalAttr>()) |
| 1359 | CGM.getCUDARuntime().emitDeviceStub(*this, Args); |
| 1360 | else if (isa<CXXMethodDecl>(FD) && |
| 1361 | cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) { |
| 1362 | |
| 1363 | |
| 1364 | EmitLambdaStaticInvokeBody(cast<CXXMethodDecl>(FD)); |
| 1365 | } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) && |
| 1366 | (cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() || |
| 1367 | cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) { |
| 1368 | |
| 1369 | |
| 1370 | emitImplicitAssignmentOperatorBody(Args); |
| 1371 | } else if (Body) { |
| 1372 | EmitFunctionBody(Body); |
| 1373 | } else |
| 1374 | llvm_unreachable("no definition for emitted function"); |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() && !SawAsmBlock && |
| 1383 | !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) { |
| 1384 | bool ShouldEmitUnreachable = |
| 1385 | CGM.getCodeGenOpts().StrictReturn || |
| 1386 | shouldUseUndefinedBehaviorReturnOptimization(FD, getContext()); |
| 1387 | if (SanOpts.has(SanitizerKind::Return)) { |
| 1388 | SanitizerScope SanScope(this); |
| 1389 | llvm::Value *IsFalse = Builder.getFalse(); |
| 1390 | EmitCheck(std::make_pair(IsFalse, SanitizerKind::Return), |
| 1391 | SanitizerHandler::MissingReturn, |
| 1392 | EmitCheckSourceLocation(FD->getLocation()), None); |
| 1393 | } else if (ShouldEmitUnreachable) { |
| 1394 | if (CGM.getCodeGenOpts().OptimizationLevel == 0) |
| 1395 | EmitTrapCall(llvm::Intrinsic::trap); |
| 1396 | } |
| 1397 | if (SanOpts.has(SanitizerKind::Return) || ShouldEmitUnreachable) { |
| 1398 | Builder.CreateUnreachable(); |
| 1399 | Builder.ClearInsertionPoint(); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | FinishFunction(BodyRange.getEnd()); |
| 1405 | |
| 1406 | |
| 1407 | |
| 1408 | if (!CurFn->doesNotThrow()) |
| 1409 | TryMarkNoThrow(CurFn); |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 1416 | |
| 1417 | if (!S) return false; |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | |
| 1424 | if (isa<LabelStmt>(S)) |
| 1425 | return true; |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 1430 | return true; |
| 1431 | |
| 1432 | |
| 1433 | if (isa<SwitchStmt>(S)) |
| 1434 | IgnoreCaseStmts = true; |
| 1435 | |
| 1436 | |
| 1437 | for (const Stmt *SubStmt : S->children()) |
| 1438 | if (ContainsLabel(SubStmt, IgnoreCaseStmts)) |
| 1439 | return true; |
| 1440 | |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
| 1447 | bool CodeGenFunction::containsBreak(const Stmt *S) { |
| 1448 | |
| 1449 | if (!S) return false; |
| 1450 | |
| 1451 | |
| 1452 | |
| 1453 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) || |
| 1454 | isa<ForStmt>(S)) |
| 1455 | return false; |
| 1456 | |
| 1457 | if (isa<BreakStmt>(S)) |
| 1458 | return true; |
| 1459 | |
| 1460 | |
| 1461 | for (const Stmt *SubStmt : S->children()) |
| 1462 | if (containsBreak(SubStmt)) |
| 1463 | return true; |
| 1464 | |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
| 1468 | bool CodeGenFunction::mightAddDeclToScope(const Stmt *S) { |
| 1469 | if (!S) return false; |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | if (isa<IfStmt>(S) || isa<SwitchStmt>(S) || isa<WhileStmt>(S) || |
| 1476 | isa<DoStmt>(S) || isa<ForStmt>(S) || isa<CompoundStmt>(S) || |
| 1477 | isa<CXXForRangeStmt>(S) || isa<CXXTryStmt>(S) || |
| 1478 | isa<ObjCForCollectionStmt>(S) || isa<ObjCAtTryStmt>(S)) |
| 1479 | return false; |
| 1480 | |
| 1481 | if (isa<DeclStmt>(S)) |
| 1482 | return true; |
| 1483 | |
| 1484 | for (const Stmt *SubStmt : S->children()) |
| 1485 | if (mightAddDeclToScope(SubStmt)) |
| 1486 | return true; |
| 1487 | |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, |
| 1495 | bool &ResultBool, |
| 1496 | bool AllowLabels) { |
| 1497 | llvm::APSInt ResultInt; |
| 1498 | if (!ConstantFoldsToSimpleInteger(Cond, ResultInt, AllowLabels)) |
| 1499 | return false; |
| 1500 | |
| 1501 | ResultBool = ResultInt.getBoolValue(); |
| 1502 | return true; |
| 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | |
| 1507 | |
| 1508 | bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, |
| 1509 | llvm::APSInt &ResultInt, |
| 1510 | bool AllowLabels) { |
| 1511 | |
| 1512 | |
| 1513 | Expr::EvalResult Result; |
| 1514 | if (!Cond->EvaluateAsInt(Result, getContext())) |
| 1515 | return false; |
| 1516 | |
| 1517 | llvm::APSInt Int = Result.Val.getInt(); |
| 1518 | if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond)) |
| 1519 | return false; |
| 1520 | |
| 1521 | ResultInt = Int; |
| 1522 | return true; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 1532 | llvm::BasicBlock *TrueBlock, |
| 1533 | llvm::BasicBlock *FalseBlock, |
| 1534 | uint64_t TrueCount) { |
| 1535 | Cond = Cond->IgnoreParens(); |
| 1536 | |
| 1537 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 1538 | |
| 1539 | |
| 1540 | if (CondBOp->getOpcode() == BO_LAnd) { |
| 1541 | |
| 1542 | |
| 1543 | bool ConstantBool = false; |
| 1544 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && |
| 1545 | ConstantBool) { |
| 1546 | |
| 1547 | incrementProfileCounter(CondBOp); |
| 1548 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, |
| 1549 | TrueCount); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && |
| 1555 | ConstantBool) { |
| 1556 | |
| 1557 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock, |
| 1558 | TrueCount); |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
| 1564 | |
| 1565 | |
| 1566 | uint64_t RHSCount = getProfileCount(CondBOp->getRHS()); |
| 1567 | |
| 1568 | ConditionalEvaluation eval(*this); |
| 1569 | { |
| 1570 | ApplyDebugLocation DL(*this, Cond); |
| 1571 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount); |
| 1572 | EmitBlock(LHSTrue); |
| 1573 | } |
| 1574 | |
| 1575 | incrementProfileCounter(CondBOp); |
| 1576 | setCurrentProfileCount(getProfileCount(CondBOp->getRHS())); |
| 1577 | |
| 1578 | |
| 1579 | eval.begin(*this); |
| 1580 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount); |
| 1581 | eval.end(*this); |
| 1582 | |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | if (CondBOp->getOpcode() == BO_LOr) { |
| 1587 | |
| 1588 | |
| 1589 | bool ConstantBool = false; |
| 1590 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && |
| 1591 | !ConstantBool) { |
| 1592 | |
| 1593 | incrementProfileCounter(CondBOp); |
| 1594 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, |
| 1595 | TrueCount); |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | |
| 1600 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && |
| 1601 | !ConstantBool) { |
| 1602 | |
| 1603 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock, |
| 1604 | TrueCount); |
| 1605 | } |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | uint64_t LHSCount = |
| 1614 | getCurrentProfileCount() - getProfileCount(CondBOp->getRHS()); |
| 1615 | uint64_t RHSCount = TrueCount - LHSCount; |
| 1616 | |
| 1617 | ConditionalEvaluation eval(*this); |
| 1618 | { |
| 1619 | ApplyDebugLocation DL(*this, Cond); |
| 1620 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount); |
| 1621 | EmitBlock(LHSFalse); |
| 1622 | } |
| 1623 | |
| 1624 | incrementProfileCounter(CondBOp); |
| 1625 | setCurrentProfileCount(getProfileCount(CondBOp->getRHS())); |
| 1626 | |
| 1627 | |
| 1628 | eval.begin(*this); |
| 1629 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount); |
| 1630 | |
| 1631 | eval.end(*this); |
| 1632 | |
| 1633 | return; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 1638 | |
| 1639 | if (CondUOp->getOpcode() == UO_LNot) { |
| 1640 | |
| 1641 | uint64_t FalseCount = getCurrentProfileCount() - TrueCount; |
| 1642 | |
| 1643 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock, |
| 1644 | FalseCount); |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 1649 | |
| 1650 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 1651 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 1652 | |
| 1653 | ConditionalEvaluation cond(*this); |
| 1654 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock, |
| 1655 | getProfileCount(CondOp)); |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | |
| 1661 | |
| 1662 | uint64_t LHSScaledTrueCount = 0; |
| 1663 | if (TrueCount) { |
| 1664 | double LHSRatio = |
| 1665 | getProfileCount(CondOp) / (double)getCurrentProfileCount(); |
| 1666 | LHSScaledTrueCount = TrueCount * LHSRatio; |
| 1667 | } |
| 1668 | |
| 1669 | cond.begin(*this); |
| 1670 | EmitBlock(LHSBlock); |
| 1671 | incrementProfileCounter(CondOp); |
| 1672 | { |
| 1673 | ApplyDebugLocation DL(*this, Cond); |
| 1674 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock, |
| 1675 | LHSScaledTrueCount); |
| 1676 | } |
| 1677 | cond.end(*this); |
| 1678 | |
| 1679 | cond.begin(*this); |
| 1680 | EmitBlock(RHSBlock); |
| 1681 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock, |
| 1682 | TrueCount - LHSScaledTrueCount); |
| 1683 | cond.end(*this); |
| 1684 | |
| 1685 | return; |
| 1686 | } |
| 1687 | |
| 1688 | if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) { |
| 1689 | |
| 1690 | |
| 1691 | |
| 1692 | |
| 1693 | |
| 1694 | EmitCXXThrowExpr(Throw, ); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | |
| 1699 | |
| 1700 | |
| 1701 | llvm::MDNode *Unpredictable = nullptr; |
| 1702 | auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts()); |
| 1703 | if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { |
| 1704 | auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl()); |
| 1705 | if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { |
| 1706 | llvm::MDBuilder MDHelper(getLLVMContext()); |
| 1707 | Unpredictable = MDHelper.createUnpredictable(); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount); |
| 1714 | llvm::MDNode *Weights = |
| 1715 | createProfileWeights(TrueCount, CurrentCount - TrueCount); |
| 1716 | |
| 1717 | |
| 1718 | llvm::Value *CondV; |
| 1719 | { |
| 1720 | ApplyDebugLocation DL(*this, Cond); |
| 1721 | CondV = EvaluateExprAsBool(Cond); |
| 1722 | } |
| 1723 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable); |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | |
| 1728 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) { |
| 1729 | CGM.ErrorUnsupported(S, Type); |
| 1730 | } |
| 1731 | |
| 1732 | |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, |
| 1740 | Address dest, Address src, |
| 1741 | llvm::Value *sizeInChars) { |
| 1742 | CGBuilderTy &Builder = CGF.Builder; |
| 1743 | |
| 1744 | CharUnits baseSize = CGF.getContext().getTypeSizeInChars(baseType); |
| 1745 | llvm::Value *baseSizeInChars |
| 1746 | = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity()); |
| 1747 | |
| 1748 | Address begin = |
| 1749 | Builder.CreateElementBitCast(dest, CGF.Int8Ty, "vla.begin"); |
| 1750 | llvm::Value *end = |
| 1751 | Builder.CreateInBoundsGEP(begin.getPointer(), sizeInChars, "vla.end"); |
| 1752 | |
| 1753 | llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock(); |
| 1754 | llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop"); |
| 1755 | llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont"); |
| 1756 | |
| 1757 | |
| 1758 | |
| 1759 | CGF.EmitBlock(loopBB); |
| 1760 | |
| 1761 | llvm::PHINode *cur = Builder.CreatePHI(begin.getType(), 2, "vla.cur"); |
| 1762 | cur->addIncoming(begin.getPointer(), originBB); |
| 1763 | |
| 1764 | CharUnits curAlign = |
| 1765 | dest.getAlignment().alignmentOfArrayElement(baseSize); |
| 1766 | |
| 1767 | |
| 1768 | Builder.CreateMemCpy(Address(cur, curAlign), src, baseSizeInChars, |
| 1769 | false); |
| 1770 | |
| 1771 | |
| 1772 | llvm::Value *next = |
| 1773 | Builder.CreateInBoundsGEP(CGF.Int8Ty, cur, baseSizeInChars, "vla.next"); |
| 1774 | |
| 1775 | |
| 1776 | llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone"); |
| 1777 | Builder.CreateCondBr(done, contBB, loopBB); |
| 1778 | cur->addIncoming(next, loopBB); |
| 1779 | |
| 1780 | CGF.EmitBlock(contBB); |
| 1781 | } |
| 1782 | |
| 1783 | void |
| 1784 | CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) { |
| 1785 | |
| 1786 | if (getLangOpts().CPlusPlus) { |
| 1787 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1788 | if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) |
| 1789 | return; |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | if (DestPtr.getElementType() != Int8Ty) |
| 1795 | DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty); |
| 1796 | |
| 1797 | |
| 1798 | CharUnits size = getContext().getTypeSizeInChars(Ty); |
| 1799 | |
| 1800 | llvm::Value *SizeVal; |
| 1801 | const VariableArrayType *vla; |
| 1802 | |
| 1803 | |
| 1804 | if (size.isZero()) { |
| 1805 | |
| 1806 | if (const VariableArrayType *vlaType = |
| 1807 | dyn_cast_or_null<VariableArrayType>( |
| 1808 | getContext().getAsArrayType(Ty))) { |
| 1809 | auto VlaSize = getVLASize(vlaType); |
| 1810 | SizeVal = VlaSize.NumElts; |
| 1811 | CharUnits eltSize = getContext().getTypeSizeInChars(VlaSize.Type); |
| 1812 | if (!eltSize.isOne()) |
| 1813 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize)); |
| 1814 | vla = vlaType; |
| 1815 | } else { |
| 1816 | return; |
| 1817 | } |
| 1818 | } else { |
| 1819 | SizeVal = CGM.getSize(size); |
| 1820 | vla = nullptr; |
| 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | |
| 1827 | if (!CGM.getTypes().isZeroInitializable(Ty)) { |
| 1828 | |
| 1829 | if (vla) Ty = getContext().getBaseElementType(vla); |
| 1830 | |
| 1831 | llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); |
| 1832 | |
| 1833 | llvm::GlobalVariable *NullVariable = |
| 1834 | new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), |
| 1835 | , |
| 1836 | llvm::GlobalVariable::PrivateLinkage, |
| 1837 | NullConstant, Twine()); |
| 1838 | CharUnits NullAlign = DestPtr.getAlignment(); |
| 1839 | NullVariable->setAlignment(NullAlign.getQuantity()); |
| 1840 | Address SrcPtr(Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()), |
| 1841 | NullAlign); |
| 1842 | |
| 1843 | if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal); |
| 1844 | |
| 1845 | |
| 1846 | Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, false); |
| 1847 | return; |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false); |
| 1854 | } |
| 1855 | |
| 1856 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) { |
| 1857 | |
| 1858 | if (!IndirectBranch) |
| 1859 | GetIndirectGotoBlock(); |
| 1860 | |
| 1861 | llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); |
| 1862 | |
| 1863 | |
| 1864 | IndirectBranch->addDestination(BB); |
| 1865 | return llvm::BlockAddress::get(CurFn, BB); |
| 1866 | } |
| 1867 | |
| 1868 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
| 1869 | |
| 1870 | if (IndirectBranch) return IndirectBranch->getParent(); |
| 1871 | |
| 1872 | CGBuilderTy TmpBuilder(*this, createBasicBlock("indirectgoto")); |
| 1873 | |
| 1874 | |
| 1875 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0, |
| 1876 | "indirect.goto.dest"); |
| 1877 | |
| 1878 | |
| 1879 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 1880 | return IndirectBranch->getParent(); |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | |
| 1885 | llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, |
| 1886 | QualType &baseType, |
| 1887 | Address &addr) { |
| 1888 | const ArrayType *arrayType = origArrayType; |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | llvm::Value *numVLAElements = nullptr; |
| 1893 | if (isa<VariableArrayType>(arrayType)) { |
| 1894 | numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).NumElts; |
| 1895 | |
| 1896 | |
| 1897 | |
| 1898 | do { |
| 1899 | QualType elementType = arrayType->getElementType(); |
| 1900 | arrayType = getContext().getAsArrayType(elementType); |
| 1901 | |
| 1902 | |
| 1903 | if (!arrayType) { |
| 1904 | baseType = elementType; |
| 1905 | return numVLAElements; |
| 1906 | } |
| 1907 | } while (isa<VariableArrayType>(arrayType)); |
| 1908 | |
| 1909 | |
| 1910 | |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | |
| 1916 | SmallVector<llvm::Value*, 8> gepIndices; |
| 1917 | |
| 1918 | |
| 1919 | llvm::ConstantInt *zero = Builder.getInt32(0); |
| 1920 | gepIndices.push_back(zero); |
| 1921 | |
| 1922 | uint64_t countFromCLAs = 1; |
| 1923 | QualType eltType; |
| 1924 | |
| 1925 | llvm::ArrayType *llvmArrayType = |
| 1926 | dyn_cast<llvm::ArrayType>(addr.getElementType()); |
| 1927 | while (llvmArrayType) { |
| 1928 | (arrayType)", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1928, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<ConstantArrayType>(arrayType)); |
| 1929 | (arrayType)->getSize().getZExtValue() == llvmArrayType->getNumElements()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1930, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue() |
| 1930 | (arrayType)->getSize().getZExtValue() == llvmArrayType->getNumElements()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1930, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> == llvmArrayType->getNumElements()); |
| 1931 | |
| 1932 | gepIndices.push_back(zero); |
| 1933 | countFromCLAs *= llvmArrayType->getNumElements(); |
| 1934 | eltType = arrayType->getElementType(); |
| 1935 | |
| 1936 | llvmArrayType = |
| 1937 | dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType()); |
| 1938 | arrayType = getContext().getAsArrayType(arrayType->getElementType()); |
| 1939 | (0) . __assert_fail ("(!llvmArrayType || arrayType) && \"LLVM and Clang types are out-of-synch\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1940, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!llvmArrayType || arrayType) && |
| 1940 | (0) . __assert_fail ("(!llvmArrayType || arrayType) && \"LLVM and Clang types are out-of-synch\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1940, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "LLVM and Clang types are out-of-synch"); |
| 1941 | } |
| 1942 | |
| 1943 | if (arrayType) { |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | while (arrayType) { |
| 1948 | countFromCLAs *= |
| 1949 | cast<ConstantArrayType>(arrayType)->getSize().getZExtValue(); |
| 1950 | eltType = arrayType->getElementType(); |
| 1951 | arrayType = getContext().getAsArrayType(eltType); |
| 1952 | } |
| 1953 | |
| 1954 | llvm::Type *baseType = ConvertType(eltType); |
| 1955 | addr = Builder.CreateElementBitCast(addr, baseType, "array.begin"); |
| 1956 | } else { |
| 1957 | |
| 1958 | addr = Address(Builder.CreateInBoundsGEP(addr.getPointer(), |
| 1959 | gepIndices, "array.begin"), |
| 1960 | addr.getAlignment()); |
| 1961 | } |
| 1962 | |
| 1963 | baseType = eltType; |
| 1964 | |
| 1965 | llvm::Value *numElements |
| 1966 | = llvm::ConstantInt::get(SizeTy, countFromCLAs); |
| 1967 | |
| 1968 | |
| 1969 | if (numVLAElements) |
| 1970 | numElements = Builder.CreateNUWMul(numVLAElements, numElements); |
| 1971 | |
| 1972 | return numElements; |
| 1973 | } |
| 1974 | |
| 1975 | CodeGenFunction::VlaSizePair CodeGenFunction::getVLASize(QualType type) { |
| 1976 | const VariableArrayType *vla = getContext().getAsVariableArrayType(type); |
| 1977 | (0) . __assert_fail ("vla && \"type was not a variable array type!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1977, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(vla && "type was not a variable array type!"); |
| 1978 | return getVLASize(vla); |
| 1979 | } |
| 1980 | |
| 1981 | CodeGenFunction::VlaSizePair |
| 1982 | CodeGenFunction::getVLASize(const VariableArrayType *type) { |
| 1983 | |
| 1984 | llvm::Value *numElements = nullptr; |
| 1985 | |
| 1986 | QualType elementType; |
| 1987 | do { |
| 1988 | elementType = type->getElementType(); |
| 1989 | llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()]; |
| 1990 | (0) . __assert_fail ("vlaSize && \"no size for VLA!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1990, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(vlaSize && "no size for VLA!"); |
| 1991 | getType() == SizeTy", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 1991, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(vlaSize->getType() == SizeTy); |
| 1992 | |
| 1993 | if (!numElements) { |
| 1994 | numElements = vlaSize; |
| 1995 | } else { |
| 1996 | |
| 1997 | |
| 1998 | numElements = Builder.CreateNUWMul(numElements, vlaSize); |
| 1999 | } |
| 2000 | } while ((type = getContext().getAsVariableArrayType(elementType))); |
| 2001 | |
| 2002 | return { numElements, elementType }; |
| 2003 | } |
| 2004 | |
| 2005 | CodeGenFunction::VlaSizePair |
| 2006 | CodeGenFunction::getVLAElements1D(QualType type) { |
| 2007 | const VariableArrayType *vla = getContext().getAsVariableArrayType(type); |
| 2008 | (0) . __assert_fail ("vla && \"type was not a variable array type!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2008, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(vla && "type was not a variable array type!"); |
| 2009 | return getVLAElements1D(vla); |
| 2010 | } |
| 2011 | |
| 2012 | CodeGenFunction::VlaSizePair |
| 2013 | CodeGenFunction::getVLAElements1D(const VariableArrayType *Vla) { |
| 2014 | llvm::Value *VlaSize = VLASizeMap[Vla->getSizeExpr()]; |
| 2015 | (0) . __assert_fail ("VlaSize && \"no size for VLA!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2015, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VlaSize && "no size for VLA!"); |
| 2016 | getType() == SizeTy", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2016, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VlaSize->getType() == SizeTy); |
| 2017 | return { VlaSize, Vla->getElementType() }; |
| 2018 | } |
| 2019 | |
| 2020 | void CodeGenFunction::EmitVariablyModifiedType(QualType type) { |
| 2021 | (0) . __assert_fail ("type->isVariablyModifiedType() && \"Must pass variably modified type to EmitVLASizes!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2022, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(type->isVariablyModifiedType() && |
| 2022 | (0) . __assert_fail ("type->isVariablyModifiedType() && \"Must pass variably modified type to EmitVLASizes!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2022, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Must pass variably modified type to EmitVLASizes!"); |
| 2023 | |
| 2024 | EnsureInsertPoint(); |
| 2025 | |
| 2026 | |
| 2027 | |
| 2028 | do { |
| 2029 | isVariablyModifiedType()", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2029, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(type->isVariablyModifiedType()); |
| 2030 | |
| 2031 | const Type *ty = type.getTypePtr(); |
| 2032 | switch (ty->getTypeClass()) { |
| 2033 | |
| 2034 | #define TYPE(Class, Base) |
| 2035 | #define ABSTRACT_TYPE(Class, Base) |
| 2036 | #define NON_CANONICAL_TYPE(Class, Base) |
| 2037 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2038 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) |
| 2039 | #include "clang/AST/TypeNodes.def" |
| 2040 | llvm_unreachable("unexpected dependent type!"); |
| 2041 | |
| 2042 | |
| 2043 | case Type::Builtin: |
| 2044 | case Type::Complex: |
| 2045 | case Type::Vector: |
| 2046 | case Type::ExtVector: |
| 2047 | case Type::Record: |
| 2048 | case Type::Enum: |
| 2049 | case Type::Elaborated: |
| 2050 | case Type::TemplateSpecialization: |
| 2051 | case Type::ObjCTypeParam: |
| 2052 | case Type::ObjCObject: |
| 2053 | case Type::ObjCInterface: |
| 2054 | case Type::ObjCObjectPointer: |
| 2055 | llvm_unreachable("type class is never variably-modified!"); |
| 2056 | |
| 2057 | case Type::Adjusted: |
| 2058 | type = cast<AdjustedType>(ty)->getAdjustedType(); |
| 2059 | break; |
| 2060 | |
| 2061 | case Type::Decayed: |
| 2062 | type = cast<DecayedType>(ty)->getPointeeType(); |
| 2063 | break; |
| 2064 | |
| 2065 | case Type::Pointer: |
| 2066 | type = cast<PointerType>(ty)->getPointeeType(); |
| 2067 | break; |
| 2068 | |
| 2069 | case Type::BlockPointer: |
| 2070 | type = cast<BlockPointerType>(ty)->getPointeeType(); |
| 2071 | break; |
| 2072 | |
| 2073 | case Type::LValueReference: |
| 2074 | case Type::RValueReference: |
| 2075 | type = cast<ReferenceType>(ty)->getPointeeType(); |
| 2076 | break; |
| 2077 | |
| 2078 | case Type::MemberPointer: |
| 2079 | type = cast<MemberPointerType>(ty)->getPointeeType(); |
| 2080 | break; |
| 2081 | |
| 2082 | case Type::ConstantArray: |
| 2083 | case Type::IncompleteArray: |
| 2084 | |
| 2085 | type = cast<ArrayType>(ty)->getElementType(); |
| 2086 | break; |
| 2087 | |
| 2088 | case Type::VariableArray: { |
| 2089 | |
| 2090 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | if (const Expr *size = vat->getSizeExpr()) { |
| 2095 | |
| 2096 | |
| 2097 | llvm::Value *&entry = VLASizeMap[size]; |
| 2098 | if (!entry) { |
| 2099 | llvm::Value *Size = EmitScalarExpr(size); |
| 2100 | |
| 2101 | |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | if (SanOpts.has(SanitizerKind::VLABound) && |
| 2106 | size->getType()->isSignedIntegerType()) { |
| 2107 | SanitizerScope SanScope(this); |
| 2108 | llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType()); |
| 2109 | llvm::Constant *StaticArgs[] = { |
| 2110 | EmitCheckSourceLocation(size->getBeginLoc()), |
| 2111 | EmitCheckTypeDescriptor(size->getType())}; |
| 2112 | EmitCheck(std::make_pair(Builder.CreateICmpSGT(Size, Zero), |
| 2113 | SanitizerKind::VLABound), |
| 2114 | SanitizerHandler::VLABoundNotPositive, StaticArgs, Size); |
| 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | entry = Builder.CreateIntCast(Size, SizeTy, false); |
| 2120 | } |
| 2121 | } |
| 2122 | type = vat->getElementType(); |
| 2123 | break; |
| 2124 | } |
| 2125 | |
| 2126 | case Type::FunctionProto: |
| 2127 | case Type::FunctionNoProto: |
| 2128 | type = cast<FunctionType>(ty)->getReturnType(); |
| 2129 | break; |
| 2130 | |
| 2131 | case Type::Paren: |
| 2132 | case Type::TypeOf: |
| 2133 | case Type::UnaryTransform: |
| 2134 | case Type::Attributed: |
| 2135 | case Type::SubstTemplateTypeParm: |
| 2136 | case Type::PackExpansion: |
| 2137 | |
| 2138 | type = type.getSingleStepDesugaredType(getContext()); |
| 2139 | break; |
| 2140 | |
| 2141 | case Type::Typedef: |
| 2142 | case Type::Decltype: |
| 2143 | case Type::Auto: |
| 2144 | case Type::DeducedTemplateSpecialization: |
| 2145 | |
| 2146 | return; |
| 2147 | |
| 2148 | case Type::TypeOfExpr: |
| 2149 | |
| 2150 | EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr()); |
| 2151 | return; |
| 2152 | |
| 2153 | case Type::Atomic: |
| 2154 | type = cast<AtomicType>(ty)->getValueType(); |
| 2155 | break; |
| 2156 | |
| 2157 | case Type::Pipe: |
| 2158 | type = cast<PipeType>(ty)->getElementType(); |
| 2159 | break; |
| 2160 | } |
| 2161 | } while (type->isVariablyModifiedType()); |
| 2162 | } |
| 2163 | |
| 2164 | Address CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 2165 | if (getContext().getBuiltinVaListType()->isArrayType()) |
| 2166 | return EmitPointerWithAlignment(E); |
| 2167 | return EmitLValue(E).getAddress(); |
| 2168 | } |
| 2169 | |
| 2170 | Address CodeGenFunction::EmitMSVAListRef(const Expr *E) { |
| 2171 | return EmitLValue(E).getAddress(); |
| 2172 | } |
| 2173 | |
| 2174 | void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, |
| 2175 | const APValue &Init) { |
| 2176 | (0) . __assert_fail ("!Init.isUninit() && \"Invalid DeclRefExpr initializer!\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2176, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Init.isUninit() && "Invalid DeclRefExpr initializer!"); |
| 2177 | if (CGDebugInfo *Dbg = getDebugInfo()) |
| 2178 | if (CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) |
| 2179 | Dbg->EmitGlobalVariable(E->getDecl(), Init); |
| 2180 | } |
| 2181 | |
| 2182 | CodeGenFunction::PeepholeProtection |
| 2183 | CodeGenFunction::protectFromPeepholes(RValue rvalue) { |
| 2184 | |
| 2185 | |
| 2186 | |
| 2187 | |
| 2188 | if (!rvalue.isScalar()) return PeepholeProtection(); |
| 2189 | llvm::Value *value = rvalue.getScalarVal(); |
| 2190 | if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection(); |
| 2191 | |
| 2192 | |
| 2193 | assert(HaveInsertPoint()); |
| 2194 | llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "", |
| 2195 | Builder.GetInsertBlock()); |
| 2196 | |
| 2197 | PeepholeProtection protection; |
| 2198 | protection.Inst = inst; |
| 2199 | return protection; |
| 2200 | } |
| 2201 | |
| 2202 | void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) { |
| 2203 | if (!protection.Inst) return; |
| 2204 | |
| 2205 | |
| 2206 | protection.Inst->eraseFromParent(); |
| 2207 | } |
| 2208 | |
| 2209 | void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, |
| 2210 | QualType Ty, SourceLocation Loc, |
| 2211 | SourceLocation AssumptionLoc, |
| 2212 | llvm::Value *Alignment, |
| 2213 | llvm::Value *OffsetValue) { |
| 2214 | llvm::Value *TheCheck; |
| 2215 | llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( |
| 2216 | CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck); |
| 2217 | if (SanOpts.has(SanitizerKind::Alignment)) { |
| 2218 | EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, |
| 2219 | OffsetValue, TheCheck, Assumption); |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, |
| 2224 | QualType Ty, SourceLocation Loc, |
| 2225 | SourceLocation AssumptionLoc, |
| 2226 | unsigned Alignment, |
| 2227 | llvm::Value *OffsetValue) { |
| 2228 | llvm::Value *TheCheck; |
| 2229 | llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( |
| 2230 | CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck); |
| 2231 | if (SanOpts.has(SanitizerKind::Alignment)) { |
| 2232 | llvm::Value *AlignmentVal = llvm::ConstantInt::get(IntPtrTy, Alignment); |
| 2233 | EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, AlignmentVal, |
| 2234 | OffsetValue, TheCheck, Assumption); |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, |
| 2239 | const Expr *E, |
| 2240 | SourceLocation AssumptionLoc, |
| 2241 | unsigned Alignment, |
| 2242 | llvm::Value *OffsetValue) { |
| 2243 | if (auto *CE = dyn_cast<CastExpr>(E)) |
| 2244 | E = CE->getSubExprAsWritten(); |
| 2245 | QualType Ty = E->getType(); |
| 2246 | SourceLocation Loc = E->getExprLoc(); |
| 2247 | |
| 2248 | EmitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment, |
| 2249 | OffsetValue); |
| 2250 | } |
| 2251 | |
| 2252 | llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn, |
| 2253 | llvm::Value *AnnotatedVal, |
| 2254 | StringRef AnnotationStr, |
| 2255 | SourceLocation Location) { |
| 2256 | llvm::Value *Args[4] = { |
| 2257 | AnnotatedVal, |
| 2258 | Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy), |
| 2259 | Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy), |
| 2260 | CGM.EmitAnnotationLineNo(Location) |
| 2261 | }; |
| 2262 | return Builder.CreateCall(AnnotationFn, Args); |
| 2263 | } |
| 2264 | |
| 2265 | void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) { |
| 2266 | (0) . __assert_fail ("D->hasAttr() && \"no annotate attribute\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2266, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 2267 | |
| 2268 | |
| 2269 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) |
| 2270 | EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), |
| 2271 | Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()), |
| 2272 | I->getAnnotation(), D->getLocation()); |
| 2273 | } |
| 2274 | |
| 2275 | Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, |
| 2276 | Address Addr) { |
| 2277 | (0) . __assert_fail ("D->hasAttr() && \"no annotate attribute\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2277, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 2278 | llvm::Value *V = Addr.getPointer(); |
| 2279 | llvm::Type *VTy = V->getType(); |
| 2280 | llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, |
| 2281 | CGM.Int8PtrTy); |
| 2282 | |
| 2283 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | if (VTy != CGM.Int8PtrTy) |
| 2288 | V = Builder.CreateBitCast(V, CGM.Int8PtrTy); |
| 2289 | V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation()); |
| 2290 | V = Builder.CreateBitCast(V, VTy); |
| 2291 | } |
| 2292 | |
| 2293 | return Address(V, Addr.getAlignment()); |
| 2294 | } |
| 2295 | |
| 2296 | CodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { } |
| 2297 | |
| 2298 | CodeGenFunction::SanitizerScope::SanitizerScope(CodeGenFunction *CGF) |
| 2299 | : CGF(CGF) { |
| 2300 | IsSanitizerScope", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2300, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CGF->IsSanitizerScope); |
| 2301 | CGF->IsSanitizerScope = true; |
| 2302 | } |
| 2303 | |
| 2304 | CodeGenFunction::SanitizerScope::~SanitizerScope() { |
| 2305 | CGF->IsSanitizerScope = false; |
| 2306 | } |
| 2307 | |
| 2308 | void CodeGenFunction::InsertHelper(llvm::Instruction *I, |
| 2309 | const llvm::Twine &Name, |
| 2310 | llvm::BasicBlock *BB, |
| 2311 | llvm::BasicBlock::iterator InsertPt) const { |
| 2312 | LoopStack.InsertHelper(I); |
| 2313 | if (IsSanitizerScope) |
| 2314 | CGM.getSanitizerMetadata()->disableSanitizerForInstruction(I); |
| 2315 | } |
| 2316 | |
| 2317 | void CGBuilderInserter::InsertHelper( |
| 2318 | llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, |
| 2319 | llvm::BasicBlock::iterator InsertPt) const { |
| 2320 | llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); |
| 2321 | if (CGF) |
| 2322 | CGF->InsertHelper(I, Name, BB, InsertPt); |
| 2323 | } |
| 2324 | |
| 2325 | static bool hasRequiredFeatures(const SmallVectorImpl<StringRef> &ReqFeatures, |
| 2326 | CodeGenModule &CGM, const FunctionDecl *FD, |
| 2327 | std::string &FirstMissing) { |
| 2328 | |
| 2329 | if (ReqFeatures.empty()) |
| 2330 | return false; |
| 2331 | |
| 2332 | |
| 2333 | |
| 2334 | llvm::StringMap<bool> CallerFeatureMap; |
| 2335 | CGM.getFunctionFeatureMap(CallerFeatureMap, GlobalDecl().getWithDecl(FD)); |
| 2336 | |
| 2337 | |
| 2338 | |
| 2339 | return std::all_of( |
| 2340 | ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef Feature) { |
| 2341 | SmallVector<StringRef, 1> OrFeatures; |
| 2342 | Feature.split(OrFeatures, '|'); |
| 2343 | return llvm::any_of(OrFeatures, [&](StringRef Feature) { |
| 2344 | if (!CallerFeatureMap.lookup(Feature)) { |
| 2345 | FirstMissing = Feature.str(); |
| 2346 | return false; |
| 2347 | } |
| 2348 | return true; |
| 2349 | }); |
| 2350 | }); |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | void CodeGenFunction::checkTargetFeatures(const CallExpr *E, |
| 2356 | const FunctionDecl *TargetDecl) { |
| 2357 | |
| 2358 | if (!TargetDecl) |
| 2359 | return; |
| 2360 | |
| 2361 | |
| 2362 | |
| 2363 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl); |
| 2364 | if (!FD) |
| 2365 | return; |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | |
| 2370 | unsigned BuiltinID = TargetDecl->getBuiltinID(); |
| 2371 | std::string MissingFeature; |
| 2372 | if (BuiltinID) { |
| 2373 | SmallVector<StringRef, 1> ReqFeatures; |
| 2374 | const char *FeatureList = |
| 2375 | CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID); |
| 2376 | |
| 2377 | if (!FeatureList || StringRef(FeatureList) == "") |
| 2378 | return; |
| 2379 | StringRef(FeatureList).split(ReqFeatures, ','); |
| 2380 | if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature)) |
| 2381 | CGM.getDiags().Report(E->getBeginLoc(), diag::err_builtin_needs_feature) |
| 2382 | << TargetDecl->getDeclName() |
| 2383 | << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID); |
| 2384 | |
| 2385 | } else if (TargetDecl->hasAttr<TargetAttr>() || |
| 2386 | TargetDecl->hasAttr<CPUSpecificAttr>()) { |
| 2387 | |
| 2388 | |
| 2389 | const TargetAttr *TD = TargetDecl->getAttr<TargetAttr>(); |
| 2390 | TargetAttr::ParsedTargetAttr ParsedAttr = CGM.filterFunctionTargetAttrs(TD); |
| 2391 | |
| 2392 | SmallVector<StringRef, 1> ReqFeatures; |
| 2393 | llvm::StringMap<bool> CalleeFeatureMap; |
| 2394 | CGM.getFunctionFeatureMap(CalleeFeatureMap, TargetDecl); |
| 2395 | |
| 2396 | for (const auto &F : ParsedAttr.Features) { |
| 2397 | if (F[0] == '+' && CalleeFeatureMap.lookup(F.substr(1))) |
| 2398 | ReqFeatures.push_back(StringRef(F).substr(1)); |
| 2399 | } |
| 2400 | |
| 2401 | for (const auto &F : CalleeFeatureMap) { |
| 2402 | |
| 2403 | if (F.getValue()) |
| 2404 | ReqFeatures.push_back(F.getKey()); |
| 2405 | } |
| 2406 | if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature)) |
| 2407 | CGM.getDiags().Report(E->getBeginLoc(), diag::err_function_needs_feature) |
| 2408 | << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) { |
| 2413 | if (!CGM.getCodeGenOpts().SanitizeStats) |
| 2414 | return; |
| 2415 | |
| 2416 | llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint()); |
| 2417 | IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation()); |
| 2418 | CGM.getSanStats().create(IRB, SSK); |
| 2419 | } |
| 2420 | |
| 2421 | llvm::Value * |
| 2422 | CodeGenFunction::FormResolverCondition(const MultiVersionResolverOption &RO) { |
| 2423 | llvm::Value *Condition = nullptr; |
| 2424 | |
| 2425 | if (!RO.Conditions.Architecture.empty()) |
| 2426 | Condition = EmitX86CpuIs(RO.Conditions.Architecture); |
| 2427 | |
| 2428 | if (!RO.Conditions.Features.empty()) { |
| 2429 | llvm::Value *FeatureCond = EmitX86CpuSupports(RO.Conditions.Features); |
| 2430 | Condition = |
| 2431 | Condition ? Builder.CreateAnd(Condition, FeatureCond) : FeatureCond; |
| 2432 | } |
| 2433 | return Condition; |
| 2434 | } |
| 2435 | |
| 2436 | static void CreateMultiVersionResolverReturn(CodeGenModule &CGM, |
| 2437 | llvm::Function *Resolver, |
| 2438 | CGBuilderTy &Builder, |
| 2439 | llvm::Function *FuncToReturn, |
| 2440 | bool SupportsIFunc) { |
| 2441 | if (SupportsIFunc) { |
| 2442 | Builder.CreateRet(FuncToReturn); |
| 2443 | return; |
| 2444 | } |
| 2445 | |
| 2446 | llvm::SmallVector<llvm::Value *, 10> Args; |
| 2447 | llvm::for_each(Resolver->args(), |
| 2448 | [&](llvm::Argument &Arg) { Args.push_back(&Arg); }); |
| 2449 | |
| 2450 | llvm::CallInst *Result = Builder.CreateCall(FuncToReturn, Args); |
| 2451 | Result->setTailCallKind(llvm::CallInst::TCK_MustTail); |
| 2452 | |
| 2453 | if (Resolver->getReturnType()->isVoidTy()) |
| 2454 | Builder.CreateRetVoid(); |
| 2455 | else |
| 2456 | Builder.CreateRet(Result); |
| 2457 | } |
| 2458 | |
| 2459 | void CodeGenFunction::EmitMultiVersionResolver( |
| 2460 | llvm::Function *Resolver, ArrayRef<MultiVersionResolverOption> Options) { |
| 2461 | (0) . __assert_fail ("(getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 || getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86_64) && \"Only implemented for x86 targets\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((getContext().getTargetInfo().getTriple().getArch() == |
| 2462 | (0) . __assert_fail ("(getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 || getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86_64) && \"Only implemented for x86 targets\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> llvm::Triple::x86 || |
| 2463 | (0) . __assert_fail ("(getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 || getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86_64) && \"Only implemented for x86 targets\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> getContext().getTargetInfo().getTriple().getArch() == |
| 2464 | (0) . __assert_fail ("(getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 || getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86_64) && \"Only implemented for x86 targets\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> llvm::Triple::x86_64) && |
| 2465 | (0) . __assert_fail ("(getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 || getContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86_64) && \"Only implemented for x86 targets\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2465, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only implemented for x86 targets"); |
| 2466 | |
| 2467 | bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc(); |
| 2468 | |
| 2469 | |
| 2470 | llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver); |
| 2471 | Builder.SetInsertPoint(CurBlock); |
| 2472 | EmitX86CpuInit(); |
| 2473 | |
| 2474 | for (const MultiVersionResolverOption &RO : Options) { |
| 2475 | Builder.SetInsertPoint(CurBlock); |
| 2476 | llvm::Value *Condition = FormResolverCondition(RO); |
| 2477 | |
| 2478 | |
| 2479 | if (!Condition) { |
| 2480 | (0) . __assert_fail ("&RO == Options.end() - 1 && \"Default or Generic case must be last\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2481, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(&RO == Options.end() - 1 && |
| 2481 | (0) . __assert_fail ("&RO == Options.end() - 1 && \"Default or Generic case must be last\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2481, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Default or Generic case must be last"); |
| 2482 | CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function, |
| 2483 | SupportsIFunc); |
| 2484 | return; |
| 2485 | } |
| 2486 | |
| 2487 | llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver); |
| 2488 | CGBuilderTy RetBuilder(*this, RetBlock); |
| 2489 | CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function, |
| 2490 | SupportsIFunc); |
| 2491 | CurBlock = createBasicBlock("resolver_else", Resolver); |
| 2492 | Builder.CreateCondBr(Condition, RetBlock, CurBlock); |
| 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | Builder.SetInsertPoint(CurBlock); |
| 2497 | llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); |
| 2498 | TrapCall->setDoesNotReturn(); |
| 2499 | TrapCall->setDoesNotThrow(); |
| 2500 | Builder.CreateUnreachable(); |
| 2501 | Builder.ClearInsertionPoint(); |
| 2502 | } |
| 2503 | |
| 2504 | |
| 2505 | |
| 2506 | |
| 2507 | |
| 2508 | |
| 2509 | |
| 2510 | void CodeGenFunction::EmitAlignmentAssumptionCheck( |
| 2511 | llvm::Value *Ptr, QualType Ty, SourceLocation Loc, |
| 2512 | SourceLocation SecondaryLoc, llvm::Value *Alignment, |
| 2513 | llvm::Value *OffsetValue, llvm::Value *TheCheck, |
| 2514 | llvm::Instruction *Assumption) { |
| 2515 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Assumption && isa<llvm::CallInst>(Assumption) && |
| 2516 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> cast<llvm::CallInst>(Assumption)->getCalledValue() == |
| 2517 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> llvm::Intrinsic::getDeclaration( |
| 2518 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Builder.GetInsertBlock()->getParent()->getParent(), |
| 2519 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> llvm::Intrinsic::assume) && |
| 2520 | (0) . __assert_fail ("Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm..Intrinsic..getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm..Intrinsic..assume) && \"Assumption should be a call to llvm.assume().\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Assumption should be a call to llvm.assume()."); |
| 2521 | (0) . __assert_fail ("&(Builder.GetInsertBlock()->back()) == Assumption && \"Assumption should be the last instruction of the basic block, \" \"since the basic block is still being generated.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2523, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(&(Builder.GetInsertBlock()->back()) == Assumption && |
| 2522 | (0) . __assert_fail ("&(Builder.GetInsertBlock()->back()) == Assumption && \"Assumption should be the last instruction of the basic block, \" \"since the basic block is still being generated.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2523, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Assumption should be the last instruction of the basic block, " |
| 2523 | (0) . __assert_fail ("&(Builder.GetInsertBlock()->back()) == Assumption && \"Assumption should be the last instruction of the basic block, \" \"since the basic block is still being generated.\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/CodeGenFunction.cpp", 2523, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "since the basic block is still being generated."); |
| 2524 | |
| 2525 | if (!SanOpts.has(SanitizerKind::Alignment)) |
| 2526 | return; |
| 2527 | |
| 2528 | |
| 2529 | |
| 2530 | if (Ty->getPointeeType().isVolatileQualified()) |
| 2531 | return; |
| 2532 | |
| 2533 | |
| 2534 | |
| 2535 | Assumption->removeFromParent(); |
| 2536 | |
| 2537 | { |
| 2538 | SanitizerScope SanScope(this); |
| 2539 | |
| 2540 | if (!OffsetValue) |
| 2541 | OffsetValue = Builder.getInt1(0); |
| 2542 | |
| 2543 | llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc), |
| 2544 | EmitCheckSourceLocation(SecondaryLoc), |
| 2545 | EmitCheckTypeDescriptor(Ty)}; |
| 2546 | llvm::Value *DynamicData[] = {EmitCheckValue(Ptr), |
| 2547 | EmitCheckValue(Alignment), |
| 2548 | EmitCheckValue(OffsetValue)}; |
| 2549 | EmitCheck({std::make_pair(TheCheck, SanitizerKind::Alignment)}, |
| 2550 | SanitizerHandler::AlignmentAssumption, StaticData, DynamicData); |
| 2551 | } |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | Builder.Insert(Assumption); |
| 2556 | |
| 2557 | } |
| 2558 | |
| 2559 | llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) { |
| 2560 | if (CGDebugInfo *DI = getDebugInfo()) |
| 2561 | return DI->SourceLocToDebugLoc(Location); |
| 2562 | |
| 2563 | return llvm::DebugLoc(); |
| 2564 | } |
| 2565 | |