aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenFunction.cpp
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2020-06-11 18:09:36 -0400
committerJohn McCall <rjmccall@apple.com>2020-06-11 18:16:41 -0400
commit7fac1acc617113b7a3276ee0f0664bedca978292 (patch)
tree1397f7236e9788268c5151971a70a60a8b8113cc /clang/lib/CodeGen/CodeGenFunction.cpp
parenta98d618f6e5f6429378ac1474b6df7ce1878061d (diff)
downloadllvm-7fac1acc617113b7a3276ee0f0664bedca978292.zip
llvm-7fac1acc617113b7a3276ee0f0664bedca978292.tar.gz
llvm-7fac1acc617113b7a3276ee0f0664bedca978292.tar.bz2
Set the LLVM FP optimization flags conservatively.
Functions can have local pragmas that override the global settings. We set the flags eagerly based on global settings, but if we emit an expression under the influence of a pragma, we clear the appropriate flags from the function. In order to avoid doing a ton of redundant work whenever we emit an FP expression, configure the IRBuilder to default to global settings, and only reconfigure it when we see an FP expression that's not using the global settings. Patch by Michele Scandale! https://reviews.llvm.org/D80462
Diffstat (limited to 'clang/lib/CodeGen/CodeGenFunction.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenFunction.cpp54
1 files changed, 50 insertions, 4 deletions
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 0fa795d..24fcd72 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -65,13 +65,14 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
: CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(),
CGBuilderInserterTy(this)),
- SanOpts(CGM.getLangOpts().Sanitize), DebugInfo(CGM.getModuleDebugInfo()),
- PGO(cgm), ShouldEmitLifetimeMarkers(shouldEmitLifetimeMarkers(
- CGM.getCodeGenOpts(), CGM.getLangOpts())) {
+ SanOpts(CGM.getLangOpts().Sanitize), CurFPFeatures(CGM.getLangOpts()),
+ DebugInfo(CGM.getModuleDebugInfo()), PGO(cgm),
+ ShouldEmitLifetimeMarkers(
+ shouldEmitLifetimeMarkers(CGM.getCodeGenOpts(), CGM.getLangOpts())) {
if (!suppressNewContext)
CGM.getCXXABI().getMangleContext().startNewFunction();
- SetFastMathFlags(FPOptions(CGM.getLangOpts()));
+ SetFastMathFlags(CurFPFeatures);
SetFPModel();
}
@@ -132,6 +133,51 @@ void CodeGenFunction::SetFastMathFlags(FPOptions FPFeatures) {
Builder.setFastMathFlags(FMF);
}
+CodeGenFunction::CGFPOptionsRAII::CGFPOptionsRAII(CodeGenFunction &CGF,
+ FPOptions FPFeatures)
+ : CGF(CGF), OldFPFeatures(CGF.CurFPFeatures) {
+ CGF.CurFPFeatures = FPFeatures;
+
+ if (OldFPFeatures == FPFeatures)
+ return;
+
+ FMFGuard.emplace(CGF.Builder);
+
+ auto NewRoundingBehavior = FPFeatures.getRoundingMode();
+ CGF.Builder.setDefaultConstrainedRounding(NewRoundingBehavior);
+ auto NewExceptionBehavior =
+ ToConstrainedExceptMD(FPFeatures.getExceptionMode());
+ CGF.Builder.setDefaultConstrainedExcept(NewExceptionBehavior);
+
+ CGF.SetFastMathFlags(FPFeatures);
+
+ assert((CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() ||
+ isa<CXXConstructorDecl>(CGF.CurFuncDecl) ||
+ isa<CXXDestructorDecl>(CGF.CurFuncDecl) ||
+ (NewExceptionBehavior == llvm::fp::ebIgnore &&
+ NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) &&
+ "FPConstrained should be enabled on entire function");
+
+ auto mergeFnAttrValue = [&](StringRef Name, bool Value) {
+ auto OldValue =
+ CGF.CurFn->getFnAttribute(Name).getValueAsString() == "true";
+ auto NewValue = OldValue & Value;
+ if (OldValue != NewValue)
+ CGF.CurFn->addFnAttr(Name, llvm::toStringRef(NewValue));
+ };
+ mergeFnAttrValue("no-infs-fp-math", FPFeatures.noHonorInfs());
+ mergeFnAttrValue("no-nans-fp-math", FPFeatures.noHonorNaNs());
+ mergeFnAttrValue("no-signed-zeros-fp-math", FPFeatures.noSignedZeros());
+ mergeFnAttrValue(
+ "unsafe-fp-math",
+ FPFeatures.allowAssociativeMath() && FPFeatures.allowReciprocalMath() &&
+ FPFeatures.allowApproximateFunctions() && FPFeatures.noSignedZeros());
+}
+
+CodeGenFunction::CGFPOptionsRAII::~CGFPOptionsRAII() {
+ CGF.CurFPFeatures = OldFPFeatures;
+}
+
LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) {
LValueBaseInfo BaseInfo;
TBAAAccessInfo TBAAInfo;