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