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