llvm.org GIT mirror llvm / 0146120
Stop resetting NoFramePointerElim in TargetMachine::resetTargetOptions. This is part of the work to remove TargetMachine::resetTargetOptions. In this patch, instead of updating global variable NoFramePointerElim in resetTargetOptions, its use in DisableFramePointerElim is replaced with a call to TargetFrameLowering::noFramePointerElim. This function determines on a per-function basis if frame pointer elimination should be disabled. There is no change in functionality except that cl:opt option "disable-fp-elim" can now override function attribute "no-frame-pointer-elim". git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238080 91177308-0d34-0410-b5e6-96231b3b80d8 Akira Hatanaka 5 years ago
15 changed file(s) with 112 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
229229 TargetOptions Options;
230230 Options.LessPreciseFPMADOption = EnableFPMAD;
231231 Options.NoFramePointerElim = DisableFPElim;
232 Options.NoFramePointerElimOverride = DisableFPElim.getNumOccurrences() > 0;
232233 Options.AllowFPOpFusion = FuseFPOps;
233234 Options.UnsafeFPMath = EnableUnsafeFPMath;
234235 Options.NoInfsFPMath = EnableNoInfsFPMath;
172172 return false;
173173 }
174174
175 /// Return true if the target needs to disable frame pointer elimination.
176 virtual bool noFramePointerElim(const MachineFunction &MF) const;
177
175178 /// hasFP - Return true if the specified function should have a dedicated
176179 /// frame pointer register. For most targets this is true only if the function
177180 /// has variable sized allocas or if frame pointer elimination is disabled.
1919
2020 namespace llvm {
2121 class MachineFunction;
22 class Module;
2223 class StringRef;
2324
2425 namespace FloatABI {
5960 public:
6061 TargetOptions()
6162 : PrintMachineCode(false), NoFramePointerElim(false),
63 NoFramePointerElimOverride(false),
6264 LessPreciseFPMADOption(false), UnsafeFPMath(false),
6365 NoInfsFPMath(false), NoNaNsFPMath(false),
6466 HonorSignDependentRoundingFPMathOption(false),
8284 /// specified on the command line. If the target supports the frame pointer
8385 /// elimination optimization, this option should disable it.
8486 unsigned NoFramePointerElim : 1;
87
88 /// This flag is true when "disable-fp-elim" appeared on the command line.
89 unsigned NoFramePointerElimOverride : 1;
8590
8691 /// DisableFramePointerElim - This returns true if frame pointer elimination
8792 /// optimization should be disabled for the given machine function.
221226 MCTargetOptions MCOptions;
222227 };
223228
224 /// \brief Set function attributes of functions in Module M based on CPU and
225 /// Features.
226 void setFunctionAttributes(StringRef CPU, StringRef Features, Module &M);
229 /// \brief Set function attributes of functions in Module M based on CPU,
230 /// Features, and Options.
231 /// If AlwaysRecordAttrs is true, it will always record the function attributes
232 /// in Options regardless of whether those attributes were specified on the
233 /// tool's command line.
234 void setFunctionAttributes(StringRef CPU, StringRef Features,
235 const TargetOptions &Options, Module &M,
236 bool AlwaysRecordAttrs);
227237
228238 // Comparison operators:
229239
1313 #include "llvm/Target/TargetFrameLowering.h"
1414 #include "llvm/CodeGen/MachineFrameInfo.h"
1515 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/IR/Function.h"
1617 #include "llvm/Target/TargetRegisterInfo.h"
1718 #include "llvm/Target/TargetSubtargetInfo.h"
1819 #include
1920 using namespace llvm;
2021
2122 TargetFrameLowering::~TargetFrameLowering() {
23 }
24
25 /// The default implementation just looks at attribute "no-frame-pointer-elim".
26 bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
27 auto Attr = MF.getFunction()->getFnAttribute("no-frame-pointer-elim");
28 return Attr.getValueAsString() == "true";
2229 }
2330
2431 /// getFrameIndexOffset - Returns the displacement from the frame register to
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/IR/Function.h"
14 #include "llvm/IR/Module.h"
1415 #include "llvm/CodeGen/MachineFrameInfo.h"
1516 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/Target/TargetFrameLowering.h"
1618 #include "llvm/Target/TargetOptions.h"
19 #include "llvm/Target/TargetSubtargetInfo.h"
1720 using namespace llvm;
1821
1922 /// DisableFramePointerElim - This returns true if frame pointer elimination
2023 /// optimization should be disabled for the given machine function.
2124 bool TargetOptions::DisableFramePointerElim(const MachineFunction &MF) const {
22 // Check to see if we should eliminate non-leaf frame pointers and then
23 // check to see if we should eliminate all frame pointers.
24 if (MF.getFunction()->hasFnAttribute("no-frame-pointer-elim-non-leaf") &&
25 !NoFramePointerElim) {
26 const MachineFrameInfo *MFI = MF.getFrameInfo();
27 return MFI->hasCalls();
28 }
25 // Check to see if we should eliminate all frame pointers.
26 if (MF.getSubtarget().getFrameLowering()->noFramePointerElim(MF))
27 return true;
2928
30 return NoFramePointerElim;
29 // Check to see if we should eliminate non-leaf frame pointers.
30 if (MF.getFunction()->hasFnAttribute("no-frame-pointer-elim-non-leaf"))
31 return MF.getFrameInfo()->hasCalls();
32
33 return false;
3134 }
3235
3336 /// LessPreciseFPMAD - This flag return true when -enable-fp-mad option
5255 }
5356
5457
55 void llvm::setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
58 void llvm::setFunctionAttributes(StringRef CPU, StringRef Features,
59 const TargetOptions &Options, Module &M,
60 bool AlwaysRecordAttrs) {
5661 for (auto &F : M) {
5762 auto &Ctx = F.getContext();
5863 AttributeSet Attrs = F.getAttributes(), NewAttrs;
6570 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
6671 "target-features", Features);
6772
73 if (Options.NoFramePointerElimOverride || AlwaysRecordAttrs)
74 NewAttrs = NewAttrs.addAttribute(
75 Ctx, AttributeSet::FunctionIndex, "no-frame-pointer-elim",
76 Options.NoFramePointerElim ? "true" : "false");
77
6878 // Let NewAttrs override Attrs.
6979 NewAttrs = Attrs.addAttributes(Ctx, AttributeSet::FunctionIndex, NewAttrs);
7080 F.setAttributes(NewAttrs);
178178 TargetOptions targetOptions;
179179 targetOptions.NoFramePointerElim = options.NoFramePointerElim;
180180 targetOptions.EnableFastISel = options.EnableFastISel;
181 std::unique_ptr Mod(unwrap(M));
182
183 if (Mod)
184 // Set function attribute "no-frame-pointer-elim" based on
185 // NoFramePointerElim.
186 setFunctionAttributes(/* CPU */ "", /* Features */ "", targetOptions, *Mod,
187 /* AlwaysRecordAttrs */ true);
181188
182189 std::string Error;
183 EngineBuilder builder(std::unique_ptr(unwrap(M)));
190 EngineBuilder builder(std::move(Mod));
184191 builder.setEngineKind(EngineKind::JIT)
185192 .setErrorStr(&Error)
186193 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
30643064 namespace llvm {
30653065 FastISel *ARM::createFastISel(FunctionLoweringInfo &funcInfo,
30663066 const TargetLibraryInfo *libInfo) {
3067 const TargetMachine &TM = funcInfo.MF->getTarget();
3068 const ARMSubtarget &STI =
3069 static_cast(funcInfo.MF->getSubtarget());
3070 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
3071 bool UseFastISel = false;
3072 UseFastISel |= STI.isTargetMachO() && !STI.isThumb1Only();
3073 UseFastISel |= STI.isTargetLinux() && !STI.isThumb();
3074 UseFastISel |= STI.isTargetNaCl() && !STI.isThumb();
3075
3076 if (UseFastISel) {
3077 // iOS always has a FP for backtracking, force other targets
3078 // to keep their FP when doing FastISel. The emitted code is
3079 // currently superior, and in cases like test-suite's lencod
3080 // FastISel isn't quite correct when FP is eliminated.
3081 TM.Options.NoFramePointerElim = true;
3067 if (funcInfo.MF->getSubtarget().useFastISel())
30823068 return new ARMFastISel(funcInfo, libInfo);
3083 }
3069
30843070 return nullptr;
30853071 }
30863072 }
4141 ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
4242 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
4343 STI(sti) {}
44
45 bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
46 // iOS always has a FP for backtracking, force other targets to keep their FP
47 // when doing FastISel. The emitted code is currently superior, and in cases
48 // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
49 return TargetFrameLowering::noFramePointerElim(MF) ||
50 MF.getSubtarget().useFastISel();
51 }
4452
4553 /// hasFP - Return true if the specified function should have a dedicated frame
4654 /// pointer register. This is true if the function has variable sized allocas
4242 const std::vector &CSI,
4343 const TargetRegisterInfo *TRI) const override;
4444
45 bool noFramePointerElim(const MachineFunction &MF) const override;
46
4547 bool hasFP(const MachineFunction &MF) const override;
4648 bool hasReservedCallFrame(const MachineFunction &MF) const override;
4749 bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override;
352352 return UseMovt && (isTargetWindows() ||
353353 !MF.getFunction()->hasFnAttribute(Attribute::MinSize));
354354 }
355
356 bool ARMSubtarget::useFastISel() const {
357 // Thumb2 support on iOS; ARM support on iOS, Linux and NaCl.
358 return TM.Options.EnableFastISel &&
359 ((isTargetMachO() && !isThumb1Only()) ||
360 (isTargetLinux() && !isThumb()) || (isTargetNaCl() && !isThumb()));
361 }
449449 /// symbol.
450450 bool GVIsIndirectSymbol(const GlobalValue *GV, Reloc::Model RelocM) const;
451451
452 /// True if fast-isel is used.
453 bool useFastISel() const;
452454 };
453455 } // End llvm namespace
454456
6565 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
6666 } while (0)
6767
68 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
6968 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
7069 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
7170 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
0 ; RUN: llc < %s -mtriple armv7-none-linux-gnueabi -O1 | FileCheck %s --check-prefix=DISABLE-FP-ELIM
1 ; RUN: llc < %s -mtriple armv7-none-linux-gnueabi -disable-fp-elim -O1 | FileCheck %s --check-prefix=DISABLE-FP-ELIM
2 ; RUN: llc < %s -mtriple armv7-none-linux-gnueabi -disable-fp-elim=false -O1 | FileCheck %s --check-prefix=ENABLE-FP-ELIM
3 ; RUN: llc < %s -mtriple armv7-none-linux-gnueabi -disable-fp-elim=false -O0 | FileCheck %s --check-prefix=DISABLE-FP-ELIM
4
5 ; Check that command line option "-disable-fp-elim" overrides function attribute
6 ; "no-frame-pointer-elim". Also, check frame pointer elimination is disabled
7 ; when fast-isel is used.
8
9 ; ENABLE-FP-ELIM-NOT: .setfp
10 ; DISABLE-FP-ELIM: .setfp r11, sp
11
12 define i32 @foo1(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) #0 {
13 entry:
14 %call = tail call i32 @foo2(i32 %a)
15 %add = add i32 %c, %b
16 %add1 = add i32 %add, %d
17 %add2 = add i32 %add1, %e
18 %add3 = add i32 %add2, %call
19 ret i32 %add3
20 }
21
22 declare i32 @foo2(i32)
23
24 attributes #0 = { nounwind "no-frame-pointer-elim"="true" }
303303 if (const DataLayout *DL = Target->getDataLayout())
304304 M->setDataLayout(*DL);
305305
306 // Override function attributes based on CPUStr and FeaturesStr.
307 setFunctionAttributes(CPUStr, FeaturesStr, *M);
306 // Override function attributes based on CPUStr, FeaturesStr, and Options.
307 // Pass AlwaysRecordAttrs=false as we want to override an attribute only when
308 // the corresponding cl::opt has been provided on llc's command line.
309 setFunctionAttributes(CPUStr, FeaturesStr, Options, *M,
310 /* AlwaysRecordAttrs */ false);
308311
309312 if (RelaxAll.getNumOccurrences() > 0 &&
310313 FileType != TargetMachine::CGFT_ObjectFile)
264264
265265 // Returns the TargetMachine instance or zero if no triple is provided.
266266 static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
267 StringRef FeaturesStr) {
267 StringRef FeaturesStr,
268 const TargetOptions &Options) {
268269 std::string Error;
269270 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
270271 Error);
274275 }
275276
276277 return TheTarget->createTargetMachine(TheTriple.getTriple(),
277 CPUStr, FeaturesStr,
278 InitTargetOptionsFromCodeGenFlags(),
278 CPUStr, FeaturesStr, Options,
279279 RelocModel, CMModel,
280280 GetCodeGenOptLevel());
281281 }
385385 Triple ModuleTriple(M->getTargetTriple());
386386 std::string CPUStr, FeaturesStr;
387387 TargetMachine *Machine = nullptr;
388 const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
388389
389390 if (ModuleTriple.getArch()) {
390391 CPUStr = getCPUStr();
391392 FeaturesStr = getFeaturesStr();
392 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr);
393 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
393394 }
394395
395396 std::unique_ptr TM(Machine);
396397
397 // Override function attributes based on CPUStr and FeaturesStr.
398 setFunctionAttributes(CPUStr, FeaturesStr, *M);
398 // Override function attributes based on CPUStr, FeaturesStr, and Options.
399 // Pass AlwaysRecordAttrs=false as we want to override an attribute only when
400 // the corresponding cl::opt has been provided on opt's command line.
401 setFunctionAttributes(CPUStr, FeaturesStr, Options, *M,
402 /* AlwaysRecordAttrs */ false);
399403
400404 // If the output is set to be emitted to standard out, and standard out is a
401405 // console, print out a warning message and refuse to do it. We don't