Mercurial > hg > CbC > CbC_llvm
comparison clang/lib/CodeGen/CodeGenFunction.cpp @ 173:0572611fdcc8 llvm10 llvm12
reorgnization done
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 11:55:54 +0900 |
parents | 1d019706d866 |
children | 2e18cbf3894f |
comparison
equal
deleted
inserted
replaced
172:9fbae9c8bf63 | 173:0572611fdcc8 |
---|---|
113 // been "emitted" to the outside, thus, modifications are still sensible. | 113 // been "emitted" to the outside, thus, modifications are still sensible. |
114 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) | 114 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) |
115 OMPBuilder->finalize(); | 115 OMPBuilder->finalize(); |
116 } | 116 } |
117 | 117 |
118 // Map the LangOption for rounding mode into | |
119 // the corresponding enum in the IR. | |
120 static llvm::fp::RoundingMode ToConstrainedRoundingMD( | |
121 LangOptions::FPRoundingModeKind Kind) { | |
122 | |
123 switch (Kind) { | |
124 case LangOptions::FPR_ToNearest: return llvm::fp::rmToNearest; | |
125 case LangOptions::FPR_Downward: return llvm::fp::rmDownward; | |
126 case LangOptions::FPR_Upward: return llvm::fp::rmUpward; | |
127 case LangOptions::FPR_TowardZero: return llvm::fp::rmTowardZero; | |
128 case LangOptions::FPR_Dynamic: return llvm::fp::rmDynamic; | |
129 } | |
130 llvm_unreachable("Unsupported FP RoundingMode"); | |
131 } | |
132 | |
133 // Map the LangOption for exception behavior into | 118 // Map the LangOption for exception behavior into |
134 // the corresponding enum in the IR. | 119 // the corresponding enum in the IR. |
135 static llvm::fp::ExceptionBehavior ToConstrainedExceptMD( | 120 llvm::fp::ExceptionBehavior |
136 LangOptions::FPExceptionModeKind Kind) { | 121 clang::ToConstrainedExceptMD(LangOptions::FPExceptionModeKind Kind) { |
137 | 122 |
138 switch (Kind) { | 123 switch (Kind) { |
139 case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore; | 124 case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore; |
140 case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap; | 125 case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap; |
141 case LangOptions::FPE_Strict: return llvm::fp::ebStrict; | 126 case LangOptions::FPE_Strict: return llvm::fp::ebStrict; |
142 } | 127 } |
143 llvm_unreachable("Unsupported FP Exception Behavior"); | 128 llvm_unreachable("Unsupported FP Exception Behavior"); |
144 } | 129 } |
145 | 130 |
146 void CodeGenFunction::SetFPModel() { | 131 void CodeGenFunction::SetFPModel() { |
147 auto fpRoundingMode = ToConstrainedRoundingMD( | 132 llvm::RoundingMode RM = getLangOpts().getFPRoundingMode(); |
148 getLangOpts().getFPRoundingMode()); | |
149 auto fpExceptionBehavior = ToConstrainedExceptMD( | 133 auto fpExceptionBehavior = ToConstrainedExceptMD( |
150 getLangOpts().getFPExceptionMode()); | 134 getLangOpts().getFPExceptionMode()); |
151 | 135 |
152 if (fpExceptionBehavior == llvm::fp::ebIgnore && | 136 Builder.setDefaultConstrainedRounding(RM); |
153 fpRoundingMode == llvm::fp::rmToNearest) | 137 Builder.setDefaultConstrainedExcept(fpExceptionBehavior); |
154 // Constrained intrinsics are not used. | 138 Builder.setIsFPConstrained(fpExceptionBehavior != llvm::fp::ebIgnore || |
155 ; | 139 RM != llvm::RoundingMode::NearestTiesToEven); |
156 else { | |
157 Builder.setIsFPConstrained(true); | |
158 Builder.setDefaultConstrainedRounding(fpRoundingMode); | |
159 Builder.setDefaultConstrainedExcept(fpExceptionBehavior); | |
160 } | |
161 } | |
162 | |
163 CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T, | |
164 LValueBaseInfo *BaseInfo, | |
165 TBAAAccessInfo *TBAAInfo) { | |
166 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo, | |
167 /* forPointeeType= */ true); | |
168 } | |
169 | |
170 CharUnits CodeGenFunction::getNaturalTypeAlignment(QualType T, | |
171 LValueBaseInfo *BaseInfo, | |
172 TBAAAccessInfo *TBAAInfo, | |
173 bool forPointeeType) { | |
174 if (TBAAInfo) | |
175 *TBAAInfo = CGM.getTBAAAccessInfo(T); | |
176 | |
177 // Honor alignment typedef attributes even on incomplete types. | |
178 // We also honor them straight for C++ class types, even as pointees; | |
179 // there's an expressivity gap here. | |
180 if (auto TT = T->getAs<TypedefType>()) { | |
181 if (auto Align = TT->getDecl()->getMaxAlignment()) { | |
182 if (BaseInfo) | |
183 *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType); | |
184 return getContext().toCharUnitsFromBits(Align); | |
185 } | |
186 } | |
187 | |
188 if (BaseInfo) | |
189 *BaseInfo = LValueBaseInfo(AlignmentSource::Type); | |
190 | |
191 CharUnits Alignment; | |
192 if (T->isIncompleteType()) { | |
193 Alignment = CharUnits::One(); // Shouldn't be used, but pessimistic is best. | |
194 } else { | |
195 // For C++ class pointees, we don't know whether we're pointing at a | |
196 // base or a complete object, so we generally need to use the | |
197 // non-virtual alignment. | |
198 const CXXRecordDecl *RD; | |
199 if (forPointeeType && (RD = T->getAsCXXRecordDecl())) { | |
200 Alignment = CGM.getClassPointerAlignment(RD); | |
201 } else { | |
202 Alignment = getContext().getTypeAlignInChars(T); | |
203 if (T.getQualifiers().hasUnaligned()) | |
204 Alignment = CharUnits::One(); | |
205 } | |
206 | |
207 // Cap to the global maximum type alignment unless the alignment | |
208 // was somehow explicit on the type. | |
209 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) { | |
210 if (Alignment.getQuantity() > MaxAlign && | |
211 !getContext().isAlignmentRequired(T)) | |
212 Alignment = CharUnits::fromQuantity(MaxAlign); | |
213 } | |
214 } | |
215 return Alignment; | |
216 } | 140 } |
217 | 141 |
218 LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { | 142 LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { |
219 LValueBaseInfo BaseInfo; | 143 LValueBaseInfo BaseInfo; |
220 TBAAAccessInfo TBAAInfo; | 144 TBAAAccessInfo TBAAInfo; |
221 CharUnits Alignment = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo); | 145 CharUnits Alignment = CGM.getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo); |
222 return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo, | 146 return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo, |
223 TBAAInfo); | 147 TBAAInfo); |
224 } | 148 } |
225 | 149 |
226 /// Given a value of type T* that may not be to a complete object, | 150 /// Given a value of type T* that may not be to a complete object, |
227 /// construct an l-value with the natural pointee alignment of T. | 151 /// construct an l-value with the natural pointee alignment of T. |
228 LValue | 152 LValue |
229 CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) { | 153 CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) { |
230 LValueBaseInfo BaseInfo; | 154 LValueBaseInfo BaseInfo; |
231 TBAAAccessInfo TBAAInfo; | 155 TBAAAccessInfo TBAAInfo; |
232 CharUnits Align = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo, | 156 CharUnits Align = CGM.getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo, |
233 /* forPointeeType= */ true); | 157 /* forPointeeType= */ true); |
234 return MakeAddrLValue(Address(V, Align), T, BaseInfo, TBAAInfo); | 158 return MakeAddrLValue(Address(V, Align), T, BaseInfo, TBAAInfo); |
235 } | 159 } |
236 | 160 |
237 | 161 |
238 llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { | 162 llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
266 case Type::LValueReference: | 190 case Type::LValueReference: |
267 case Type::RValueReference: | 191 case Type::RValueReference: |
268 case Type::MemberPointer: | 192 case Type::MemberPointer: |
269 case Type::Vector: | 193 case Type::Vector: |
270 case Type::ExtVector: | 194 case Type::ExtVector: |
195 case Type::ConstantMatrix: | |
271 case Type::FunctionProto: | 196 case Type::FunctionProto: |
272 case Type::FunctionNoProto: | 197 case Type::FunctionNoProto: |
273 case Type::Enum: | 198 case Type::Enum: |
274 case Type::ObjCObjectPointer: | 199 case Type::ObjCObjectPointer: |
275 case Type::Pipe: | 200 case Type::Pipe: |
201 case Type::ExtInt: | |
276 return TEK_Scalar; | 202 return TEK_Scalar; |
277 | 203 |
278 // Complexes. | 204 // Complexes. |
279 case Type::Complex: | 205 case Type::Complex: |
280 return TEK_Complex; | 206 return TEK_Complex; |
493 } | 419 } |
494 | 420 |
495 // Scan function arguments for vector width. | 421 // Scan function arguments for vector width. |
496 for (llvm::Argument &A : CurFn->args()) | 422 for (llvm::Argument &A : CurFn->args()) |
497 if (auto *VT = dyn_cast<llvm::VectorType>(A.getType())) | 423 if (auto *VT = dyn_cast<llvm::VectorType>(A.getType())) |
498 LargestVectorWidth = std::max((uint64_t)LargestVectorWidth, | 424 LargestVectorWidth = |
499 VT->getPrimitiveSizeInBits().getFixedSize()); | 425 std::max((uint64_t)LargestVectorWidth, |
426 VT->getPrimitiveSizeInBits().getKnownMinSize()); | |
500 | 427 |
501 // Update vector width based on return type. | 428 // Update vector width based on return type. |
502 if (auto *VT = dyn_cast<llvm::VectorType>(CurFn->getReturnType())) | 429 if (auto *VT = dyn_cast<llvm::VectorType>(CurFn->getReturnType())) |
503 LargestVectorWidth = std::max((uint64_t)LargestVectorWidth, | 430 LargestVectorWidth = |
504 VT->getPrimitiveSizeInBits().getFixedSize()); | 431 std::max((uint64_t)LargestVectorWidth, |
432 VT->getPrimitiveSizeInBits().getKnownMinSize()); | |
505 | 433 |
506 // Add the required-vector-width attribute. This contains the max width from: | 434 // Add the required-vector-width attribute. This contains the max width from: |
507 // 1. min-vector-width attribute used in the source program. | 435 // 1. min-vector-width attribute used in the source program. |
508 // 2. Any builtins used that have a vector width specified. | 436 // 2. Any builtins used that have a vector width specified. |
509 // 3. Values passed in and out of inline assembly. | 437 // 3. Values passed in and out of inline assembly. |
806 if (const auto *FD = dyn_cast<FunctionDecl>(D)) | 734 if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
807 if (FD->getBody() && | 735 if (FD->getBody() && |
808 FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) | 736 FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) |
809 SanOpts.Mask &= ~SanitizerKind::Null; | 737 SanOpts.Mask &= ~SanitizerKind::Null; |
810 | 738 |
811 if (D) { | 739 // Apply xray attributes to the function (as a string, for now) |
812 // Apply xray attributes to the function (as a string, for now) | 740 if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) { |
813 if (const auto *XRayAttr = D->getAttr<XRayInstrumentAttr>()) { | 741 if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has( |
814 if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has( | 742 XRayInstrKind::FunctionEntry) || |
815 XRayInstrKind::FunctionEntry) || | 743 CGM.getCodeGenOpts().XRayInstrumentationBundle.has( |
816 CGM.getCodeGenOpts().XRayInstrumentationBundle.has( | 744 XRayInstrKind::FunctionExit)) { |
817 XRayInstrKind::FunctionExit)) { | 745 if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) |
818 if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) | 746 Fn->addFnAttr("function-instrument", "xray-always"); |
819 Fn->addFnAttr("function-instrument", "xray-always"); | 747 if (XRayAttr->neverXRayInstrument()) |
820 if (XRayAttr->neverXRayInstrument()) | 748 Fn->addFnAttr("function-instrument", "xray-never"); |
821 Fn->addFnAttr("function-instrument", "xray-never"); | 749 if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>()) |
822 if (const auto *LogArgs = D->getAttr<XRayLogArgsAttr>()) | 750 if (ShouldXRayInstrumentFunction()) |
823 if (ShouldXRayInstrumentFunction()) | 751 Fn->addFnAttr("xray-log-args", |
824 Fn->addFnAttr("xray-log-args", | 752 llvm::utostr(LogArgs->getArgumentCount())); |
825 llvm::utostr(LogArgs->getArgumentCount())); | 753 } |
826 } | 754 } else { |
827 } else { | 755 if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc)) |
828 if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc)) | 756 Fn->addFnAttr( |
829 Fn->addFnAttr( | 757 "xray-instruction-threshold", |
830 "xray-instruction-threshold", | 758 llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold)); |
831 llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold)); | 759 } |
832 } | 760 |
833 | 761 if (ShouldXRayInstrumentFunction()) { |
834 if (ShouldXRayInstrumentFunction()) { | 762 if (CGM.getCodeGenOpts().XRayIgnoreLoops) |
835 if (CGM.getCodeGenOpts().XRayIgnoreLoops) | 763 Fn->addFnAttr("xray-ignore-loops"); |
836 Fn->addFnAttr("xray-ignore-loops"); | 764 |
837 | 765 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has( |
838 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has( | 766 XRayInstrKind::FunctionExit)) |
839 XRayInstrKind::FunctionExit)) | 767 Fn->addFnAttr("xray-skip-exit"); |
840 Fn->addFnAttr("xray-skip-exit"); | 768 |
841 | 769 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has( |
842 if (!CGM.getCodeGenOpts().XRayInstrumentationBundle.has( | 770 XRayInstrKind::FunctionEntry)) |
843 XRayInstrKind::FunctionEntry)) | 771 Fn->addFnAttr("xray-skip-entry"); |
844 Fn->addFnAttr("xray-skip-entry"); | 772 } |
845 } | 773 |
846 | 774 unsigned Count, Offset; |
847 unsigned Count, Offset; | 775 if (const auto *Attr = |
848 if (const auto *Attr = D->getAttr<PatchableFunctionEntryAttr>()) { | 776 D ? D->getAttr<PatchableFunctionEntryAttr>() : nullptr) { |
849 Count = Attr->getCount(); | 777 Count = Attr->getCount(); |
850 Offset = Attr->getOffset(); | 778 Offset = Attr->getOffset(); |
851 } else { | 779 } else { |
852 Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount; | 780 Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount; |
853 Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset; | 781 Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset; |
854 } | 782 } |
855 if (Count && Offset <= Count) { | 783 if (Count && Offset <= Count) { |
856 Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset)); | 784 Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset)); |
857 if (Offset) | 785 if (Offset) |
858 Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset)); | 786 Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset)); |
859 } | |
860 } | 787 } |
861 | 788 |
862 // Add no-jump-tables value. | 789 // Add no-jump-tables value. |
863 Fn->addFnAttr("no-jump-tables", | 790 Fn->addFnAttr("no-jump-tables", |
864 llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); | 791 llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); |
916 } | 843 } |
917 | 844 |
918 // If we're in C++ mode and the function name is "main", it is guaranteed | 845 // If we're in C++ mode and the function name is "main", it is guaranteed |
919 // to be norecurse by the standard (3.6.1.3 "The function main shall not be | 846 // to be norecurse by the standard (3.6.1.3 "The function main shall not be |
920 // used within a program"). | 847 // used within a program"). |
921 if (getLangOpts().CPlusPlus) | 848 // |
922 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) | 849 // OpenCL C 2.0 v2.2-11 s6.9.i: |
923 if (FD->isMain()) | 850 // Recursion is not supported. |
924 Fn->addFnAttr(llvm::Attribute::NoRecurse); | 851 // |
925 | 852 // SYCL v1.2.1 s3.10: |
926 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) | 853 // kernels cannot include RTTI information, exception classes, |
854 // recursive code, virtual functions or make use of C++ libraries that | |
855 // are not compiled for the device. | |
856 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { | |
857 if ((getLangOpts().CPlusPlus && FD->isMain()) || getLangOpts().OpenCL || | |
858 getLangOpts().SYCLIsDevice || | |
859 (getLangOpts().CUDA && FD->hasAttr<CUDAGlobalAttr>())) | |
860 Fn->addFnAttr(llvm::Attribute::NoRecurse); | |
861 } | |
862 | |
863 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { | |
864 Builder.setIsFPConstrained(FD->usesFPIntrin()); | |
927 if (FD->usesFPIntrin()) | 865 if (FD->usesFPIntrin()) |
928 Fn->addFnAttr(llvm::Attribute::StrictFP); | 866 Fn->addFnAttr(llvm::Attribute::StrictFP); |
867 } | |
929 | 868 |
930 // If a custom alignment is used, force realigning to this alignment on | 869 // If a custom alignment is used, force realigning to this alignment on |
931 // any main function which certainly will need it. | 870 // any main function which certainly will need it. |
932 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) | 871 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
933 if ((FD->isMain() || FD->isMSVCRTEntryPoint()) && | 872 if ((FD->isMain() || FD->isMSVCRTEntryPoint()) && |
1048 llvm::Function::arg_iterator EI = CurFn->arg_end(); | 987 llvm::Function::arg_iterator EI = CurFn->arg_end(); |
1049 --EI; | 988 --EI; |
1050 llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx); | 989 llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx); |
1051 ReturnValuePointer = Address(Addr, getPointerAlign()); | 990 ReturnValuePointer = Address(Addr, getPointerAlign()); |
1052 Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result"); | 991 Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result"); |
1053 ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy)); | 992 ReturnValue = Address(Addr, CGM.getNaturalTypeAlignment(RetTy)); |
1054 } else { | 993 } else { |
1055 ReturnValue = CreateIRTemp(RetTy, "retval"); | 994 ReturnValue = CreateIRTemp(RetTy, "retval"); |
1056 | 995 |
1057 // Tell the epilog emitter to autorelease the result. We do this | 996 // Tell the epilog emitter to autorelease the result. We do this |
1058 // now so that various specialized functions can suppress it | 997 // now so that various specialized functions can suppress it |
2005 // These types are never variably-modified. | 1944 // These types are never variably-modified. |
2006 case Type::Builtin: | 1945 case Type::Builtin: |
2007 case Type::Complex: | 1946 case Type::Complex: |
2008 case Type::Vector: | 1947 case Type::Vector: |
2009 case Type::ExtVector: | 1948 case Type::ExtVector: |
1949 case Type::ConstantMatrix: | |
2010 case Type::Record: | 1950 case Type::Record: |
2011 case Type::Enum: | 1951 case Type::Enum: |
2012 case Type::Elaborated: | 1952 case Type::Elaborated: |
2013 case Type::TemplateSpecialization: | 1953 case Type::TemplateSpecialization: |
2014 case Type::ObjCTypeParam: | 1954 case Type::ObjCTypeParam: |
2015 case Type::ObjCObject: | 1955 case Type::ObjCObject: |
2016 case Type::ObjCInterface: | 1956 case Type::ObjCInterface: |
2017 case Type::ObjCObjectPointer: | 1957 case Type::ObjCObjectPointer: |
1958 case Type::ExtInt: | |
2018 llvm_unreachable("type class is never variably-modified!"); | 1959 llvm_unreachable("type class is never variably-modified!"); |
2019 | 1960 |
2020 case Type::Adjusted: | 1961 case Type::Adjusted: |
2021 type = cast<AdjustedType>(ty)->getAdjustedType(); | 1962 type = cast<AdjustedType>(ty)->getAdjustedType(); |
2022 break; | 1963 break; |
2168 | 2109 |
2169 // In theory, we could try to duplicate the peepholes now, but whatever. | 2110 // In theory, we could try to duplicate the peepholes now, but whatever. |
2170 protection.Inst->eraseFromParent(); | 2111 protection.Inst->eraseFromParent(); |
2171 } | 2112 } |
2172 | 2113 |
2173 void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, | 2114 void CodeGenFunction::emitAlignmentAssumption(llvm::Value *PtrValue, |
2174 QualType Ty, SourceLocation Loc, | 2115 QualType Ty, SourceLocation Loc, |
2175 SourceLocation AssumptionLoc, | 2116 SourceLocation AssumptionLoc, |
2176 llvm::Value *Alignment, | 2117 llvm::Value *Alignment, |
2177 llvm::Value *OffsetValue) { | 2118 llvm::Value *OffsetValue) { |
2178 llvm::Value *TheCheck; | 2119 llvm::Value *TheCheck; |
2179 llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( | 2120 llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( |
2180 CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck); | 2121 CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck); |
2181 if (SanOpts.has(SanitizerKind::Alignment)) { | 2122 if (SanOpts.has(SanitizerKind::Alignment)) { |
2182 EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, | 2123 emitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, |
2183 OffsetValue, TheCheck, Assumption); | 2124 OffsetValue, TheCheck, Assumption); |
2184 } | 2125 } |
2185 } | 2126 } |
2186 | 2127 |
2187 void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, | 2128 void CodeGenFunction::emitAlignmentAssumption(llvm::Value *PtrValue, |
2188 const Expr *E, | 2129 const Expr *E, |
2189 SourceLocation AssumptionLoc, | 2130 SourceLocation AssumptionLoc, |
2190 llvm::Value *Alignment, | 2131 llvm::Value *Alignment, |
2191 llvm::Value *OffsetValue) { | 2132 llvm::Value *OffsetValue) { |
2192 if (auto *CE = dyn_cast<CastExpr>(E)) | 2133 if (auto *CE = dyn_cast<CastExpr>(E)) |
2193 E = CE->getSubExprAsWritten(); | 2134 E = CE->getSubExprAsWritten(); |
2194 QualType Ty = E->getType(); | 2135 QualType Ty = E->getType(); |
2195 SourceLocation Loc = E->getExprLoc(); | 2136 SourceLocation Loc = E->getExprLoc(); |
2196 | 2137 |
2197 EmitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment, | 2138 emitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment, |
2198 OffsetValue); | 2139 OffsetValue); |
2199 } | 2140 } |
2200 | 2141 |
2201 llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn, | 2142 llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn, |
2202 llvm::Value *AnnotatedVal, | 2143 llvm::Value *AnnotatedVal, |
2346 ParsedTargetAttr ParsedAttr = | 2287 ParsedTargetAttr ParsedAttr = |
2347 CGM.getContext().filterFunctionTargetAttrs(TD); | 2288 CGM.getContext().filterFunctionTargetAttrs(TD); |
2348 | 2289 |
2349 SmallVector<StringRef, 1> ReqFeatures; | 2290 SmallVector<StringRef, 1> ReqFeatures; |
2350 llvm::StringMap<bool> CalleeFeatureMap; | 2291 llvm::StringMap<bool> CalleeFeatureMap; |
2351 CGM.getContext().getFunctionFeatureMap(CalleeFeatureMap, | 2292 CGM.getContext().getFunctionFeatureMap(CalleeFeatureMap, TargetDecl); |
2352 GlobalDecl(TargetDecl)); | |
2353 | 2293 |
2354 for (const auto &F : ParsedAttr.Features) { | 2294 for (const auto &F : ParsedAttr.Features) { |
2355 if (F[0] == '+' && CalleeFeatureMap.lookup(F.substr(1))) | 2295 if (F[0] == '+' && CalleeFeatureMap.lookup(F.substr(1))) |
2356 ReqFeatures.push_back(StringRef(F).substr(1)); | 2296 ReqFeatures.push_back(StringRef(F).substr(1)); |
2357 } | 2297 } |
2460 // alignment has failed. | 2400 // alignment has failed. |
2461 // SecondaryLoc - if present (will be present if sufficiently different from | 2401 // SecondaryLoc - if present (will be present if sufficiently different from |
2462 // Loc), the diagnostic will additionally point a "Note:" to this location. | 2402 // Loc), the diagnostic will additionally point a "Note:" to this location. |
2463 // It should be the location where the __attribute__((assume_aligned)) | 2403 // It should be the location where the __attribute__((assume_aligned)) |
2464 // was written e.g. | 2404 // was written e.g. |
2465 void CodeGenFunction::EmitAlignmentAssumptionCheck( | 2405 void CodeGenFunction::emitAlignmentAssumptionCheck( |
2466 llvm::Value *Ptr, QualType Ty, SourceLocation Loc, | 2406 llvm::Value *Ptr, QualType Ty, SourceLocation Loc, |
2467 SourceLocation SecondaryLoc, llvm::Value *Alignment, | 2407 SourceLocation SecondaryLoc, llvm::Value *Alignment, |
2468 llvm::Value *OffsetValue, llvm::Value *TheCheck, | 2408 llvm::Value *OffsetValue, llvm::Value *TheCheck, |
2469 llvm::Instruction *Assumption) { | 2409 llvm::Instruction *Assumption) { |
2470 assert(Assumption && isa<llvm::CallInst>(Assumption) && | 2410 assert(Assumption && isa<llvm::CallInst>(Assumption) && |
2471 cast<llvm::CallInst>(Assumption)->getCalledValue() == | 2411 cast<llvm::CallInst>(Assumption)->getCalledOperand() == |
2472 llvm::Intrinsic::getDeclaration( | 2412 llvm::Intrinsic::getDeclaration( |
2473 Builder.GetInsertBlock()->getParent()->getParent(), | 2413 Builder.GetInsertBlock()->getParent()->getParent(), |
2474 llvm::Intrinsic::assume) && | 2414 llvm::Intrinsic::assume) && |
2475 "Assumption should be a call to llvm.assume()."); | 2415 "Assumption should be a call to llvm.assume()."); |
2476 assert(&(Builder.GetInsertBlock()->back()) == Assumption && | 2416 assert(&(Builder.GetInsertBlock()->back()) == Assumption && |