Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 1 | //===- ShrinkWrap.cpp - Compute safe point for prolog/epilog insertion ----===// |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass looks for safe point where the prologue and epilogue can be |
| 11 | // inserted. |
| 12 | // The safe point for the prologue (resp. epilogue) is called Save |
| 13 | // (resp. Restore). |
| 14 | // A point is safe for prologue (resp. epilogue) if and only if |
| 15 | // it 1) dominates (resp. post-dominates) all the frame related operations and |
| 16 | // between 2) two executions of the Save (resp. Restore) point there is an |
| 17 | // execution of the Restore (resp. Save) point. |
| 18 | // |
| 19 | // For instance, the following points are safe: |
| 20 | // for (int i = 0; i < 10; ++i) { |
| 21 | // Save |
| 22 | // ... |
| 23 | // Restore |
| 24 | // } |
| 25 | // Indeed, the execution looks like Save -> Restore -> Save -> Restore ... |
| 26 | // And the following points are not: |
| 27 | // for (int i = 0; i < 10; ++i) { |
| 28 | // Save |
| 29 | // ... |
| 30 | // } |
| 31 | // for (int i = 0; i < 10; ++i) { |
| 32 | // ... |
| 33 | // Restore |
| 34 | // } |
| 35 | // Indeed, the execution looks like Save -> Save -> ... -> Restore -> Restore. |
| 36 | // |
| 37 | // This pass also ensures that the safe points are 3) cheaper than the regular |
| 38 | // entry and exits blocks. |
| 39 | // |
| 40 | // Property #1 is ensured via the use of MachineDominatorTree and |
| 41 | // MachinePostDominatorTree. |
| 42 | // Property #2 is ensured via property #1 and MachineLoopInfo, i.e., both |
| 43 | // points must be in the same loop. |
| 44 | // Property #3 is ensured via the MachineBlockFrequencyInfo. |
| 45 | // |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 46 | // If this pass found points matching all these properties, then |
Chad Rosier | ccbb5bd | 2015-12-22 15:06:47 +0000 | [diff] [blame] | 47 | // MachineFrameInfo is updated with this information. |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 48 | // |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 50 | |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/BitVector.h" |
Quentin Colombet | 55c7a22 | 2016-01-07 01:23:49 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/PostOrderIterator.h" |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SetVector.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SmallVector.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/Statistic.h" |
Florian Hahn | 9b9abca | 2018-03-02 12:24:25 +0000 | [diff] [blame] | 56 | #include "llvm/Analysis/CFG.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 57 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 58 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 59 | #include "llvm/CodeGen/MachineDominators.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 60 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 61 | #include "llvm/CodeGen/MachineFunction.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 62 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 63 | #include "llvm/CodeGen/MachineInstr.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 64 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 65 | #include "llvm/CodeGen/MachineOperand.h" |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 66 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 67 | #include "llvm/CodeGen/MachinePostDominators.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 68 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 69 | #include "llvm/CodeGen/RegisterScavenging.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 70 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 71 | #include "llvm/CodeGen/TargetInstrInfo.h" |
Momchil Velikov | afb8c1f | 2018-04-17 08:37:38 +0000 | [diff] [blame] | 72 | #include "llvm/CodeGen/TargetLowering.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 73 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 74 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 75 | #include "llvm/IR/Attributes.h" |
| 76 | #include "llvm/IR/Function.h" |
Quentin Colombet | 88d45c6 | 2015-11-12 18:13:42 +0000 | [diff] [blame] | 77 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 78 | #include "llvm/Pass.h" |
| 79 | #include "llvm/Support/CommandLine.h" |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 80 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 81 | #include "llvm/Support/ErrorHandling.h" |
| 82 | #include "llvm/Support/raw_ostream.h" |
Quentin Colombet | 88d45c6 | 2015-11-12 18:13:42 +0000 | [diff] [blame] | 83 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 84 | #include <cassert> |
| 85 | #include <cstdint> |
| 86 | #include <memory> |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 87 | |
| 88 | using namespace llvm; |
| 89 | |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 90 | #define DEBUG_TYPE "shrink-wrap" |
| 91 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 92 | STATISTIC(NumFunc, "Number of functions"); |
| 93 | STATISTIC(NumCandidates, "Number of shrink-wrapping candidates"); |
| 94 | STATISTIC(NumCandidatesDropped, |
| 95 | "Number of shrink-wrapping candidates dropped because of frequency"); |
| 96 | |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 97 | static cl::opt<cl::boolOrDefault> |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 98 | EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden, |
| 99 | cl::desc("enable the shrink-wrapping pass")); |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 100 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 101 | namespace { |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 102 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 103 | /// Class to determine where the safe point to insert the |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 104 | /// prologue and epilogue are. |
| 105 | /// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the |
| 106 | /// shrink-wrapping term for prologue/epilogue placement, this pass |
| 107 | /// does not rely on expensive data-flow analysis. Instead we use the |
| 108 | /// dominance properties and loop information to decide which point |
| 109 | /// are safe for such insertion. |
| 110 | class ShrinkWrap : public MachineFunctionPass { |
| 111 | /// Hold callee-saved information. |
| 112 | RegisterClassInfo RCI; |
| 113 | MachineDominatorTree *MDT; |
| 114 | MachinePostDominatorTree *MPDT; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 115 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 116 | /// Current safe point found for the prologue. |
| 117 | /// The prologue will be inserted before the first instruction |
| 118 | /// in this basic block. |
| 119 | MachineBasicBlock *Save; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 120 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 121 | /// Current safe point found for the epilogue. |
| 122 | /// The epilogue will be inserted before the first terminator instruction |
| 123 | /// in this basic block. |
| 124 | MachineBasicBlock *Restore; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 125 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 126 | /// Hold the information of the basic block frequency. |
| 127 | /// Use to check the profitability of the new points. |
| 128 | MachineBlockFrequencyInfo *MBFI; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 129 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 130 | /// Hold the loop information. Used to determine if Save and Restore |
| 131 | /// are in the same loop. |
| 132 | MachineLoopInfo *MLI; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 133 | |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 134 | // Emit remarks. |
| 135 | MachineOptimizationRemarkEmitter *ORE = nullptr; |
| 136 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 137 | /// Frequency of the Entry block. |
| 138 | uint64_t EntryFreq; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 139 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 140 | /// Current opcode for frame setup. |
Matthias Braun | e4603f0 | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 141 | unsigned FrameSetupOpcode; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 142 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 143 | /// Current opcode for frame destroy. |
Matthias Braun | e4603f0 | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 144 | unsigned FrameDestroyOpcode; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 145 | |
Momchil Velikov | afb8c1f | 2018-04-17 08:37:38 +0000 | [diff] [blame] | 146 | /// Stack pointer register, used by llvm.{savestack,restorestack} |
| 147 | unsigned SP; |
| 148 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 149 | /// Entry block. |
| 150 | const MachineBasicBlock *Entry; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 151 | |
| 152 | using SetOfRegs = SmallSetVector<unsigned, 16>; |
| 153 | |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 154 | /// Registers that need to be saved for the current function. |
| 155 | mutable SetOfRegs CurrentCSRs; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 156 | |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 157 | /// Current MachineFunction. |
| 158 | MachineFunction *MachineFunc; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 159 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 160 | /// Check if \p MI uses or defines a callee-saved register or |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 161 | /// a frame index. If this is the case, this means \p MI must happen |
| 162 | /// after Save and before Restore. |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 163 | bool useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS) const; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 164 | |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 165 | const SetOfRegs &getCurrentCSRs(RegScavenger *RS) const { |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 166 | if (CurrentCSRs.empty()) { |
| 167 | BitVector SavedRegs; |
| 168 | const TargetFrameLowering *TFI = |
| 169 | MachineFunc->getSubtarget().getFrameLowering(); |
| 170 | |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 171 | TFI->determineCalleeSaves(*MachineFunc, SavedRegs, RS); |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 172 | |
| 173 | for (int Reg = SavedRegs.find_first(); Reg != -1; |
| 174 | Reg = SavedRegs.find_next(Reg)) |
| 175 | CurrentCSRs.insert((unsigned)Reg); |
| 176 | } |
| 177 | return CurrentCSRs; |
| 178 | } |
| 179 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 180 | /// Update the Save and Restore points such that \p MBB is in |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 181 | /// the region that is dominated by Save and post-dominated by Restore |
| 182 | /// and Save and Restore still match the safe point definition. |
| 183 | /// Such point may not exist and Save and/or Restore may be null after |
| 184 | /// this call. |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 185 | void updateSaveRestorePoints(MachineBasicBlock &MBB, RegScavenger *RS); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 186 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 187 | /// Initialize the pass for \p MF. |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 188 | void init(MachineFunction &MF) { |
| 189 | RCI.runOnMachineFunction(MF); |
| 190 | MDT = &getAnalysis<MachineDominatorTree>(); |
| 191 | MPDT = &getAnalysis<MachinePostDominatorTree>(); |
| 192 | Save = nullptr; |
| 193 | Restore = nullptr; |
| 194 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
| 195 | MLI = &getAnalysis<MachineLoopInfo>(); |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 196 | ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE(); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 197 | EntryFreq = MBFI->getEntryFreq(); |
Momchil Velikov | afb8c1f | 2018-04-17 08:37:38 +0000 | [diff] [blame] | 198 | const TargetSubtargetInfo &Subtarget = MF.getSubtarget(); |
| 199 | const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 200 | FrameSetupOpcode = TII.getCallFrameSetupOpcode(); |
| 201 | FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); |
Momchil Velikov | afb8c1f | 2018-04-17 08:37:38 +0000 | [diff] [blame] | 202 | SP = Subtarget.getTargetLowering()->getStackPointerRegisterToSaveRestore(); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 203 | Entry = &MF.front(); |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 204 | CurrentCSRs.clear(); |
| 205 | MachineFunc = &MF; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 206 | |
| 207 | ++NumFunc; |
| 208 | } |
| 209 | |
| 210 | /// Check whether or not Save and Restore points are still interesting for |
| 211 | /// shrink-wrapping. |
| 212 | bool ArePointsInteresting() const { return Save != Entry && Save && Restore; } |
| 213 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 214 | /// Check if shrink wrapping is enabled for this target and function. |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 215 | static bool isShrinkWrapEnabled(const MachineFunction &MF); |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 216 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 217 | public: |
| 218 | static char ID; |
| 219 | |
| 220 | ShrinkWrap() : MachineFunctionPass(ID) { |
| 221 | initializeShrinkWrapPass(*PassRegistry::getPassRegistry()); |
| 222 | } |
| 223 | |
| 224 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 225 | AU.setPreservesAll(); |
| 226 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 227 | AU.addRequired<MachineDominatorTree>(); |
| 228 | AU.addRequired<MachinePostDominatorTree>(); |
| 229 | AU.addRequired<MachineLoopInfo>(); |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 230 | AU.addRequired<MachineOptimizationRemarkEmitterPass>(); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 231 | MachineFunctionPass::getAnalysisUsage(AU); |
| 232 | } |
| 233 | |
Jun Bum Lim | c23bb98 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 234 | MachineFunctionProperties getRequiredProperties() const override { |
| 235 | return MachineFunctionProperties().set( |
| 236 | MachineFunctionProperties::Property::NoVRegs); |
| 237 | } |
| 238 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 239 | StringRef getPassName() const override { return "Shrink Wrapping analysis"; } |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 240 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 241 | /// Perform the shrink-wrapping analysis and update |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 242 | /// the MachineFrameInfo attached to \p MF with the results. |
| 243 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 244 | }; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 245 | |
| 246 | } // end anonymous namespace |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 247 | |
| 248 | char ShrinkWrap::ID = 0; |
Eugene Zelenko | 887aef7 | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 249 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 250 | char &llvm::ShrinkWrapID = ShrinkWrap::ID; |
| 251 | |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 252 | INITIALIZE_PASS_BEGIN(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 253 | INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) |
| 254 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 255 | INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree) |
| 256 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 257 | INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass) |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 258 | INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 259 | |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 260 | bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI, |
| 261 | RegScavenger *RS) const { |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 262 | if (MI.getOpcode() == FrameSetupOpcode || |
| 263 | MI.getOpcode() == FrameDestroyOpcode) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 264 | LLVM_DEBUG(dbgs() << "Frame instruction: " << MI << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 265 | return true; |
| 266 | } |
| 267 | for (const MachineOperand &MO : MI.operands()) { |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 268 | bool UseOrDefCSR = false; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 269 | if (MO.isReg()) { |
Francis Visoiu Mistrih | 1aee848 | 2018-01-16 18:55:26 +0000 | [diff] [blame] | 270 | // Ignore instructions like DBG_VALUE which don't read/def the register. |
| 271 | if (!MO.isDef() && !MO.readsReg()) |
| 272 | continue; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 273 | unsigned PhysReg = MO.getReg(); |
| 274 | if (!PhysReg) |
| 275 | continue; |
| 276 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 277 | "Unallocated register?!"); |
Momchil Velikov | afb8c1f | 2018-04-17 08:37:38 +0000 | [diff] [blame] | 278 | // The stack pointer is not normally described as a callee-saved register |
| 279 | // in calling convention definitions, so we need to watch for it |
| 280 | // separately. An SP mentioned by a call instruction, we can ignore, |
| 281 | // though, as it's harmless and we do not want to effectively disable tail |
| 282 | // calls by forcing the restore point to post-dominate them. |
| 283 | UseOrDefCSR = (!MI.isCall() && PhysReg == SP) || |
| 284 | RCI.getLastCalleeSavedAlias(PhysReg); |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 285 | } else if (MO.isRegMask()) { |
| 286 | // Check if this regmask clobbers any of the CSRs. |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 287 | for (unsigned Reg : getCurrentCSRs(RS)) { |
Quentin Colombet | 1cc0c43 | 2015-11-06 21:00:13 +0000 | [diff] [blame] | 288 | if (MO.clobbersPhysReg(Reg)) { |
| 289 | UseOrDefCSR = true; |
| 290 | break; |
| 291 | } |
| 292 | } |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 293 | } |
Francis Visoiu Mistrih | 1aee848 | 2018-01-16 18:55:26 +0000 | [diff] [blame] | 294 | // Skip FrameIndex operands in DBG_VALUE instructions. |
| 295 | if (UseOrDefCSR || (MO.isFI() && !MI.isDebugValue())) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 296 | LLVM_DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI(" |
| 297 | << MO.isFI() << "): " << MI << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 304 | /// Helper function to find the immediate (post) dominator. |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 305 | template <typename ListOfBBs, typename DominanceAnalysis> |
Benjamin Kramer | 284030a | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 306 | static MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs, |
| 307 | DominanceAnalysis &Dom) { |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 308 | MachineBasicBlock *IDom = &Block; |
| 309 | for (MachineBasicBlock *BB : BBs) { |
| 310 | IDom = Dom.findNearestCommonDominator(IDom, BB); |
| 311 | if (!IDom) |
| 312 | break; |
| 313 | } |
Michael Kuperstein | f8ab3ef | 2016-01-06 18:40:11 +0000 | [diff] [blame] | 314 | if (IDom == &Block) |
| 315 | return nullptr; |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 316 | return IDom; |
| 317 | } |
| 318 | |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 319 | void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB, |
| 320 | RegScavenger *RS) { |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 321 | // Get rid of the easy cases first. |
| 322 | if (!Save) |
| 323 | Save = &MBB; |
| 324 | else |
| 325 | Save = MDT->findNearestCommonDominator(Save, &MBB); |
| 326 | |
| 327 | if (!Save) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 328 | LLVM_DEBUG(dbgs() << "Found a block that is not reachable from Entry\n"); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (!Restore) |
| 333 | Restore = &MBB; |
Francis Visoiu Mistrih | cc8486f | 2017-05-15 23:13:35 +0000 | [diff] [blame] | 334 | else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it |
| 335 | // means the block never returns. If that's the |
| 336 | // case, we don't want to call |
| 337 | // `findNearestCommonDominator`, which will |
| 338 | // return `Restore`. |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 339 | Restore = MPDT->findNearestCommonDominator(Restore, &MBB); |
Francis Visoiu Mistrih | cc8486f | 2017-05-15 23:13:35 +0000 | [diff] [blame] | 340 | else |
| 341 | Restore = nullptr; // Abort, we can't find a restore point in this case. |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 342 | |
| 343 | // Make sure we would be able to insert the restore code before the |
| 344 | // terminator. |
| 345 | if (Restore == &MBB) { |
| 346 | for (const MachineInstr &Terminator : MBB.terminators()) { |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 347 | if (!useOrDefCSROrFI(Terminator, RS)) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 348 | continue; |
| 349 | // One of the terminator needs to happen before the restore point. |
| 350 | if (MBB.succ_empty()) { |
Francis Visoiu Mistrih | cc8486f | 2017-05-15 23:13:35 +0000 | [diff] [blame] | 351 | Restore = nullptr; // Abort, we can't find a restore point in this case. |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 352 | break; |
| 353 | } |
| 354 | // Look for a restore point that post-dominates all the successors. |
| 355 | // The immediate post-dominator is what we are looking for. |
| 356 | Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT); |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (!Restore) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 362 | LLVM_DEBUG( |
| 363 | dbgs() << "Restore point needs to be spanned on several blocks\n"); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
| 367 | // Make sure Save and Restore are suitable for shrink-wrapping: |
| 368 | // 1. all path from Save needs to lead to Restore before exiting. |
| 369 | // 2. all path to Restore needs to go through Save from Entry. |
| 370 | // We achieve that by making sure that: |
| 371 | // A. Save dominates Restore. |
| 372 | // B. Restore post-dominates Save. |
| 373 | // C. Save and Restore are in the same loop. |
| 374 | bool SaveDominatesRestore = false; |
| 375 | bool RestorePostDominatesSave = false; |
| 376 | while (Save && Restore && |
| 377 | (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) || |
| 378 | !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) || |
Quentin Colombet | 8d412e8 | 2015-12-15 03:28:11 +0000 | [diff] [blame] | 379 | // Post-dominance is not enough in loops to ensure that all uses/defs |
| 380 | // are after the prologue and before the epilogue at runtime. |
| 381 | // E.g., |
| 382 | // while(1) { |
| 383 | // Save |
| 384 | // Restore |
| 385 | // if (...) |
| 386 | // break; |
| 387 | // use/def CSRs |
| 388 | // } |
| 389 | // All the uses/defs of CSRs are dominated by Save and post-dominated |
| 390 | // by Restore. However, the CSRs uses are still reachable after |
| 391 | // Restore and before Save are executed. |
| 392 | // |
| 393 | // For now, just push the restore/save points outside of loops. |
| 394 | // FIXME: Refine the criteria to still find interesting cases |
| 395 | // for loops. |
| 396 | MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) { |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 397 | // Fix (A). |
| 398 | if (!SaveDominatesRestore) { |
| 399 | Save = MDT->findNearestCommonDominator(Save, Restore); |
| 400 | continue; |
| 401 | } |
| 402 | // Fix (B). |
| 403 | if (!RestorePostDominatesSave) |
| 404 | Restore = MPDT->findNearestCommonDominator(Restore, Save); |
| 405 | |
| 406 | // Fix (C). |
Quentin Colombet | 8d412e8 | 2015-12-15 03:28:11 +0000 | [diff] [blame] | 407 | if (Save && Restore && |
| 408 | (MLI->getLoopFor(Save) || MLI->getLoopFor(Restore))) { |
Kit Barton | 1c618a4 | 2015-08-06 19:01:57 +0000 | [diff] [blame] | 409 | if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) { |
| 410 | // Push Save outside of this loop if immediate dominator is different |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 411 | // from save block. If immediate dominator is not different, bail out. |
Michael Kuperstein | f8ab3ef | 2016-01-06 18:40:11 +0000 | [diff] [blame] | 412 | Save = FindIDom<>(*Save, Save->predecessors(), *MDT); |
| 413 | if (!Save) |
Kit Barton | 1c618a4 | 2015-08-06 19:01:57 +0000 | [diff] [blame] | 414 | break; |
Quentin Colombet | 8d412e8 | 2015-12-15 03:28:11 +0000 | [diff] [blame] | 415 | } else { |
Quentin Colombet | 07400ea | 2015-09-15 18:19:39 +0000 | [diff] [blame] | 416 | // If the loop does not exit, there is no point in looking |
| 417 | // for a post-dominator outside the loop. |
| 418 | SmallVector<MachineBasicBlock*, 4> ExitBlocks; |
| 419 | MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks); |
Quentin Colombet | d68bf2b | 2015-09-17 23:21:34 +0000 | [diff] [blame] | 420 | // Push Restore outside of this loop. |
| 421 | // Look for the immediate post-dominator of the loop exits. |
| 422 | MachineBasicBlock *IPdom = Restore; |
| 423 | for (MachineBasicBlock *LoopExitBB: ExitBlocks) { |
| 424 | IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT); |
| 425 | if (!IPdom) |
| 426 | break; |
Quentin Colombet | 07400ea | 2015-09-15 18:19:39 +0000 | [diff] [blame] | 427 | } |
Quentin Colombet | d68bf2b | 2015-09-17 23:21:34 +0000 | [diff] [blame] | 428 | // If the immediate post-dominator is not in a less nested loop, |
| 429 | // then we are stuck in a program with an infinite loop. |
| 430 | // In that case, we will not find a safe point, hence, bail out. |
| 431 | if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore)) |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 432 | Restore = IPdom; |
Kit Barton | 1c618a4 | 2015-08-06 19:01:57 +0000 | [diff] [blame] | 433 | else { |
| 434 | Restore = nullptr; |
| 435 | break; |
| 436 | } |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 437 | } |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 442 | static bool giveUpWithRemarks(MachineOptimizationRemarkEmitter *ORE, |
| 443 | StringRef RemarkName, StringRef RemarkMessage, |
| 444 | const DiagnosticLocation &Loc, |
| 445 | const MachineBasicBlock *MBB) { |
| 446 | ORE->emit([&]() { |
| 447 | return MachineOptimizationRemarkMissed(DEBUG_TYPE, RemarkName, Loc, MBB) |
| 448 | << RemarkMessage; |
| 449 | }); |
| 450 | |
| 451 | LLVM_DEBUG(dbgs() << RemarkMessage << '\n'); |
| 452 | return false; |
| 453 | } |
| 454 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 455 | bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 456 | if (skipFunction(MF.getFunction()) || MF.empty() || !isShrinkWrapEnabled(MF)) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 457 | return false; |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 458 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 459 | LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 460 | |
| 461 | init(MF); |
| 462 | |
Florian Hahn | 9b9abca | 2018-03-02 12:24:25 +0000 | [diff] [blame] | 463 | ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin()); |
| 464 | if (containsIrreducibleCFG<MachineBasicBlock *>(RPOT, *MLI)) { |
Quentin Colombet | 55c7a22 | 2016-01-07 01:23:49 +0000 | [diff] [blame] | 465 | // If MF is irreducible, a block may be in a loop without |
| 466 | // MachineLoopInfo reporting it. I.e., we may use the |
| 467 | // post-dominance property in loops, which lead to incorrect |
| 468 | // results. Moreover, we may miss that the prologue and |
| 469 | // epilogue are not in the same loop, leading to unbalanced |
| 470 | // construction/deconstruction of the stack frame. |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 471 | return giveUpWithRemarks(ORE, "UnsupportedIrreducibleCFG", |
| 472 | "Irreducible CFGs are not supported yet.", |
| 473 | MF.getFunction().getSubprogram(), &MF.front()); |
Quentin Colombet | 55c7a22 | 2016-01-07 01:23:49 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 476 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 477 | std::unique_ptr<RegScavenger> RS( |
| 478 | TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : nullptr); |
| 479 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 480 | for (MachineBasicBlock &MBB : MF) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 481 | LLVM_DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' ' |
| 482 | << MBB.getName() << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 483 | |
Francis Visoiu Mistrih | e70aaf7 | 2018-06-05 00:27:24 +0000 | [diff] [blame] | 484 | if (MBB.isEHFuncletEntry()) |
| 485 | return giveUpWithRemarks(ORE, "UnsupportedEHFunclets", |
| 486 | "EH Funclets are not supported yet.", |
| 487 | MBB.front().getDebugLoc(), &MBB); |
Quentin Colombet | 88d45c6 | 2015-11-12 18:13:42 +0000 | [diff] [blame] | 488 | |
Quentin Colombet | c83d754 | 2018-03-20 02:44:40 +0000 | [diff] [blame] | 489 | if (MBB.isEHPad()) { |
| 490 | // Push the prologue and epilogue outside of |
| 491 | // the region that may throw by making sure |
| 492 | // that all the landing pads are at least at the |
| 493 | // boundary of the save and restore points. |
| 494 | // The problem with exceptions is that the throw |
| 495 | // is not properly modeled and in particular, a |
| 496 | // basic block can jump out from the middle. |
| 497 | updateSaveRestorePoints(MBB, RS.get()); |
| 498 | if (!ArePointsInteresting()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 499 | LLVM_DEBUG(dbgs() << "EHPad prevents shrink-wrapping\n"); |
Quentin Colombet | c83d754 | 2018-03-20 02:44:40 +0000 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | continue; |
| 503 | } |
| 504 | |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 505 | for (const MachineInstr &MI : MBB) { |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 506 | if (!useOrDefCSROrFI(MI, RS.get())) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 507 | continue; |
| 508 | // Save (resp. restore) point must dominate (resp. post dominate) |
| 509 | // MI. Look for the proper basic block for those. |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 510 | updateSaveRestorePoints(MBB, RS.get()); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 511 | // If we are at a point where we cannot improve the placement of |
| 512 | // save/restore instructions, just give up. |
| 513 | if (!ArePointsInteresting()) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 514 | LLVM_DEBUG(dbgs() << "No Shrink wrap candidate found\n"); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 515 | return false; |
| 516 | } |
| 517 | // No need to look for other instructions, this basic block |
| 518 | // will already be part of the handled region. |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | if (!ArePointsInteresting()) { |
| 523 | // If the points are not interesting at this point, then they must be null |
| 524 | // because it means we did not encounter any frame/CSR related code. |
| 525 | // Otherwise, we would have returned from the previous loop. |
| 526 | assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!"); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 527 | LLVM_DEBUG(dbgs() << "Nothing to shrink-wrap\n"); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 531 | LLVM_DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq |
| 532 | << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 533 | |
Quentin Colombet | bdfc117 | 2015-05-27 06:25:48 +0000 | [diff] [blame] | 534 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 535 | do { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 536 | LLVM_DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: " |
| 537 | << Save->getNumber() << ' ' << Save->getName() << ' ' |
| 538 | << MBFI->getBlockFreq(Save).getFrequency() |
| 539 | << "\nRestore: " << Restore->getNumber() << ' ' |
| 540 | << Restore->getName() << ' ' |
| 541 | << MBFI->getBlockFreq(Restore).getFrequency() << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 542 | |
Quentin Colombet | bdfc117 | 2015-05-27 06:25:48 +0000 | [diff] [blame] | 543 | bool IsSaveCheap, TargetCanUseSaveAsPrologue = false; |
| 544 | if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) && |
| 545 | EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) && |
| 546 | ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) && |
| 547 | TFI->canUseAsEpilogue(*Restore))) |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 548 | break; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 549 | LLVM_DEBUG( |
| 550 | dbgs() << "New points are too expensive or invalid for the target\n"); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 551 | MachineBasicBlock *NewBB; |
Quentin Colombet | bdfc117 | 2015-05-27 06:25:48 +0000 | [diff] [blame] | 552 | if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) { |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 553 | Save = FindIDom<>(*Save, Save->predecessors(), *MDT); |
| 554 | if (!Save) |
| 555 | break; |
| 556 | NewBB = Save; |
| 557 | } else { |
| 558 | // Restore is expensive. |
| 559 | Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT); |
| 560 | if (!Restore) |
| 561 | break; |
| 562 | NewBB = Restore; |
| 563 | } |
Arnaud A. de Grandmaison | 8ba4bf9 | 2015-11-20 21:54:27 +0000 | [diff] [blame] | 564 | updateSaveRestorePoints(*NewBB, RS.get()); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 565 | } while (Save && Restore); |
| 566 | |
| 567 | if (!ArePointsInteresting()) { |
| 568 | ++NumCandidatesDropped; |
| 569 | return false; |
| 570 | } |
| 571 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 572 | LLVM_DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: " |
| 573 | << Save->getNumber() << ' ' << Save->getName() |
| 574 | << "\nRestore: " << Restore->getNumber() << ' ' |
| 575 | << Restore->getName() << '\n'); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 576 | |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 577 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 578 | MFI.setSavePoint(Save); |
| 579 | MFI.setRestorePoint(Restore); |
Quentin Colombet | 2f7322b | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 580 | ++NumCandidates; |
| 581 | return false; |
| 582 | } |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 583 | |
| 584 | bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) { |
| 585 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
| 586 | |
| 587 | switch (EnableShrinkWrapOpt) { |
| 588 | case cl::BOU_UNSET: |
Quentin Colombet | 88d45c6 | 2015-11-12 18:13:42 +0000 | [diff] [blame] | 589 | return TFI->enableShrinkWrapping(MF) && |
Evgeniy Stepanov | d47b5b3 | 2017-12-09 00:21:41 +0000 | [diff] [blame] | 590 | // Windows with CFI has some limitations that make it impossible |
| 591 | // to use shrink-wrapping. |
| 592 | !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() && |
| 593 | // Sanitizers look at the value of the stack at the location |
| 594 | // of the crash. Since a crash can happen anywhere, the |
| 595 | // frame must be lowered before anything else happen for the |
| 596 | // sanitizers to be able to get a correct stack frame. |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 597 | !(MF.getFunction().hasFnAttribute(Attribute::SanitizeAddress) || |
| 598 | MF.getFunction().hasFnAttribute(Attribute::SanitizeThread) || |
| 599 | MF.getFunction().hasFnAttribute(Attribute::SanitizeMemory) || |
| 600 | MF.getFunction().hasFnAttribute(Attribute::SanitizeHWAddress)); |
Kit Barton | 21b967e | 2015-08-31 18:26:45 +0000 | [diff] [blame] | 601 | // If EnableShrinkWrap is set, it takes precedence on whatever the |
| 602 | // target sets. The rational is that we assume we want to test |
| 603 | // something related to shrink-wrapping. |
| 604 | case cl::BOU_TRUE: |
| 605 | return true; |
| 606 | case cl::BOU_FALSE: |
| 607 | return false; |
| 608 | } |
| 609 | llvm_unreachable("Invalid shrink-wrapping state"); |
| 610 | } |