| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1 | //===- HexagonHardwareLoops.cpp - Identify and generate hardware loops ----===// |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +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 identifies loops where we can generate the Hexagon hardware |
| 11 | // loop instruction. The hardware loop can perform loop branches with a |
| 12 | // zero-cycle overhead. |
| 13 | // |
| 14 | // The pattern that defines the induction variable can changed depending on |
| 15 | // prior optimizations. For example, the IndVarSimplify phase run by 'opt' |
| 16 | // normalizes induction variables, and the Loop Strength Reduction pass |
| 17 | // run by 'llc' may also make changes to the induction variable. |
| 18 | // The pattern detected by this phase is due to running Strength Reduction. |
| 19 | // |
| 20 | // Criteria for hardware loops: |
| 21 | // - Countable loops (w/ ind. var for a trip count) |
| 22 | // - Assumes loops are normalized by IndVarSimplify |
| 23 | // - Try inner-most loops first |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 24 | // - No function calls in loops. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 28 | #include "HexagonInstrInfo.h" |
| Eric Christopher | 2c44f43 | 2015-02-02 19:05:28 +0000 | [diff] [blame] | 29 | #include "HexagonSubtarget.h" |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/ArrayRef.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallSet.h" |
| 33 | #include "llvm/ADT/SmallVector.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringRef.h" |
| 36 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineDominators.h" |
| 38 | #include "llvm/CodeGen/MachineFunction.h" |
| 39 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineInstr.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 42 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineOperand.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Constants.h" |
| 46 | #include "llvm/IR/DebugLoc.h" |
| 47 | #include "llvm/Pass.h" |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CommandLine.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ErrorHandling.h" |
| 51 | #include "llvm/Support/MathExtras.h" |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetRegisterInfo.h" |
| 54 | #include <cassert> |
| 55 | #include <cstdint> |
| 56 | #include <cstdlib> |
| 57 | #include <iterator> |
| 58 | #include <map> |
| 59 | #include <set> |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 60 | #include <string> |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 61 | #include <utility> |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 62 | #include <vector> |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 63 | |
| 64 | using namespace llvm; |
| 65 | |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 66 | #define DEBUG_TYPE "hwloops" |
| 67 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 68 | #ifndef NDEBUG |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 69 | static cl::opt<int> HWLoopLimit("hexagon-max-hwloop", cl::Hidden, cl::init(-1)); |
| 70 | |
| 71 | // Option to create preheader only for a specific function. |
| 72 | static cl::opt<std::string> PHFn("hexagon-hwloop-phfn", cl::Hidden, |
| 73 | cl::init("")); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 76 | // Option to create a preheader if one doesn't exist. |
| 77 | static cl::opt<bool> HWCreatePreheader("hexagon-hwloop-preheader", |
| 78 | cl::Hidden, cl::init(true), |
| 79 | cl::desc("Add a preheader to a hardware loop if one doesn't exist")); |
| 80 | |
| Krzysztof Parzyszek | 06a2b6b | 2016-07-27 21:20:54 +0000 | [diff] [blame] | 81 | // Turn it off by default. If a preheader block is not created here, the |
| 82 | // software pipeliner may be unable to find a block suitable to serve as |
| 83 | // a preheader. In that case SWP will not run. |
| 84 | static cl::opt<bool> SpecPreheader("hwloop-spec-preheader", cl::init(false), |
| 85 | cl::Hidden, cl::ZeroOrMore, cl::desc("Allow speculation of preheader " |
| 86 | "instructions")); |
| 87 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 88 | STATISTIC(NumHWLoops, "Number of loops converted to hardware loops"); |
| 89 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 90 | namespace llvm { |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 91 | |
| Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 92 | FunctionPass *createHexagonHardwareLoops(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 93 | void initializeHexagonHardwareLoopsPass(PassRegistry&); |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 94 | |
| 95 | } // end namespace llvm |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 96 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 97 | namespace { |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 98 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 99 | class CountValue; |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 100 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 101 | struct HexagonHardwareLoops : public MachineFunctionPass { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 102 | MachineLoopInfo *MLI; |
| 103 | MachineRegisterInfo *MRI; |
| 104 | MachineDominatorTree *MDT; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 105 | const HexagonInstrInfo *TII; |
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 106 | const HexagonRegisterInfo *TRI; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 107 | #ifndef NDEBUG |
| 108 | static int Counter; |
| 109 | #endif |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 110 | |
| 111 | public: |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 112 | static char ID; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 113 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 114 | HexagonHardwareLoops() : MachineFunctionPass(ID) { |
| 115 | initializeHexagonHardwareLoopsPass(*PassRegistry::getPassRegistry()); |
| 116 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 117 | |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 118 | bool runOnMachineFunction(MachineFunction &MF) override; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 119 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 120 | StringRef getPassName() const override { return "Hexagon Hardware Loops"; } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 121 | |
| Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 122 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 123 | AU.addRequired<MachineDominatorTree>(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 124 | AU.addRequired<MachineLoopInfo>(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 125 | MachineFunctionPass::getAnalysisUsage(AU); |
| 126 | } |
| 127 | |
| 128 | private: |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 129 | using LoopFeederMap = std::map<unsigned, MachineInstr *>; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 130 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 131 | /// Kinds of comparisons in the compare instructions. |
| 132 | struct Comparison { |
| 133 | enum Kind { |
| 134 | EQ = 0x01, |
| 135 | NE = 0x02, |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 136 | L = 0x04, |
| 137 | G = 0x08, |
| 138 | U = 0x40, |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 139 | LTs = L, |
| 140 | LEs = L | EQ, |
| 141 | GTs = G, |
| 142 | GEs = G | EQ, |
| 143 | LTu = L | U, |
| 144 | LEu = L | EQ | U, |
| 145 | GTu = G | U, |
| 146 | GEu = G | EQ | U |
| 147 | }; |
| 148 | |
| 149 | static Kind getSwappedComparison(Kind Cmp) { |
| 150 | assert ((!((Cmp & L) && (Cmp & G))) && "Malformed comparison operator"); |
| 151 | if ((Cmp & L) || (Cmp & G)) |
| 152 | return (Kind)(Cmp ^ (L|G)); |
| 153 | return Cmp; |
| 154 | } |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 155 | |
| 156 | static Kind getNegatedComparison(Kind Cmp) { |
| 157 | if ((Cmp & L) || (Cmp & G)) |
| 158 | return (Kind)((Cmp ^ (L | G)) ^ EQ); |
| 159 | if ((Cmp & NE) || (Cmp & EQ)) |
| 160 | return (Kind)(Cmp ^ (EQ | NE)); |
| 161 | return (Kind)0; |
| 162 | } |
| 163 | |
| 164 | static bool isSigned(Kind Cmp) { |
| 165 | return (Cmp & (L | G) && !(Cmp & U)); |
| 166 | } |
| 167 | |
| 168 | static bool isUnsigned(Kind Cmp) { |
| 169 | return (Cmp & U); |
| 170 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /// \brief Find the register that contains the loop controlling |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 174 | /// induction variable. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 175 | /// If successful, it will return true and set the \p Reg, \p IVBump |
| 176 | /// and \p IVOp arguments. Otherwise it will return false. |
| 177 | /// The returned induction register is the register R that follows the |
| 178 | /// following induction pattern: |
| 179 | /// loop: |
| 180 | /// R = phi ..., [ R.next, LatchBlock ] |
| 181 | /// R.next = R + #bump |
| 182 | /// if (R.next < #N) goto loop |
| 183 | /// IVBump is the immediate value added to R, and IVOp is the instruction |
| 184 | /// "R.next = R + #bump". |
| 185 | bool findInductionRegister(MachineLoop *L, unsigned &Reg, |
| 186 | int64_t &IVBump, MachineInstr *&IVOp) const; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 187 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 188 | /// \brief Return the comparison kind for the specified opcode. |
| 189 | Comparison::Kind getComparisonKind(unsigned CondOpc, |
| 190 | MachineOperand *InitialValue, |
| 191 | const MachineOperand *Endvalue, |
| 192 | int64_t IVBump) const; |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 193 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 194 | /// \brief Analyze the statements in a loop to determine if the loop |
| 195 | /// has a computable trip count and, if so, return a value that represents |
| 196 | /// the trip count expression. |
| 197 | CountValue *getLoopTripCount(MachineLoop *L, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 198 | SmallVectorImpl<MachineInstr *> &OldInsts); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 199 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 200 | /// \brief Return the expression that represents the number of times |
| 201 | /// a loop iterates. The function takes the operands that represent the |
| 202 | /// loop start value, loop end value, and induction value. Based upon |
| 203 | /// these operands, the function attempts to compute the trip count. |
| 204 | /// If the trip count is not directly available (as an immediate value, |
| 205 | /// or a register), the function will attempt to insert computation of it |
| 206 | /// to the loop's preheader. |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 207 | CountValue *computeCount(MachineLoop *Loop, const MachineOperand *Start, |
| 208 | const MachineOperand *End, unsigned IVReg, |
| 209 | int64_t IVBump, Comparison::Kind Cmp) const; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 210 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 211 | /// \brief Return true if the instruction is not valid within a hardware |
| 212 | /// loop. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 213 | bool isInvalidLoopOperation(const MachineInstr *MI, |
| 214 | bool IsInnerHWLoop) const; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 215 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 216 | /// \brief Return true if the loop contains an instruction that inhibits |
| 217 | /// using the hardware loop. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 218 | bool containsInvalidInstruction(MachineLoop *L, bool IsInnerHWLoop) const; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 219 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 220 | /// \brief Given a loop, check if we can convert it to a hardware loop. |
| 221 | /// If so, then perform the conversion and return true. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 222 | bool convertToHardwareLoop(MachineLoop *L, bool &L0used, bool &L1used); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 223 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 224 | /// \brief Return true if the instruction is now dead. |
| 225 | bool isDead(const MachineInstr *MI, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 226 | SmallVectorImpl<MachineInstr *> &DeadPhis) const; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 227 | |
| 228 | /// \brief Remove the instruction if it is now dead. |
| 229 | void removeIfDead(MachineInstr *MI); |
| 230 | |
| 231 | /// \brief Make sure that the "bump" instruction executes before the |
| 232 | /// compare. We need that for the IV fixup, so that the compare |
| 233 | /// instruction would not use a bumped value that has not yet been |
| 234 | /// defined. If the instructions are out of order, try to reorder them. |
| 235 | bool orderBumpCompare(MachineInstr *BumpI, MachineInstr *CmpI); |
| 236 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 237 | /// \brief Return true if MO and MI pair is visited only once. If visited |
| 238 | /// more than once, this indicates there is recursion. In such a case, |
| 239 | /// return false. |
| 240 | bool isLoopFeeder(MachineLoop *L, MachineBasicBlock *A, MachineInstr *MI, |
| 241 | const MachineOperand *MO, |
| 242 | LoopFeederMap &LoopFeederPhi) const; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 243 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 244 | /// \brief Return true if the Phi may generate a value that may underflow, |
| 245 | /// or may wrap. |
| 246 | bool phiMayWrapOrUnderflow(MachineInstr *Phi, const MachineOperand *EndVal, |
| 247 | MachineBasicBlock *MBB, MachineLoop *L, |
| 248 | LoopFeederMap &LoopFeederPhi) const; |
| 249 | |
| 250 | /// \brief Return true if the induction variable may underflow an unsigned |
| 251 | /// value in the first iteration. |
| 252 | bool loopCountMayWrapOrUnderFlow(const MachineOperand *InitVal, |
| 253 | const MachineOperand *EndVal, |
| 254 | MachineBasicBlock *MBB, MachineLoop *L, |
| 255 | LoopFeederMap &LoopFeederPhi) const; |
| 256 | |
| 257 | /// \brief Check if the given operand has a compile-time known constant |
| 258 | /// value. Return true if yes, and false otherwise. When returning true, set |
| 259 | /// Val to the corresponding constant value. |
| 260 | bool checkForImmediate(const MachineOperand &MO, int64_t &Val) const; |
| 261 | |
| 262 | /// \brief Check if the operand has a compile-time known constant value. |
| 263 | bool isImmediate(const MachineOperand &MO) const { |
| 264 | int64_t V; |
| 265 | return checkForImmediate(MO, V); |
| 266 | } |
| 267 | |
| 268 | /// \brief Return the immediate for the specified operand. |
| 269 | int64_t getImmediate(const MachineOperand &MO) const { |
| 270 | int64_t V; |
| 271 | if (!checkForImmediate(MO, V)) |
| 272 | llvm_unreachable("Invalid operand"); |
| 273 | return V; |
| 274 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 275 | |
| 276 | /// \brief Reset the given machine operand to now refer to a new immediate |
| 277 | /// value. Assumes that the operand was already referencing an immediate |
| 278 | /// value, either directly, or via a register. |
| 279 | void setImmediate(MachineOperand &MO, int64_t Val); |
| 280 | |
| Mandeep Singh Grang | 1be19e6 | 2017-09-15 20:01:43 +0000 | [diff] [blame] | 281 | /// \brief Fix the data flow of the induction variable. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 282 | /// The desired flow is: phi ---> bump -+-> comparison-in-latch. |
| 283 | /// | |
| 284 | /// +-> back to phi |
| 285 | /// where "bump" is the increment of the induction variable: |
| 286 | /// iv = iv + #const. |
| 287 | /// Due to some prior code transformations, the actual flow may look |
| 288 | /// like this: |
| 289 | /// phi -+-> bump ---> back to phi |
| 290 | /// | |
| 291 | /// +-> comparison-in-latch (against upper_bound-bump), |
| 292 | /// i.e. the comparison that controls the loop execution may be using |
| 293 | /// the value of the induction variable from before the increment. |
| 294 | /// |
| 295 | /// Return true if the loop's flow is the desired one (i.e. it's |
| 296 | /// either been fixed, or no fixing was necessary). |
| 297 | /// Otherwise, return false. This can happen if the induction variable |
| 298 | /// couldn't be identified, or if the value in the latch's comparison |
| 299 | /// cannot be adjusted to reflect the post-bump value. |
| 300 | bool fixupInductionVariable(MachineLoop *L); |
| 301 | |
| 302 | /// \brief Given a loop, if it does not have a preheader, create one. |
| 303 | /// Return the block that is the preheader. |
| 304 | MachineBasicBlock *createPreheaderForLoop(MachineLoop *L); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | char HexagonHardwareLoops::ID = 0; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 308 | #ifndef NDEBUG |
| 309 | int HexagonHardwareLoops::Counter = 0; |
| 310 | #endif |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 311 | |
| Sid Manning | 67a8936 | 2014-08-28 14:16:32 +0000 | [diff] [blame] | 312 | /// \brief Abstraction for a trip count of a loop. A smaller version |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 313 | /// of the MachineOperand class without the concerns of changing the |
| 314 | /// operand representation. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 315 | class CountValue { |
| 316 | public: |
| 317 | enum CountValueType { |
| 318 | CV_Register, |
| 319 | CV_Immediate |
| 320 | }; |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 321 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 322 | private: |
| 323 | CountValueType Kind; |
| 324 | union Values { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 325 | struct { |
| 326 | unsigned Reg; |
| 327 | unsigned Sub; |
| 328 | } R; |
| 329 | unsigned ImmVal; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 330 | } Contents; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 331 | |
| 332 | public: |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 333 | explicit CountValue(CountValueType t, unsigned v, unsigned u = 0) { |
| 334 | Kind = t; |
| 335 | if (Kind == CV_Register) { |
| 336 | Contents.R.Reg = v; |
| 337 | Contents.R.Sub = u; |
| 338 | } else { |
| 339 | Contents.ImmVal = v; |
| 340 | } |
| 341 | } |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 342 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 343 | bool isReg() const { return Kind == CV_Register; } |
| 344 | bool isImm() const { return Kind == CV_Immediate; } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 345 | |
| 346 | unsigned getReg() const { |
| 347 | assert(isReg() && "Wrong CountValue accessor"); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 348 | return Contents.R.Reg; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 349 | } |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 350 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 351 | unsigned getSubReg() const { |
| 352 | assert(isReg() && "Wrong CountValue accessor"); |
| 353 | return Contents.R.Sub; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 354 | } |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 355 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 356 | unsigned getImm() const { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 357 | assert(isImm() && "Wrong CountValue accessor"); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 358 | return Contents.ImmVal; |
| 359 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 360 | |
| Eric Christopher | 2c44f43 | 2015-02-02 19:05:28 +0000 | [diff] [blame] | 361 | void print(raw_ostream &OS, const TargetRegisterInfo *TRI = nullptr) const { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 362 | if (isReg()) { OS << PrintReg(Contents.R.Reg, TRI, Contents.R.Sub); } |
| 363 | if (isImm()) { OS << Contents.ImmVal; } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 364 | } |
| 365 | }; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 366 | |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 367 | } // end anonymous namespace |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 368 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 369 | INITIALIZE_PASS_BEGIN(HexagonHardwareLoops, "hwloops", |
| 370 | "Hexagon Hardware Loops", false, false) |
| 371 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 372 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 373 | INITIALIZE_PASS_END(HexagonHardwareLoops, "hwloops", |
| 374 | "Hexagon Hardware Loops", false, false) |
| 375 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 376 | FunctionPass *llvm::createHexagonHardwareLoops() { |
| 377 | return new HexagonHardwareLoops(); |
| 378 | } |
| 379 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 380 | bool HexagonHardwareLoops::runOnMachineFunction(MachineFunction &MF) { |
| 381 | DEBUG(dbgs() << "********* Hexagon Hardware Loops *********\n"); |
| Andrew Kaylor | 5b444a2 | 2016-04-26 19:46:28 +0000 | [diff] [blame] | 382 | if (skipFunction(*MF.getFunction())) |
| 383 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 384 | |
| 385 | bool Changed = false; |
| 386 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 387 | MLI = &getAnalysis<MachineLoopInfo>(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 388 | MRI = &MF.getRegInfo(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 389 | MDT = &getAnalysis<MachineDominatorTree>(); |
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 390 | const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>(); |
| 391 | TII = HST.getInstrInfo(); |
| 392 | TRI = HST.getRegisterInfo(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 393 | |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 394 | for (auto &L : *MLI) |
| 395 | if (!L->getParentLoop()) { |
| 396 | bool L0Used = false; |
| 397 | bool L1Used = false; |
| 398 | Changed |= convertToHardwareLoop(L, L0Used, L1Used); |
| 399 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 400 | |
| 401 | return Changed; |
| 402 | } |
| 403 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 404 | bool HexagonHardwareLoops::findInductionRegister(MachineLoop *L, |
| 405 | unsigned &Reg, |
| 406 | int64_t &IVBump, |
| 407 | MachineInstr *&IVOp |
| 408 | ) const { |
| 409 | MachineBasicBlock *Header = L->getHeader(); |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 410 | MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 411 | MachineBasicBlock *Latch = L->getLoopLatch(); |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 412 | MachineBasicBlock *ExitingBlock = L->findLoopControlBlock(); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 413 | if (!Header || !Preheader || !Latch || !ExitingBlock) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 414 | return false; |
| 415 | |
| 416 | // This pair represents an induction register together with an immediate |
| 417 | // value that will be added to it in each loop iteration. |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 418 | using RegisterBump = std::pair<unsigned, int64_t>; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 419 | |
| 420 | // Mapping: R.next -> (R, bump), where R, R.next and bump are derived |
| 421 | // from an induction operation |
| 422 | // R.next = R + bump |
| 423 | // where bump is an immediate value. |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 424 | using InductionMap = std::map<unsigned, RegisterBump>; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 425 | |
| 426 | InductionMap IndMap; |
| 427 | |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 428 | using instr_iterator = MachineBasicBlock::instr_iterator; |
| 429 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 430 | for (instr_iterator I = Header->instr_begin(), E = Header->instr_end(); |
| 431 | I != E && I->isPHI(); ++I) { |
| 432 | MachineInstr *Phi = &*I; |
| 433 | |
| 434 | // Have a PHI instruction. Get the operand that corresponds to the |
| 435 | // latch block, and see if is a result of an addition of form "reg+imm", |
| 436 | // where the "reg" is defined by the PHI node we are looking at. |
| 437 | for (unsigned i = 1, n = Phi->getNumOperands(); i < n; i += 2) { |
| 438 | if (Phi->getOperand(i+1).getMBB() != Latch) |
| 439 | continue; |
| 440 | |
| 441 | unsigned PhiOpReg = Phi->getOperand(i).getReg(); |
| 442 | MachineInstr *DI = MRI->getVRegDef(PhiOpReg); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 443 | |
| Sjoerd Meijer | 724023a | 2016-09-14 08:20:03 +0000 | [diff] [blame] | 444 | if (DI->getDesc().isAdd()) { |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 445 | // If the register operand to the add is the PHI we're looking at, this |
| 446 | // meets the induction pattern. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 447 | unsigned IndReg = DI->getOperand(1).getReg(); |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 448 | MachineOperand &Opnd2 = DI->getOperand(2); |
| 449 | int64_t V; |
| 450 | if (MRI->getVRegDef(IndReg) == Phi && checkForImmediate(Opnd2, V)) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 451 | unsigned UpdReg = DI->getOperand(0).getReg(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 452 | IndMap.insert(std::make_pair(UpdReg, std::make_pair(IndReg, V))); |
| 453 | } |
| 454 | } |
| 455 | } // for (i) |
| 456 | } // for (instr) |
| 457 | |
| 458 | SmallVector<MachineOperand,2> Cond; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 459 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 460 | bool NotAnalyzed = TII->analyzeBranch(*ExitingBlock, TB, FB, Cond, false); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 461 | if (NotAnalyzed) |
| 462 | return false; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 463 | |
| Brendon Cahoon | df43e68 | 2015-05-08 16:16:29 +0000 | [diff] [blame] | 464 | unsigned PredR, PredPos, PredRegFlags; |
| 465 | if (!TII->getPredReg(Cond, PredR, PredPos, PredRegFlags)) |
| 466 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 467 | |
| 468 | MachineInstr *PredI = MRI->getVRegDef(PredR); |
| 469 | if (!PredI->isCompare()) |
| 470 | return false; |
| 471 | |
| 472 | unsigned CmpReg1 = 0, CmpReg2 = 0; |
| 473 | int CmpImm = 0, CmpMask = 0; |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 474 | bool CmpAnalyzed = |
| 475 | TII->analyzeCompare(*PredI, CmpReg1, CmpReg2, CmpMask, CmpImm); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 476 | // Fail if the compare was not analyzed, or it's not comparing a register |
| 477 | // with an immediate value. Not checking the mask here, since we handle |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 478 | // the individual compare opcodes (including A4_cmpb*) later on. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 479 | if (!CmpAnalyzed) |
| 480 | return false; |
| 481 | |
| 482 | // Exactly one of the input registers to the comparison should be among |
| 483 | // the induction registers. |
| 484 | InductionMap::iterator IndMapEnd = IndMap.end(); |
| 485 | InductionMap::iterator F = IndMapEnd; |
| 486 | if (CmpReg1 != 0) { |
| 487 | InductionMap::iterator F1 = IndMap.find(CmpReg1); |
| 488 | if (F1 != IndMapEnd) |
| 489 | F = F1; |
| 490 | } |
| 491 | if (CmpReg2 != 0) { |
| 492 | InductionMap::iterator F2 = IndMap.find(CmpReg2); |
| 493 | if (F2 != IndMapEnd) { |
| 494 | if (F != IndMapEnd) |
| 495 | return false; |
| 496 | F = F2; |
| 497 | } |
| 498 | } |
| 499 | if (F == IndMapEnd) |
| 500 | return false; |
| 501 | |
| 502 | Reg = F->second.first; |
| 503 | IVBump = F->second.second; |
| 504 | IVOp = MRI->getVRegDef(F->first); |
| 505 | return true; |
| 506 | } |
| 507 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 508 | // Return the comparison kind for the specified opcode. |
| 509 | HexagonHardwareLoops::Comparison::Kind |
| 510 | HexagonHardwareLoops::getComparisonKind(unsigned CondOpc, |
| 511 | MachineOperand *InitialValue, |
| 512 | const MachineOperand *EndValue, |
| 513 | int64_t IVBump) const { |
| 514 | Comparison::Kind Cmp = (Comparison::Kind)0; |
| 515 | switch (CondOpc) { |
| 516 | case Hexagon::C2_cmpeqi: |
| 517 | case Hexagon::C2_cmpeq: |
| 518 | case Hexagon::C2_cmpeqp: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 519 | Cmp = Comparison::EQ; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 520 | break; |
| 521 | case Hexagon::C4_cmpneq: |
| 522 | case Hexagon::C4_cmpneqi: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 523 | Cmp = Comparison::NE; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 524 | break; |
| 525 | case Hexagon::C4_cmplte: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 526 | Cmp = Comparison::LEs; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 527 | break; |
| 528 | case Hexagon::C4_cmplteu: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 529 | Cmp = Comparison::LEu; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 530 | break; |
| 531 | case Hexagon::C2_cmpgtui: |
| 532 | case Hexagon::C2_cmpgtu: |
| 533 | case Hexagon::C2_cmpgtup: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 534 | Cmp = Comparison::GTu; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 535 | break; |
| 536 | case Hexagon::C2_cmpgti: |
| 537 | case Hexagon::C2_cmpgt: |
| 538 | case Hexagon::C2_cmpgtp: |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 539 | Cmp = Comparison::GTs; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 540 | break; |
| 541 | default: |
| 542 | return (Comparison::Kind)0; |
| 543 | } |
| 544 | return Cmp; |
| 545 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 546 | |
| 547 | /// \brief Analyze the statements in a loop to determine if the loop has |
| 548 | /// a computable trip count and, if so, return a value that represents |
| 549 | /// the trip count expression. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 550 | /// |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 551 | /// This function iterates over the phi nodes in the loop to check for |
| 552 | /// induction variable patterns that are used in the calculation for |
| 553 | /// the number of time the loop is executed. |
| 554 | CountValue *HexagonHardwareLoops::getLoopTripCount(MachineLoop *L, |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 555 | SmallVectorImpl<MachineInstr *> &OldInsts) { |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 556 | MachineBasicBlock *TopMBB = L->getTopBlock(); |
| 557 | MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(); |
| 558 | assert(PI != TopMBB->pred_end() && |
| 559 | "Loop must have more than one incoming edge!"); |
| 560 | MachineBasicBlock *Backedge = *PI++; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 561 | if (PI == TopMBB->pred_end()) // dead loop? |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 562 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 563 | MachineBasicBlock *Incoming = *PI++; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 564 | if (PI != TopMBB->pred_end()) // multiple backedges? |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 565 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 566 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 567 | // Make sure there is one incoming and one backedge and determine which |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 568 | // is which. |
| 569 | if (L->contains(Incoming)) { |
| 570 | if (L->contains(Backedge)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 571 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 572 | std::swap(Incoming, Backedge); |
| 573 | } else if (!L->contains(Backedge)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 574 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 575 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 576 | // Look for the cmp instruction to determine if we can get a useful trip |
| 577 | // count. The trip count can be either a register or an immediate. The |
| 578 | // location of the value depends upon the type (reg or imm). |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 579 | MachineBasicBlock *ExitingBlock = L->findLoopControlBlock(); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 580 | if (!ExitingBlock) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 581 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 582 | |
| 583 | unsigned IVReg = 0; |
| 584 | int64_t IVBump = 0; |
| 585 | MachineInstr *IVOp; |
| 586 | bool FoundIV = findInductionRegister(L, IVReg, IVBump, IVOp); |
| 587 | if (!FoundIV) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 588 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 589 | |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 590 | MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 591 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 592 | MachineOperand *InitialValue = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 593 | MachineInstr *IV_Phi = MRI->getVRegDef(IVReg); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 594 | MachineBasicBlock *Latch = L->getLoopLatch(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 595 | for (unsigned i = 1, n = IV_Phi->getNumOperands(); i < n; i += 2) { |
| 596 | MachineBasicBlock *MBB = IV_Phi->getOperand(i+1).getMBB(); |
| 597 | if (MBB == Preheader) |
| 598 | InitialValue = &IV_Phi->getOperand(i); |
| 599 | else if (MBB == Latch) |
| 600 | IVReg = IV_Phi->getOperand(i).getReg(); // Want IV reg after bump. |
| 601 | } |
| 602 | if (!InitialValue) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 603 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 604 | |
| 605 | SmallVector<MachineOperand,2> Cond; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 606 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 607 | bool NotAnalyzed = TII->analyzeBranch(*ExitingBlock, TB, FB, Cond, false); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 608 | if (NotAnalyzed) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 609 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 610 | |
| 611 | MachineBasicBlock *Header = L->getHeader(); |
| 612 | // TB must be non-null. If FB is also non-null, one of them must be |
| 613 | // the header. Otherwise, branch to TB could be exiting the loop, and |
| 614 | // the fall through can go to the header. |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 615 | assert (TB && "Exit block without a branch?"); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 616 | if (ExitingBlock != Latch && (TB == Latch || FB == Latch)) { |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 617 | MachineBasicBlock *LTB = nullptr, *LFB = nullptr; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 618 | SmallVector<MachineOperand,2> LCond; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 619 | bool NotAnalyzed = TII->analyzeBranch(*Latch, LTB, LFB, LCond, false); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 620 | if (NotAnalyzed) |
| 621 | return nullptr; |
| 622 | if (TB == Latch) |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 623 | TB = (LTB == Header) ? LTB : LFB; |
| 624 | else |
| 625 | FB = (LTB == Header) ? LTB: LFB; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 626 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 627 | assert ((!FB || TB == Header || FB == Header) && "Branches not to header?"); |
| 628 | if (!TB || (FB && TB != Header && FB != Header)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 629 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 630 | |
| 631 | // Branches of form "if (!P) ..." cause HexagonInstrInfo::AnalyzeBranch |
| 632 | // to put imm(0), followed by P in the vector Cond. |
| 633 | // If TB is not the header, it means that the "not-taken" path must lead |
| 634 | // to the header. |
| Brendon Cahoon | df43e68 | 2015-05-08 16:16:29 +0000 | [diff] [blame] | 635 | bool Negated = TII->predOpcodeHasNot(Cond) ^ (TB != Header); |
| 636 | unsigned PredReg, PredPos, PredRegFlags; |
| 637 | if (!TII->getPredReg(Cond, PredReg, PredPos, PredRegFlags)) |
| 638 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 639 | MachineInstr *CondI = MRI->getVRegDef(PredReg); |
| 640 | unsigned CondOpc = CondI->getOpcode(); |
| 641 | |
| 642 | unsigned CmpReg1 = 0, CmpReg2 = 0; |
| 643 | int Mask = 0, ImmValue = 0; |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 644 | bool AnalyzedCmp = |
| 645 | TII->analyzeCompare(*CondI, CmpReg1, CmpReg2, Mask, ImmValue); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 646 | if (!AnalyzedCmp) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 647 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 648 | |
| 649 | // The comparison operator type determines how we compute the loop |
| 650 | // trip count. |
| 651 | OldInsts.push_back(CondI); |
| 652 | OldInsts.push_back(IVOp); |
| 653 | |
| 654 | // Sadly, the following code gets information based on the position |
| 655 | // of the operands in the compare instruction. This has to be done |
| 656 | // this way, because the comparisons check for a specific relationship |
| 657 | // between the operands (e.g. is-less-than), rather than to find out |
| 658 | // what relationship the operands are in (as on PPC). |
| 659 | Comparison::Kind Cmp; |
| 660 | bool isSwapped = false; |
| 661 | const MachineOperand &Op1 = CondI->getOperand(1); |
| 662 | const MachineOperand &Op2 = CondI->getOperand(2); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 663 | const MachineOperand *EndValue = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 664 | |
| 665 | if (Op1.isReg()) { |
| 666 | if (Op2.isImm() || Op1.getReg() == IVReg) |
| 667 | EndValue = &Op2; |
| 668 | else { |
| 669 | EndValue = &Op1; |
| 670 | isSwapped = true; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 673 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 674 | if (!EndValue) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 675 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 676 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 677 | Cmp = getComparisonKind(CondOpc, InitialValue, EndValue, IVBump); |
| 678 | if (!Cmp) |
| 679 | return nullptr; |
| 680 | if (Negated) |
| 681 | Cmp = Comparison::getNegatedComparison(Cmp); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 682 | if (isSwapped) |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 683 | Cmp = Comparison::getSwappedComparison(Cmp); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 684 | |
| 685 | if (InitialValue->isReg()) { |
| 686 | unsigned R = InitialValue->getReg(); |
| 687 | MachineBasicBlock *DefBB = MRI->getVRegDef(R)->getParent(); |
| 688 | if (!MDT->properlyDominates(DefBB, Header)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 689 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 690 | OldInsts.push_back(MRI->getVRegDef(R)); |
| 691 | } |
| 692 | if (EndValue->isReg()) { |
| 693 | unsigned R = EndValue->getReg(); |
| 694 | MachineBasicBlock *DefBB = MRI->getVRegDef(R)->getParent(); |
| 695 | if (!MDT->properlyDominates(DefBB, Header)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 696 | return nullptr; |
| Brendon Cahoon | 485bea74 | 2015-05-14 17:31:40 +0000 | [diff] [blame] | 697 | OldInsts.push_back(MRI->getVRegDef(R)); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | return computeCount(L, InitialValue, EndValue, IVReg, IVBump, Cmp); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 703 | /// \brief Helper function that returns the expression that represents the |
| 704 | /// number of times a loop iterates. The function takes the operands that |
| 705 | /// represent the loop start value, loop end value, and induction value. |
| 706 | /// Based upon these operands, the function attempts to compute the trip count. |
| 707 | CountValue *HexagonHardwareLoops::computeCount(MachineLoop *Loop, |
| 708 | const MachineOperand *Start, |
| 709 | const MachineOperand *End, |
| 710 | unsigned IVReg, |
| 711 | int64_t IVBump, |
| 712 | Comparison::Kind Cmp) const { |
| 713 | // Cannot handle comparison EQ, i.e. while (A == B). |
| 714 | if (Cmp == Comparison::EQ) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 715 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 716 | |
| 717 | // Check if either the start or end values are an assignment of an immediate. |
| 718 | // If so, use the immediate value rather than the register. |
| 719 | if (Start->isReg()) { |
| 720 | const MachineInstr *StartValInstr = MRI->getVRegDef(Start->getReg()); |
| Brendon Cahoon | 485bea74 | 2015-05-14 17:31:40 +0000 | [diff] [blame] | 721 | if (StartValInstr && (StartValInstr->getOpcode() == Hexagon::A2_tfrsi || |
| 722 | StartValInstr->getOpcode() == Hexagon::A2_tfrpi)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 723 | Start = &StartValInstr->getOperand(1); |
| 724 | } |
| 725 | if (End->isReg()) { |
| 726 | const MachineInstr *EndValInstr = MRI->getVRegDef(End->getReg()); |
| Brendon Cahoon | 485bea74 | 2015-05-14 17:31:40 +0000 | [diff] [blame] | 727 | if (EndValInstr && (EndValInstr->getOpcode() == Hexagon::A2_tfrsi || |
| 728 | EndValInstr->getOpcode() == Hexagon::A2_tfrpi)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 729 | End = &EndValInstr->getOperand(1); |
| 730 | } |
| 731 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 732 | if (!Start->isReg() && !Start->isImm()) |
| 733 | return nullptr; |
| 734 | if (!End->isReg() && !End->isImm()) |
| 735 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 736 | |
| 737 | bool CmpLess = Cmp & Comparison::L; |
| 738 | bool CmpGreater = Cmp & Comparison::G; |
| 739 | bool CmpHasEqual = Cmp & Comparison::EQ; |
| 740 | |
| 741 | // Avoid certain wrap-arounds. This doesn't detect all wrap-arounds. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 742 | if (CmpLess && IVBump < 0) |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 743 | // Loop going while iv is "less" with the iv value going down. Must wrap. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 744 | return nullptr; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 745 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 746 | if (CmpGreater && IVBump > 0) |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 747 | // Loop going while iv is "greater" with the iv value going up. Must wrap. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 748 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 749 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 750 | // Phis that may feed into the loop. |
| 751 | LoopFeederMap LoopFeederPhi; |
| 752 | |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 753 | // Check if the initial value may be zero and can be decremented in the first |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 754 | // iteration. If the value is zero, the endloop instruction will not decrement |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 755 | // the loop counter, so we shouldn't generate a hardware loop in this case. |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 756 | if (loopCountMayWrapOrUnderFlow(Start, End, Loop->getLoopPreheader(), Loop, |
| 757 | LoopFeederPhi)) |
| 758 | return nullptr; |
| 759 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 760 | if (Start->isImm() && End->isImm()) { |
| 761 | // Both, start and end are immediates. |
| 762 | int64_t StartV = Start->getImm(); |
| 763 | int64_t EndV = End->getImm(); |
| 764 | int64_t Dist = EndV - StartV; |
| 765 | if (Dist == 0) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 766 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 767 | |
| 768 | bool Exact = (Dist % IVBump) == 0; |
| 769 | |
| 770 | if (Cmp == Comparison::NE) { |
| 771 | if (!Exact) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 772 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 773 | if ((Dist < 0) ^ (IVBump < 0)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 774 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // For comparisons that include the final value (i.e. include equality |
| 778 | // with the final value), we need to increase the distance by 1. |
| 779 | if (CmpHasEqual) |
| 780 | Dist = Dist > 0 ? Dist+1 : Dist-1; |
| 781 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 782 | // For the loop to iterate, CmpLess should imply Dist > 0. Similarly, |
| 783 | // CmpGreater should imply Dist < 0. These conditions could actually |
| 784 | // fail, for example, in unreachable code (which may still appear to be |
| 785 | // reachable in the CFG). |
| 786 | if ((CmpLess && Dist < 0) || (CmpGreater && Dist > 0)) |
| 787 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 788 | |
| 789 | // "Normalized" distance, i.e. with the bump set to +-1. |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 790 | int64_t Dist1 = (IVBump > 0) ? (Dist + (IVBump - 1)) / IVBump |
| 791 | : (-Dist + (-IVBump - 1)) / (-IVBump); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 792 | assert (Dist1 > 0 && "Fishy thing. Both operands have the same sign."); |
| 793 | |
| 794 | uint64_t Count = Dist1; |
| 795 | |
| 796 | if (Count > 0xFFFFFFFFULL) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 797 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 798 | |
| 799 | return new CountValue(CountValue::CV_Immediate, Count); |
| 800 | } |
| 801 | |
| 802 | // A general case: Start and End are some values, but the actual |
| 803 | // iteration count may not be available. If it is not, insert |
| 804 | // a computation of it into the preheader. |
| 805 | |
| 806 | // If the induction variable bump is not a power of 2, quit. |
| 807 | // Othwerise we'd need a general integer division. |
| Benjamin Kramer | 7bd1f7c | 2015-03-09 20:20:16 +0000 | [diff] [blame] | 808 | if (!isPowerOf2_64(std::abs(IVBump))) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 809 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 810 | |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 811 | MachineBasicBlock *PH = MLI->findLoopPreheader(Loop, SpecPreheader); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 812 | assert (PH && "Should have a preheader by now"); |
| 813 | MachineBasicBlock::iterator InsertPos = PH->getFirstTerminator(); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 814 | DebugLoc DL; |
| 815 | if (InsertPos != PH->end()) |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 816 | DL = InsertPos->getDebugLoc(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 817 | |
| 818 | // If Start is an immediate and End is a register, the trip count |
| 819 | // will be "reg - imm". Hexagon's "subtract immediate" instruction |
| 820 | // is actually "reg + -imm". |
| 821 | |
| 822 | // If the loop IV is going downwards, i.e. if the bump is negative, |
| 823 | // then the iteration count (computed as End-Start) will need to be |
| 824 | // negated. To avoid the negation, just swap Start and End. |
| 825 | if (IVBump < 0) { |
| 826 | std::swap(Start, End); |
| 827 | IVBump = -IVBump; |
| 828 | } |
| 829 | // Cmp may now have a wrong direction, e.g. LEs may now be GEs. |
| 830 | // Signedness, and "including equality" are preserved. |
| 831 | |
| 832 | bool RegToImm = Start->isReg() && End->isImm(); // for (reg..imm) |
| 833 | bool RegToReg = Start->isReg() && End->isReg(); // for (reg..reg) |
| 834 | |
| 835 | int64_t StartV = 0, EndV = 0; |
| 836 | if (Start->isImm()) |
| 837 | StartV = Start->getImm(); |
| 838 | if (End->isImm()) |
| 839 | EndV = End->getImm(); |
| 840 | |
| 841 | int64_t AdjV = 0; |
| 842 | // To compute the iteration count, we would need this computation: |
| 843 | // Count = (End - Start + (IVBump-1)) / IVBump |
| 844 | // or, when CmpHasEqual: |
| 845 | // Count = (End - Start + (IVBump-1)+1) / IVBump |
| 846 | // The "IVBump-1" part is the adjustment (AdjV). We can avoid |
| 847 | // generating an instruction specifically to add it if we can adjust |
| 848 | // the immediate values for Start or End. |
| 849 | |
| 850 | if (CmpHasEqual) { |
| 851 | // Need to add 1 to the total iteration count. |
| 852 | if (Start->isImm()) |
| 853 | StartV--; |
| 854 | else if (End->isImm()) |
| 855 | EndV++; |
| 856 | else |
| 857 | AdjV += 1; |
| 858 | } |
| 859 | |
| 860 | if (Cmp != Comparison::NE) { |
| 861 | if (Start->isImm()) |
| 862 | StartV -= (IVBump-1); |
| 863 | else if (End->isImm()) |
| 864 | EndV += (IVBump-1); |
| 865 | else |
| 866 | AdjV += (IVBump-1); |
| 867 | } |
| 868 | |
| 869 | unsigned R = 0, SR = 0; |
| 870 | if (Start->isReg()) { |
| 871 | R = Start->getReg(); |
| 872 | SR = Start->getSubReg(); |
| 873 | } else { |
| 874 | R = End->getReg(); |
| 875 | SR = End->getSubReg(); |
| 876 | } |
| 877 | const TargetRegisterClass *RC = MRI->getRegClass(R); |
| 878 | // Hardware loops cannot handle 64-bit registers. If it's a double |
| 879 | // register, it has to have a subregister. |
| 880 | if (!SR && RC == &Hexagon::DoubleRegsRegClass) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 881 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 882 | const TargetRegisterClass *IntRC = &Hexagon::IntRegsRegClass; |
| 883 | |
| 884 | // Compute DistR (register with the distance between Start and End). |
| 885 | unsigned DistR, DistSR; |
| 886 | |
| 887 | // Avoid special case, where the start value is an imm(0). |
| 888 | if (Start->isImm() && StartV == 0) { |
| 889 | DistR = End->getReg(); |
| 890 | DistSR = End->getSubReg(); |
| 891 | } else { |
| Colin LeMahieu | e88447d | 2014-11-21 21:19:18 +0000 | [diff] [blame] | 892 | const MCInstrDesc &SubD = RegToReg ? TII->get(Hexagon::A2_sub) : |
| Colin LeMahieu | 27d5007 | 2015-02-05 18:38:08 +0000 | [diff] [blame] | 893 | (RegToImm ? TII->get(Hexagon::A2_subri) : |
| Colin LeMahieu | f297dbe | 2015-02-05 17:49:13 +0000 | [diff] [blame] | 894 | TII->get(Hexagon::A2_addi)); |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 895 | if (RegToReg || RegToImm) { |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 896 | unsigned SubR = MRI->createVirtualRegister(IntRC); |
| 897 | MachineInstrBuilder SubIB = |
| 898 | BuildMI(*PH, InsertPos, DL, SubD, SubR); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 899 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 900 | if (RegToReg) |
| 901 | SubIB.addReg(End->getReg(), 0, End->getSubReg()) |
| 902 | .addReg(Start->getReg(), 0, Start->getSubReg()); |
| 903 | else |
| 904 | SubIB.addImm(EndV) |
| 905 | .addReg(Start->getReg(), 0, Start->getSubReg()); |
| 906 | DistR = SubR; |
| 907 | } else { |
| 908 | // If the loop has been unrolled, we should use the original loop count |
| 909 | // instead of recalculating the value. This will avoid additional |
| 910 | // 'Add' instruction. |
| 911 | const MachineInstr *EndValInstr = MRI->getVRegDef(End->getReg()); |
| 912 | if (EndValInstr->getOpcode() == Hexagon::A2_addi && |
| 913 | EndValInstr->getOperand(2).getImm() == StartV) { |
| 914 | DistR = EndValInstr->getOperand(1).getReg(); |
| 915 | } else { |
| 916 | unsigned SubR = MRI->createVirtualRegister(IntRC); |
| 917 | MachineInstrBuilder SubIB = |
| 918 | BuildMI(*PH, InsertPos, DL, SubD, SubR); |
| 919 | SubIB.addReg(End->getReg(), 0, End->getSubReg()) |
| 920 | .addImm(-StartV); |
| 921 | DistR = SubR; |
| 922 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 923 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 924 | DistSR = 0; |
| 925 | } |
| 926 | |
| 927 | // From DistR, compute AdjR (register with the adjusted distance). |
| 928 | unsigned AdjR, AdjSR; |
| 929 | |
| 930 | if (AdjV == 0) { |
| 931 | AdjR = DistR; |
| 932 | AdjSR = DistSR; |
| 933 | } else { |
| 934 | // Generate CountR = ADD DistR, AdjVal |
| 935 | unsigned AddR = MRI->createVirtualRegister(IntRC); |
| Colin LeMahieu | f297dbe | 2015-02-05 17:49:13 +0000 | [diff] [blame] | 936 | MCInstrDesc const &AddD = TII->get(Hexagon::A2_addi); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 937 | BuildMI(*PH, InsertPos, DL, AddD, AddR) |
| 938 | .addReg(DistR, 0, DistSR) |
| 939 | .addImm(AdjV); |
| 940 | |
| 941 | AdjR = AddR; |
| 942 | AdjSR = 0; |
| 943 | } |
| 944 | |
| 945 | // From AdjR, compute CountR (register with the final count). |
| 946 | unsigned CountR, CountSR; |
| 947 | |
| 948 | if (IVBump == 1) { |
| 949 | CountR = AdjR; |
| 950 | CountSR = AdjSR; |
| 951 | } else { |
| 952 | // The IV bump is a power of two. Log_2(IV bump) is the shift amount. |
| 953 | unsigned Shift = Log2_32(IVBump); |
| 954 | |
| 955 | // Generate NormR = LSR DistR, Shift. |
| 956 | unsigned LsrR = MRI->createVirtualRegister(IntRC); |
| Colin LeMahieu | aa1bade | 2014-12-16 23:36:15 +0000 | [diff] [blame] | 957 | const MCInstrDesc &LsrD = TII->get(Hexagon::S2_lsr_i_r); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 958 | BuildMI(*PH, InsertPos, DL, LsrD, LsrR) |
| 959 | .addReg(AdjR, 0, AdjSR) |
| 960 | .addImm(Shift); |
| 961 | |
| 962 | CountR = LsrR; |
| 963 | CountSR = 0; |
| 964 | } |
| 965 | |
| 966 | return new CountValue(CountValue::CV_Register, CountR, CountSR); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 969 | /// \brief Return true if the operation is invalid within hardware loop. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 970 | bool HexagonHardwareLoops::isInvalidLoopOperation(const MachineInstr *MI, |
| 971 | bool IsInnerHWLoop) const { |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 972 | // Call is not allowed because the callee may use a hardware loop except for |
| 973 | // the case when the call never returns. |
| Krzysztof Parzyszek | 1b689da | 2016-08-11 21:14:25 +0000 | [diff] [blame] | 974 | if (MI->getDesc().isCall()) |
| 975 | return !TII->doesNotReturn(*MI); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 976 | |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 977 | // Check if the instruction defines a hardware loop register. |
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 978 | using namespace Hexagon; |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 979 | |
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 980 | static const unsigned Regs01[] = { LC0, SA0, LC1, SA1 }; |
| 981 | static const unsigned Regs1[] = { LC1, SA1 }; |
| 982 | auto CheckRegs = IsInnerHWLoop ? makeArrayRef(Regs01, array_lengthof(Regs01)) |
| 983 | : makeArrayRef(Regs1, array_lengthof(Regs1)); |
| 984 | for (unsigned R : CheckRegs) |
| 985 | if (MI->modifiesRegister(R, TRI)) |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 986 | return true; |
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 987 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 988 | return false; |
| 989 | } |
| 990 | |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 991 | /// \brief Return true if the loop contains an instruction that inhibits |
| 992 | /// the use of the hardware loop instruction. |
| 993 | bool HexagonHardwareLoops::containsInvalidInstruction(MachineLoop *L, |
| 994 | bool IsInnerHWLoop) const { |
| Benjamin Kramer | 7d60526 | 2013-09-15 22:04:42 +0000 | [diff] [blame] | 995 | const std::vector<MachineBasicBlock *> &Blocks = L->getBlocks(); |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 996 | DEBUG(dbgs() << "\nhw_loop head, BB#" << Blocks[0]->getNumber();); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 997 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 998 | MachineBasicBlock *MBB = Blocks[i]; |
| 999 | for (MachineBasicBlock::iterator |
| 1000 | MII = MBB->begin(), E = MBB->end(); MII != E; ++MII) { |
| 1001 | const MachineInstr *MI = &*MII; |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1002 | if (isInvalidLoopOperation(MI, IsInnerHWLoop)) { |
| 1003 | DEBUG(dbgs()<< "\nCannot convert to hw_loop due to:"; MI->dump();); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1004 | return true; |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1005 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1011 | /// \brief Returns true if the instruction is dead. This was essentially |
| 1012 | /// copied from DeadMachineInstructionElim::isDead, but with special cases |
| 1013 | /// for inline asm, physical registers and instructions with side effects |
| 1014 | /// removed. |
| 1015 | bool HexagonHardwareLoops::isDead(const MachineInstr *MI, |
| Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1016 | SmallVectorImpl<MachineInstr *> &DeadPhis) const { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1017 | // Examine each operand. |
| 1018 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1019 | const MachineOperand &MO = MI->getOperand(i); |
| 1020 | if (!MO.isReg() || !MO.isDef()) |
| 1021 | continue; |
| 1022 | |
| 1023 | unsigned Reg = MO.getReg(); |
| 1024 | if (MRI->use_nodbg_empty(Reg)) |
| 1025 | continue; |
| 1026 | |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1027 | using use_nodbg_iterator = MachineRegisterInfo::use_nodbg_iterator; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1028 | |
| 1029 | // This instruction has users, but if the only user is the phi node for the |
| 1030 | // parent block, and the only use of that phi node is this instruction, then |
| 1031 | // this instruction is dead: both it (and the phi node) can be removed. |
| 1032 | use_nodbg_iterator I = MRI->use_nodbg_begin(Reg); |
| 1033 | use_nodbg_iterator End = MRI->use_nodbg_end(); |
| Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1034 | if (std::next(I) != End || !I->getParent()->isPHI()) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | |
| Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1037 | MachineInstr *OnePhi = I->getParent(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1038 | for (unsigned j = 0, f = OnePhi->getNumOperands(); j != f; ++j) { |
| 1039 | const MachineOperand &OPO = OnePhi->getOperand(j); |
| 1040 | if (!OPO.isReg() || !OPO.isDef()) |
| 1041 | continue; |
| 1042 | |
| 1043 | unsigned OPReg = OPO.getReg(); |
| 1044 | use_nodbg_iterator nextJ; |
| 1045 | for (use_nodbg_iterator J = MRI->use_nodbg_begin(OPReg); |
| 1046 | J != End; J = nextJ) { |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1047 | nextJ = std::next(J); |
| Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1048 | MachineOperand &Use = *J; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1049 | MachineInstr *UseMI = Use.getParent(); |
| 1050 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1051 | // If the phi node has a user that is not MI, bail. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1052 | if (MI != UseMI) |
| 1053 | return false; |
| 1054 | } |
| 1055 | } |
| 1056 | DeadPhis.push_back(OnePhi); |
| 1057 | } |
| 1058 | |
| 1059 | // If there are no defs with uses, the instruction is dead. |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | void HexagonHardwareLoops::removeIfDead(MachineInstr *MI) { |
| 1064 | // This procedure was essentially copied from DeadMachineInstructionElim. |
| 1065 | |
| 1066 | SmallVector<MachineInstr*, 1> DeadPhis; |
| 1067 | if (isDead(MI, DeadPhis)) { |
| 1068 | DEBUG(dbgs() << "HW looping will remove: " << *MI); |
| 1069 | |
| 1070 | // It is possible that some DBG_VALUE instructions refer to this |
| 1071 | // instruction. Examine each def operand for such references; |
| 1072 | // if found, mark the DBG_VALUE as undef (but don't delete it). |
| 1073 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1074 | const MachineOperand &MO = MI->getOperand(i); |
| 1075 | if (!MO.isReg() || !MO.isDef()) |
| 1076 | continue; |
| 1077 | unsigned Reg = MO.getReg(); |
| 1078 | MachineRegisterInfo::use_iterator nextI; |
| 1079 | for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg), |
| 1080 | E = MRI->use_end(); I != E; I = nextI) { |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1081 | nextI = std::next(I); // I is invalidated by the setReg |
| Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1082 | MachineOperand &Use = *I; |
| 1083 | MachineInstr *UseMI = I->getParent(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1084 | if (UseMI == MI) |
| 1085 | continue; |
| 1086 | if (Use.isDebug()) |
| 1087 | UseMI->getOperand(0).setReg(0U); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | MI->eraseFromParent(); |
| 1092 | for (unsigned i = 0; i < DeadPhis.size(); ++i) |
| 1093 | DeadPhis[i]->eraseFromParent(); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /// \brief Check if the loop is a candidate for converting to a hardware |
| 1098 | /// loop. If so, then perform the transformation. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1099 | /// |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1100 | /// This function works on innermost loops first. A loop can be converted |
| 1101 | /// if it is a counting loop; either a register value or an immediate. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1102 | /// |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1103 | /// The code makes several assumptions about the representation of the loop |
| 1104 | /// in llvm. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1105 | bool HexagonHardwareLoops::convertToHardwareLoop(MachineLoop *L, |
| 1106 | bool &RecL0used, |
| 1107 | bool &RecL1used) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1108 | // This is just for sanity. |
| 1109 | assert(L->getHeader() && "Loop without a header?"); |
| 1110 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1111 | bool Changed = false; |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1112 | bool L0Used = false; |
| 1113 | bool L1Used = false; |
| 1114 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1115 | // Process nested loops first. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1116 | for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I) { |
| 1117 | Changed |= convertToHardwareLoop(*I, RecL0used, RecL1used); |
| 1118 | L0Used |= RecL0used; |
| 1119 | L1Used |= RecL1used; |
| 1120 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1121 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1122 | // If a nested loop has been converted, then we can't convert this loop. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1123 | if (Changed && L0Used && L1Used) |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1124 | return Changed; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1125 | |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1126 | unsigned LOOP_i; |
| 1127 | unsigned LOOP_r; |
| 1128 | unsigned ENDLOOP; |
| 1129 | |
| 1130 | // Flag used to track loopN instruction: |
| 1131 | // 1 - Hardware loop is being generated for the inner most loop. |
| 1132 | // 0 - Hardware loop is being generated for the outer loop. |
| 1133 | unsigned IsInnerHWLoop = 1; |
| 1134 | |
| 1135 | if (L0Used) { |
| 1136 | LOOP_i = Hexagon::J2_loop1i; |
| 1137 | LOOP_r = Hexagon::J2_loop1r; |
| 1138 | ENDLOOP = Hexagon::ENDLOOP1; |
| 1139 | IsInnerHWLoop = 0; |
| 1140 | } else { |
| 1141 | LOOP_i = Hexagon::J2_loop0i; |
| 1142 | LOOP_r = Hexagon::J2_loop0r; |
| 1143 | ENDLOOP = Hexagon::ENDLOOP0; |
| 1144 | } |
| 1145 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1146 | #ifndef NDEBUG |
| 1147 | // Stop trying after reaching the limit (if any). |
| 1148 | int Limit = HWLoopLimit; |
| 1149 | if (Limit >= 0) { |
| 1150 | if (Counter >= HWLoopLimit) |
| 1151 | return false; |
| 1152 | Counter++; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1153 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1154 | #endif |
| 1155 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1156 | // Does the loop contain any invalid instructions? |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1157 | if (containsInvalidInstruction(L, IsInnerHWLoop)) |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1158 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1159 | |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1160 | MachineBasicBlock *LastMBB = L->findLoopControlBlock(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1161 | // Don't generate hw loop if the loop has more than one exit. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1162 | if (!LastMBB) |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1163 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1164 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1165 | MachineBasicBlock::iterator LastI = LastMBB->getFirstTerminator(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1166 | if (LastI == LastMBB->end()) |
| Matthew Curtis | 7a93811 | 2012-12-07 21:03:15 +0000 | [diff] [blame] | 1167 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1168 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1169 | // Is the induction variable bump feeding the latch condition? |
| 1170 | if (!fixupInductionVariable(L)) |
| 1171 | return false; |
| 1172 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1173 | // Ensure the loop has a preheader: the loop instruction will be |
| 1174 | // placed there. |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1175 | MachineBasicBlock *Preheader = MLI->findLoopPreheader(L, SpecPreheader); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1176 | if (!Preheader) { |
| 1177 | Preheader = createPreheaderForLoop(L); |
| 1178 | if (!Preheader) |
| 1179 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1180 | } |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1181 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1182 | MachineBasicBlock::iterator InsertPos = Preheader->getFirstTerminator(); |
| 1183 | |
| 1184 | SmallVector<MachineInstr*, 2> OldInsts; |
| 1185 | // Are we able to determine the trip count for the loop? |
| 1186 | CountValue *TripCount = getLoopTripCount(L, OldInsts); |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1187 | if (!TripCount) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1188 | return false; |
| 1189 | |
| 1190 | // Is the trip count available in the preheader? |
| 1191 | if (TripCount->isReg()) { |
| 1192 | // There will be a use of the register inserted into the preheader, |
| 1193 | // so make sure that the register is actually defined at that point. |
| 1194 | MachineInstr *TCDef = MRI->getVRegDef(TripCount->getReg()); |
| 1195 | MachineBasicBlock *BBDef = TCDef->getParent(); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1196 | if (!MDT->dominates(BBDef, Preheader)) |
| 1197 | return false; |
| Matthew Curtis | 7a93811 | 2012-12-07 21:03:15 +0000 | [diff] [blame] | 1198 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Determine the loop start. |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1201 | MachineBasicBlock *TopBlock = L->getTopBlock(); |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1202 | MachineBasicBlock *ExitingBlock = L->findLoopControlBlock(); |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1203 | MachineBasicBlock *LoopStart = nullptr; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1204 | if (ExitingBlock != L->getLoopLatch()) { |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1205 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1206 | SmallVector<MachineOperand, 2> Cond; |
| 1207 | |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1208 | if (TII->analyzeBranch(*ExitingBlock, TB, FB, Cond, false)) |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | |
| 1211 | if (L->contains(TB)) |
| 1212 | LoopStart = TB; |
| 1213 | else if (L->contains(FB)) |
| 1214 | LoopStart = FB; |
| 1215 | else |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1216 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1217 | } |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1218 | else |
| 1219 | LoopStart = TopBlock; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1220 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1221 | // Convert the loop to a hardware loop. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1222 | DEBUG(dbgs() << "Change to hardware loop at "; L->dump()); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1223 | DebugLoc DL; |
| Matthew Curtis | 7a93811 | 2012-12-07 21:03:15 +0000 | [diff] [blame] | 1224 | if (InsertPos != Preheader->end()) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1225 | DL = InsertPos->getDebugLoc(); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (TripCount->isReg()) { |
| 1228 | // Create a copy of the loop count register. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1229 | unsigned CountReg = MRI->createVirtualRegister(&Hexagon::IntRegsRegClass); |
| 1230 | BuildMI(*Preheader, InsertPos, DL, TII->get(TargetOpcode::COPY), CountReg) |
| 1231 | .addReg(TripCount->getReg(), 0, TripCount->getSubReg()); |
| Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 1232 | // Add the Loop instruction to the beginning of the loop. |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1233 | BuildMI(*Preheader, InsertPos, DL, TII->get(LOOP_r)).addMBB(LoopStart) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1234 | .addReg(CountReg); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1235 | } else { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1236 | assert(TripCount->isImm() && "Expecting immediate value for trip count"); |
| 1237 | // Add the Loop immediate instruction to the beginning of the loop, |
| 1238 | // if the immediate fits in the instructions. Otherwise, we need to |
| 1239 | // create a new virtual register. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1240 | int64_t CountImm = TripCount->getImm(); |
| Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 1241 | if (!TII->isValidOffset(LOOP_i, CountImm, TRI)) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1242 | unsigned CountReg = MRI->createVirtualRegister(&Hexagon::IntRegsRegClass); |
| Colin LeMahieu | 4af437f | 2014-12-09 20:23:30 +0000 | [diff] [blame] | 1243 | BuildMI(*Preheader, InsertPos, DL, TII->get(Hexagon::A2_tfrsi), CountReg) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1244 | .addImm(CountImm); |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1245 | BuildMI(*Preheader, InsertPos, DL, TII->get(LOOP_r)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1246 | .addMBB(LoopStart).addReg(CountReg); |
| 1247 | } else |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1248 | BuildMI(*Preheader, InsertPos, DL, TII->get(LOOP_i)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1249 | .addMBB(LoopStart).addImm(CountImm); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1252 | // Make sure the loop start always has a reference in the CFG. We need |
| 1253 | // to create a BlockAddress operand to get this mechanism to work both the |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1254 | // MachineBasicBlock and BasicBlock objects need the flag set. |
| 1255 | LoopStart->setHasAddressTaken(); |
| 1256 | // This line is needed to set the hasAddressTaken flag on the BasicBlock |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1257 | // object. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1258 | BlockAddress::get(const_cast<BasicBlock *>(LoopStart->getBasicBlock())); |
| 1259 | |
| 1260 | // Replace the loop branch with an endloop instruction. |
| Matthew Curtis | 7a93811 | 2012-12-07 21:03:15 +0000 | [diff] [blame] | 1261 | DebugLoc LastIDL = LastI->getDebugLoc(); |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1262 | BuildMI(*LastMBB, LastI, LastIDL, TII->get(ENDLOOP)).addMBB(LoopStart); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1263 | |
| 1264 | // The loop ends with either: |
| 1265 | // - a conditional branch followed by an unconditional branch, or |
| 1266 | // - a conditional branch to the loop start. |
| Colin LeMahieu | db0b13c | 2014-12-10 21:24:10 +0000 | [diff] [blame] | 1267 | if (LastI->getOpcode() == Hexagon::J2_jumpt || |
| 1268 | LastI->getOpcode() == Hexagon::J2_jumpf) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1269 | // Delete one and change/add an uncond. branch to out of the loop. |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1270 | MachineBasicBlock *BranchTarget = LastI->getOperand(1).getMBB(); |
| 1271 | LastI = LastMBB->erase(LastI); |
| 1272 | if (!L->contains(BranchTarget)) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1273 | if (LastI != LastMBB->end()) |
| 1274 | LastI = LastMBB->erase(LastI); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1275 | SmallVector<MachineOperand, 0> Cond; |
| Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1276 | TII->insertBranch(*LastMBB, BranchTarget, nullptr, Cond, LastIDL); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } else { |
| 1279 | // Conditional branch to loop start; just delete it. |
| 1280 | LastMBB->erase(LastI); |
| 1281 | } |
| 1282 | delete TripCount; |
| 1283 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1284 | // The induction operation and the comparison may now be |
| 1285 | // unneeded. If these are unneeded, then remove them. |
| 1286 | for (unsigned i = 0; i < OldInsts.size(); ++i) |
| 1287 | removeIfDead(OldInsts[i]); |
| 1288 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1289 | ++NumHWLoops; |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Set RecL1used and RecL0used only after hardware loop has been |
| 1292 | // successfully generated. Doing it earlier can cause wrong loop instruction |
| 1293 | // to be used. |
| 1294 | if (L0Used) // Loop0 was already used. So, the correct loop must be loop1. |
| 1295 | RecL1used = true; |
| 1296 | else |
| 1297 | RecL0used = true; |
| 1298 | |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1299 | return true; |
| 1300 | } |
| 1301 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1302 | bool HexagonHardwareLoops::orderBumpCompare(MachineInstr *BumpI, |
| 1303 | MachineInstr *CmpI) { |
| 1304 | assert (BumpI != CmpI && "Bump and compare in the same instruction?"); |
| 1305 | |
| 1306 | MachineBasicBlock *BB = BumpI->getParent(); |
| 1307 | if (CmpI->getParent() != BB) |
| 1308 | return false; |
| 1309 | |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1310 | using instr_iterator = MachineBasicBlock::instr_iterator; |
| 1311 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1312 | // Check if things are in order to begin with. |
| Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 1313 | for (instr_iterator I(BumpI), E = BB->instr_end(); I != E; ++I) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1314 | if (&*I == CmpI) |
| 1315 | return true; |
| 1316 | |
| 1317 | // Out of order. |
| 1318 | unsigned PredR = CmpI->getOperand(0).getReg(); |
| 1319 | bool FoundBump = false; |
| Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 1320 | instr_iterator CmpIt = CmpI->getIterator(), NextIt = std::next(CmpIt); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1321 | for (instr_iterator I = NextIt, E = BB->instr_end(); I != E; ++I) { |
| 1322 | MachineInstr *In = &*I; |
| 1323 | for (unsigned i = 0, n = In->getNumOperands(); i < n; ++i) { |
| 1324 | MachineOperand &MO = In->getOperand(i); |
| 1325 | if (MO.isReg() && MO.isUse()) { |
| 1326 | if (MO.getReg() == PredR) // Found an intervening use of PredR. |
| 1327 | return false; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | if (In == BumpI) { |
| Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 1332 | BB->splice(++BumpI->getIterator(), BB, CmpI->getIterator()); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1333 | FoundBump = true; |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
| 1337 | assert (FoundBump && "Cannot determine instruction order"); |
| 1338 | return FoundBump; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1341 | /// This function is required to break recursion. Visiting phis in a loop may |
| 1342 | /// result in recursion during compilation. We break the recursion by making |
| 1343 | /// sure that we visit a MachineOperand and its definition in a |
| 1344 | /// MachineInstruction only once. If we attempt to visit more than once, then |
| 1345 | /// there is recursion, and will return false. |
| 1346 | bool HexagonHardwareLoops::isLoopFeeder(MachineLoop *L, MachineBasicBlock *A, |
| 1347 | MachineInstr *MI, |
| 1348 | const MachineOperand *MO, |
| 1349 | LoopFeederMap &LoopFeederPhi) const { |
| 1350 | if (LoopFeederPhi.find(MO->getReg()) == LoopFeederPhi.end()) { |
| 1351 | const std::vector<MachineBasicBlock *> &Blocks = L->getBlocks(); |
| 1352 | DEBUG(dbgs() << "\nhw_loop head, BB#" << Blocks[0]->getNumber();); |
| 1353 | // Ignore all BBs that form Loop. |
| 1354 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 1355 | MachineBasicBlock *MBB = Blocks[i]; |
| 1356 | if (A == MBB) |
| 1357 | return false; |
| 1358 | } |
| 1359 | MachineInstr *Def = MRI->getVRegDef(MO->getReg()); |
| 1360 | LoopFeederPhi.insert(std::make_pair(MO->getReg(), Def)); |
| 1361 | return true; |
| 1362 | } else |
| 1363 | // Already visited node. |
| 1364 | return false; |
| 1365 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1366 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1367 | /// Return true if a Phi may generate a value that can underflow. |
| 1368 | /// This function calls loopCountMayWrapOrUnderFlow for each Phi operand. |
| 1369 | bool HexagonHardwareLoops::phiMayWrapOrUnderflow( |
| 1370 | MachineInstr *Phi, const MachineOperand *EndVal, MachineBasicBlock *MBB, |
| 1371 | MachineLoop *L, LoopFeederMap &LoopFeederPhi) const { |
| 1372 | assert(Phi->isPHI() && "Expecting a Phi."); |
| 1373 | // Walk through each Phi, and its used operands. Make sure that |
| 1374 | // if there is recursion in Phi, we won't generate hardware loops. |
| 1375 | for (int i = 1, n = Phi->getNumOperands(); i < n; i += 2) |
| 1376 | if (isLoopFeeder(L, MBB, Phi, &(Phi->getOperand(i)), LoopFeederPhi)) |
| 1377 | if (loopCountMayWrapOrUnderFlow(&(Phi->getOperand(i)), EndVal, |
| 1378 | Phi->getParent(), L, LoopFeederPhi)) |
| 1379 | return true; |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | /// Return true if the induction variable can underflow in the first iteration. |
| 1384 | /// An example, is an initial unsigned value that is 0 and is decrement in the |
| 1385 | /// first itertion of a do-while loop. In this case, we cannot generate a |
| 1386 | /// hardware loop because the endloop instruction does not decrement the loop |
| 1387 | /// counter if it is <= 1. We only need to perform this analysis if the |
| 1388 | /// initial value is a register. |
| 1389 | /// |
| 1390 | /// This function assumes the initial value may underfow unless proven |
| 1391 | /// otherwise. If the type is signed, then we don't care because signed |
| 1392 | /// underflow is undefined. We attempt to prove the initial value is not |
| 1393 | /// zero by perfoming a crude analysis of the loop counter. This function |
| 1394 | /// checks if the initial value is used in any comparison prior to the loop |
| 1395 | /// and, if so, assumes the comparison is a range check. This is inexact, |
| 1396 | /// but will catch the simple cases. |
| 1397 | bool HexagonHardwareLoops::loopCountMayWrapOrUnderFlow( |
| 1398 | const MachineOperand *InitVal, const MachineOperand *EndVal, |
| 1399 | MachineBasicBlock *MBB, MachineLoop *L, |
| 1400 | LoopFeederMap &LoopFeederPhi) const { |
| 1401 | // Only check register values since they are unknown. |
| 1402 | if (!InitVal->isReg()) |
| 1403 | return false; |
| 1404 | |
| 1405 | if (!EndVal->isImm()) |
| 1406 | return false; |
| 1407 | |
| 1408 | // A register value that is assigned an immediate is a known value, and it |
| 1409 | // won't underflow in the first iteration. |
| 1410 | int64_t Imm; |
| 1411 | if (checkForImmediate(*InitVal, Imm)) |
| 1412 | return (EndVal->getImm() == Imm); |
| 1413 | |
| 1414 | unsigned Reg = InitVal->getReg(); |
| 1415 | |
| 1416 | // We don't know the value of a physical register. |
| 1417 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 1418 | return true; |
| 1419 | |
| 1420 | MachineInstr *Def = MRI->getVRegDef(Reg); |
| 1421 | if (!Def) |
| 1422 | return true; |
| 1423 | |
| 1424 | // If the initial value is a Phi or copy and the operands may not underflow, |
| 1425 | // then the definition cannot be underflow either. |
| 1426 | if (Def->isPHI() && !phiMayWrapOrUnderflow(Def, EndVal, Def->getParent(), |
| 1427 | L, LoopFeederPhi)) |
| 1428 | return false; |
| 1429 | if (Def->isCopy() && !loopCountMayWrapOrUnderFlow(&(Def->getOperand(1)), |
| 1430 | EndVal, Def->getParent(), |
| 1431 | L, LoopFeederPhi)) |
| 1432 | return false; |
| 1433 | |
| 1434 | // Iterate over the uses of the initial value. If the initial value is used |
| 1435 | // in a compare, then we assume this is a range check that ensures the loop |
| 1436 | // doesn't underflow. This is not an exact test and should be improved. |
| 1437 | for (MachineRegisterInfo::use_instr_nodbg_iterator I = MRI->use_instr_nodbg_begin(Reg), |
| 1438 | E = MRI->use_instr_nodbg_end(); I != E; ++I) { |
| 1439 | MachineInstr *MI = &*I; |
| 1440 | unsigned CmpReg1 = 0, CmpReg2 = 0; |
| 1441 | int CmpMask = 0, CmpValue = 0; |
| 1442 | |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1443 | if (!TII->analyzeCompare(*MI, CmpReg1, CmpReg2, CmpMask, CmpValue)) |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1444 | continue; |
| 1445 | |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1446 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1447 | SmallVector<MachineOperand, 2> Cond; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1448 | if (TII->analyzeBranch(*MI->getParent(), TBB, FBB, Cond, false)) |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1449 | continue; |
| 1450 | |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1451 | Comparison::Kind Cmp = |
| 1452 | getComparisonKind(MI->getOpcode(), nullptr, nullptr, 0); |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1453 | if (Cmp == 0) |
| 1454 | continue; |
| 1455 | if (TII->predOpcodeHasNot(Cond) ^ (TBB != MBB)) |
| 1456 | Cmp = Comparison::getNegatedComparison(Cmp); |
| 1457 | if (CmpReg2 != 0 && CmpReg2 == Reg) |
| 1458 | Cmp = Comparison::getSwappedComparison(Cmp); |
| 1459 | |
| 1460 | // Signed underflow is undefined. |
| 1461 | if (Comparison::isSigned(Cmp)) |
| 1462 | return false; |
| 1463 | |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1464 | // Check if there is a comparison of the initial value. If the initial value |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1465 | // is greater than or not equal to another value, then assume this is a |
| 1466 | // range check. |
| 1467 | if ((Cmp & Comparison::G) || Cmp == Comparison::NE) |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
| 1471 | // OK - this is a hack that needs to be improved. We really need to analyze |
| 1472 | // the instructions performed on the initial value. This works on the simplest |
| 1473 | // cases only. |
| 1474 | if (!Def->isCopy() && !Def->isPHI()) |
| 1475 | return false; |
| 1476 | |
| 1477 | return true; |
| 1478 | } |
| 1479 | |
| 1480 | bool HexagonHardwareLoops::checkForImmediate(const MachineOperand &MO, |
| 1481 | int64_t &Val) const { |
| 1482 | if (MO.isImm()) { |
| 1483 | Val = MO.getImm(); |
| 1484 | return true; |
| 1485 | } |
| 1486 | if (!MO.isReg()) |
| 1487 | return false; |
| 1488 | |
| 1489 | // MO is a register. Check whether it is defined as an immediate value, |
| 1490 | // and if so, get the value of it in TV. That value will then need to be |
| 1491 | // processed to handle potential subregisters in MO. |
| 1492 | int64_t TV; |
| 1493 | |
| 1494 | unsigned R = MO.getReg(); |
| 1495 | if (!TargetRegisterInfo::isVirtualRegister(R)) |
| 1496 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1497 | MachineInstr *DI = MRI->getVRegDef(R); |
| 1498 | unsigned DOpc = DI->getOpcode(); |
| 1499 | switch (DOpc) { |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1500 | case TargetOpcode::COPY: |
| Colin LeMahieu | 4af437f | 2014-12-09 20:23:30 +0000 | [diff] [blame] | 1501 | case Hexagon::A2_tfrsi: |
| Colin LeMahieu | 0f850bd | 2014-12-19 20:29:29 +0000 | [diff] [blame] | 1502 | case Hexagon::A2_tfrpi: |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 1503 | case Hexagon::CONST32: |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1504 | case Hexagon::CONST64: |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1505 | // Call recursively to avoid an extra check whether operand(1) is |
| 1506 | // indeed an immediate (it could be a global address, for example), |
| 1507 | // plus we can handle COPY at the same time. |
| 1508 | if (!checkForImmediate(DI->getOperand(1), TV)) |
| 1509 | return false; |
| 1510 | break; |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1511 | case Hexagon::A2_combineii: |
| 1512 | case Hexagon::A4_combineir: |
| 1513 | case Hexagon::A4_combineii: |
| 1514 | case Hexagon::A4_combineri: |
| 1515 | case Hexagon::A2_combinew: { |
| 1516 | const MachineOperand &S1 = DI->getOperand(1); |
| 1517 | const MachineOperand &S2 = DI->getOperand(2); |
| 1518 | int64_t V1, V2; |
| 1519 | if (!checkForImmediate(S1, V1) || !checkForImmediate(S2, V2)) |
| 1520 | return false; |
| Vitaly Buka | bcb6622 | 2017-02-11 12:44:03 +0000 | [diff] [blame] | 1521 | TV = V2 | (static_cast<uint64_t>(V1) << 32); |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1522 | break; |
| 1523 | } |
| 1524 | case TargetOpcode::REG_SEQUENCE: { |
| 1525 | const MachineOperand &S1 = DI->getOperand(1); |
| 1526 | const MachineOperand &S3 = DI->getOperand(3); |
| 1527 | int64_t V1, V3; |
| 1528 | if (!checkForImmediate(S1, V1) || !checkForImmediate(S3, V3)) |
| 1529 | return false; |
| 1530 | unsigned Sub2 = DI->getOperand(2).getImm(); |
| 1531 | unsigned Sub4 = DI->getOperand(4).getImm(); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1532 | if (Sub2 == Hexagon::isub_lo && Sub4 == Hexagon::isub_hi) |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1533 | TV = V1 | (V3 << 32); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1534 | else if (Sub2 == Hexagon::isub_hi && Sub4 == Hexagon::isub_lo) |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1535 | TV = V3 | (V1 << 32); |
| 1536 | else |
| 1537 | llvm_unreachable("Unexpected form of REG_SEQUENCE"); |
| 1538 | break; |
| 1539 | } |
| 1540 | |
| 1541 | default: |
| 1542 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1543 | } |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1544 | |
| Simon Pilgrim | 6ba672e | 2016-11-17 19:21:20 +0000 | [diff] [blame] | 1545 | // By now, we should have successfully obtained the immediate value defining |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1546 | // the register referenced in MO. Handle a potential use of a subregister. |
| 1547 | switch (MO.getSubReg()) { |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1548 | case Hexagon::isub_lo: |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1549 | Val = TV & 0xFFFFFFFFULL; |
| 1550 | break; |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1551 | case Hexagon::isub_hi: |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1552 | Val = (TV >> 32) & 0xFFFFFFFFULL; |
| 1553 | break; |
| 1554 | default: |
| 1555 | Val = TV; |
| 1556 | break; |
| 1557 | } |
| 1558 | return true; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1561 | void HexagonHardwareLoops::setImmediate(MachineOperand &MO, int64_t Val) { |
| 1562 | if (MO.isImm()) { |
| 1563 | MO.setImm(Val); |
| 1564 | return; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1567 | assert(MO.isReg()); |
| 1568 | unsigned R = MO.getReg(); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1569 | MachineInstr *DI = MRI->getVRegDef(R); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1570 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1571 | const TargetRegisterClass *RC = MRI->getRegClass(R); |
| 1572 | unsigned NewR = MRI->createVirtualRegister(RC); |
| 1573 | MachineBasicBlock &B = *DI->getParent(); |
| 1574 | DebugLoc DL = DI->getDebugLoc(); |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1575 | BuildMI(B, DI, DL, TII->get(DI->getOpcode()), NewR).addImm(Val); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1576 | MO.setReg(NewR); |
| 1577 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1578 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1579 | static bool isImmValidForOpcode(unsigned CmpOpc, int64_t Imm) { |
| 1580 | // These two instructions are not extendable. |
| 1581 | if (CmpOpc == Hexagon::A4_cmpbeqi) |
| 1582 | return isUInt<8>(Imm); |
| 1583 | if (CmpOpc == Hexagon::A4_cmpbgti) |
| 1584 | return isInt<8>(Imm); |
| 1585 | // The rest of the comparison-with-immediate instructions are extendable. |
| 1586 | return true; |
| 1587 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1588 | |
| 1589 | bool HexagonHardwareLoops::fixupInductionVariable(MachineLoop *L) { |
| 1590 | MachineBasicBlock *Header = L->getHeader(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1591 | MachineBasicBlock *Latch = L->getLoopLatch(); |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1592 | MachineBasicBlock *ExitingBlock = L->findLoopControlBlock(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1593 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1594 | if (!(Header && Latch && ExitingBlock)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1595 | return false; |
| 1596 | |
| 1597 | // These data structures follow the same concept as the corresponding |
| 1598 | // ones in findInductionRegister (where some comments are). |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1599 | using RegisterBump = std::pair<unsigned, int64_t>; |
| 1600 | using RegisterInduction = std::pair<unsigned, RegisterBump>; |
| 1601 | using RegisterInductionSet = std::set<RegisterInduction>; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1602 | |
| 1603 | // Register candidates for induction variables, with their associated bumps. |
| 1604 | RegisterInductionSet IndRegs; |
| 1605 | |
| 1606 | // Look for induction patterns: |
| 1607 | // vreg1 = PHI ..., [ latch, vreg2 ] |
| 1608 | // vreg2 = ADD vreg1, imm |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1609 | using instr_iterator = MachineBasicBlock::instr_iterator; |
| 1610 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1611 | for (instr_iterator I = Header->instr_begin(), E = Header->instr_end(); |
| 1612 | I != E && I->isPHI(); ++I) { |
| 1613 | MachineInstr *Phi = &*I; |
| 1614 | |
| 1615 | // Have a PHI instruction. |
| 1616 | for (unsigned i = 1, n = Phi->getNumOperands(); i < n; i += 2) { |
| 1617 | if (Phi->getOperand(i+1).getMBB() != Latch) |
| 1618 | continue; |
| 1619 | |
| 1620 | unsigned PhiReg = Phi->getOperand(i).getReg(); |
| 1621 | MachineInstr *DI = MRI->getVRegDef(PhiReg); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1622 | |
| Sjoerd Meijer | 724023a | 2016-09-14 08:20:03 +0000 | [diff] [blame] | 1623 | if (DI->getDesc().isAdd()) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1624 | // If the register operand to the add/sub is the PHI we are looking |
| 1625 | // at, this meets the induction pattern. |
| 1626 | unsigned IndReg = DI->getOperand(1).getReg(); |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1627 | MachineOperand &Opnd2 = DI->getOperand(2); |
| 1628 | int64_t V; |
| 1629 | if (MRI->getVRegDef(IndReg) == Phi && checkForImmediate(Opnd2, V)) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1630 | unsigned UpdReg = DI->getOperand(0).getReg(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1631 | IndRegs.insert(std::make_pair(UpdReg, std::make_pair(IndReg, V))); |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1632 | } |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1633 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1634 | } // for (i) |
| 1635 | } // for (instr) |
| 1636 | |
| 1637 | if (IndRegs.empty()) |
| 1638 | return false; |
| 1639 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1640 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1641 | SmallVector<MachineOperand,2> Cond; |
| 1642 | // AnalyzeBranch returns true if it fails to analyze branch. |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1643 | bool NotAnalyzed = TII->analyzeBranch(*ExitingBlock, TB, FB, Cond, false); |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1644 | if (NotAnalyzed || Cond.empty()) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1645 | return false; |
| 1646 | |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1647 | if (ExitingBlock != Latch && (TB == Latch || FB == Latch)) { |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1648 | MachineBasicBlock *LTB = nullptr, *LFB = nullptr; |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1649 | SmallVector<MachineOperand,2> LCond; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1650 | bool NotAnalyzed = TII->analyzeBranch(*Latch, LTB, LFB, LCond, false); |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1651 | if (NotAnalyzed) |
| 1652 | return false; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1653 | |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1654 | // Since latch is not the exiting block, the latch branch should be an |
| 1655 | // unconditional branch to the loop header. |
| 1656 | if (TB == Latch) |
| 1657 | TB = (LTB == Header) ? LTB : LFB; |
| 1658 | else |
| 1659 | FB = (LTB == Header) ? LTB : LFB; |
| 1660 | } |
| 1661 | if (TB != Header) { |
| 1662 | if (FB != Header) { |
| 1663 | // The latch/exit block does not go back to the header. |
| 1664 | return false; |
| 1665 | } |
| 1666 | // FB is the header (i.e., uncond. jump to branch header) |
| 1667 | // In this case, the LoopBody -> TB should not be a back edge otherwise |
| 1668 | // it could result in an infinite loop after conversion to hw_loop. |
| 1669 | // This case can happen when the Latch has two jumps like this: |
| 1670 | // Jmp_c OuterLoopHeader <-- TB |
| 1671 | // Jmp InnerLoopHeader <-- FB |
| 1672 | if (MDT->dominates(TB, FB)) |
| 1673 | return false; |
| 1674 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Expecting a predicate register as a condition. It won't be a hardware |
| 1677 | // predicate register at this point yet, just a vreg. |
| 1678 | // HexagonInstrInfo::AnalyzeBranch for negated branches inserts imm(0) |
| 1679 | // into Cond, followed by the predicate register. For non-negated branches |
| 1680 | // it's just the register. |
| 1681 | unsigned CSz = Cond.size(); |
| 1682 | if (CSz != 1 && CSz != 2) |
| 1683 | return false; |
| 1684 | |
| Brendon Cahoon | 254e656 | 2015-05-13 14:54:24 +0000 | [diff] [blame] | 1685 | if (!Cond[CSz-1].isReg()) |
| 1686 | return false; |
| 1687 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1688 | unsigned P = Cond[CSz-1].getReg(); |
| 1689 | MachineInstr *PredDef = MRI->getVRegDef(P); |
| 1690 | |
| 1691 | if (!PredDef->isCompare()) |
| 1692 | return false; |
| 1693 | |
| 1694 | SmallSet<unsigned,2> CmpRegs; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1695 | MachineOperand *CmpImmOp = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Go over all operands to the compare and look for immediate and register |
| 1698 | // operands. Assume that if the compare has a single register use and a |
| 1699 | // single immediate operand, then the register is being compared with the |
| 1700 | // immediate value. |
| 1701 | for (unsigned i = 0, n = PredDef->getNumOperands(); i < n; ++i) { |
| 1702 | MachineOperand &MO = PredDef->getOperand(i); |
| 1703 | if (MO.isReg()) { |
| 1704 | // Skip all implicit references. In one case there was: |
| 1705 | // %vreg140<def> = FCMPUGT32_rr %vreg138, %vreg139, %USR<imp-use> |
| 1706 | if (MO.isImplicit()) |
| 1707 | continue; |
| 1708 | if (MO.isUse()) { |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1709 | if (!isImmediate(MO)) { |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1710 | CmpRegs.insert(MO.getReg()); |
| 1711 | continue; |
| 1712 | } |
| 1713 | // Consider the register to be the "immediate" operand. |
| 1714 | if (CmpImmOp) |
| 1715 | return false; |
| 1716 | CmpImmOp = &MO; |
| 1717 | } |
| 1718 | } else if (MO.isImm()) { |
| 1719 | if (CmpImmOp) // A second immediate argument? Confusing. Bail out. |
| 1720 | return false; |
| 1721 | CmpImmOp = &MO; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1725 | if (CmpRegs.empty()) |
| 1726 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1727 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1728 | // Check if the compared register follows the order we want. Fix if needed. |
| 1729 | for (RegisterInductionSet::iterator I = IndRegs.begin(), E = IndRegs.end(); |
| 1730 | I != E; ++I) { |
| 1731 | // This is a success. If the register used in the comparison is one that |
| 1732 | // we have identified as a bumped (updated) induction register, there is |
| 1733 | // nothing to do. |
| 1734 | if (CmpRegs.count(I->first)) |
| 1735 | return true; |
| 1736 | |
| 1737 | // Otherwise, if the register being compared comes out of a PHI node, |
| 1738 | // and has been recognized as following the induction pattern, and is |
| 1739 | // compared against an immediate, we can fix it. |
| 1740 | const RegisterBump &RB = I->second; |
| 1741 | if (CmpRegs.count(RB.first)) { |
| Brendon Cahoon | 7c8a3b0 | 2015-05-14 20:36:19 +0000 | [diff] [blame] | 1742 | if (!CmpImmOp) { |
| 1743 | // If both operands to the compare instruction are registers, see if |
| 1744 | // it can be changed to use induction register as one of the operands. |
| 1745 | MachineInstr *IndI = nullptr; |
| 1746 | MachineInstr *nonIndI = nullptr; |
| 1747 | MachineOperand *IndMO = nullptr; |
| 1748 | MachineOperand *nonIndMO = nullptr; |
| 1749 | |
| 1750 | for (unsigned i = 1, n = PredDef->getNumOperands(); i < n; ++i) { |
| 1751 | MachineOperand &MO = PredDef->getOperand(i); |
| 1752 | if (MO.isReg() && MO.getReg() == RB.first) { |
| 1753 | DEBUG(dbgs() << "\n DefMI(" << i << ") = " |
| 1754 | << *(MRI->getVRegDef(I->first))); |
| 1755 | if (IndI) |
| 1756 | return false; |
| 1757 | |
| 1758 | IndI = MRI->getVRegDef(I->first); |
| 1759 | IndMO = &MO; |
| 1760 | } else if (MO.isReg()) { |
| 1761 | DEBUG(dbgs() << "\n DefMI(" << i << ") = " |
| 1762 | << *(MRI->getVRegDef(MO.getReg()))); |
| 1763 | if (nonIndI) |
| 1764 | return false; |
| 1765 | |
| 1766 | nonIndI = MRI->getVRegDef(MO.getReg()); |
| 1767 | nonIndMO = &MO; |
| 1768 | } |
| 1769 | } |
| 1770 | if (IndI && nonIndI && |
| 1771 | nonIndI->getOpcode() == Hexagon::A2_addi && |
| 1772 | nonIndI->getOperand(2).isImm() && |
| 1773 | nonIndI->getOperand(2).getImm() == - RB.second) { |
| 1774 | bool Order = orderBumpCompare(IndI, PredDef); |
| 1775 | if (Order) { |
| 1776 | IndMO->setReg(I->first); |
| 1777 | nonIndMO->setReg(nonIndI->getOperand(1).getReg()); |
| 1778 | return true; |
| 1779 | } |
| 1780 | } |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
| 1784 | // It is not valid to do this transformation on an unsigned comparison |
| 1785 | // because it may underflow. |
| Eugene Zelenko | 26e8c7d | 2016-12-16 01:00:40 +0000 | [diff] [blame] | 1786 | Comparison::Kind Cmp = |
| 1787 | getComparisonKind(PredDef->getOpcode(), nullptr, nullptr, 0); |
| Brendon Cahoon | 7c8a3b0 | 2015-05-14 20:36:19 +0000 | [diff] [blame] | 1788 | if (!Cmp || Comparison::isUnsigned(Cmp)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1789 | return false; |
| 1790 | |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1791 | // If the register is being compared against an immediate, try changing |
| 1792 | // the compare instruction to use induction register and adjust the |
| 1793 | // immediate operand. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1794 | int64_t CmpImm = getImmediate(*CmpImmOp); |
| 1795 | int64_t V = RB.second; |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1796 | // Handle Overflow (64-bit). |
| 1797 | if (((V > 0) && (CmpImm > INT64_MAX - V)) || |
| 1798 | ((V < 0) && (CmpImm < INT64_MIN - V))) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1799 | return false; |
| 1800 | CmpImm += V; |
| Brendon Cahoon | 9376e99 | 2015-05-14 14:15:08 +0000 | [diff] [blame] | 1801 | // Most comparisons of register against an immediate value allow |
| 1802 | // the immediate to be constant-extended. There are some exceptions |
| 1803 | // though. Make sure the new combination will work. |
| 1804 | if (CmpImmOp->isImm()) |
| 1805 | if (!isImmValidForOpcode(PredDef->getOpcode(), CmpImm)) |
| 1806 | return false; |
| 1807 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1808 | // Make sure that the compare happens after the bump. Otherwise, |
| 1809 | // after the fixup, the compare would use a yet-undefined register. |
| 1810 | MachineInstr *BumpI = MRI->getVRegDef(I->first); |
| 1811 | bool Order = orderBumpCompare(BumpI, PredDef); |
| 1812 | if (!Order) |
| 1813 | return false; |
| 1814 | |
| 1815 | // Finally, fix the compare instruction. |
| 1816 | setImmediate(*CmpImmOp, CmpImm); |
| 1817 | for (unsigned i = 0, n = PredDef->getNumOperands(); i < n; ++i) { |
| 1818 | MachineOperand &MO = PredDef->getOperand(i); |
| 1819 | if (MO.isReg() && MO.getReg() == RB.first) { |
| 1820 | MO.setReg(I->first); |
| 1821 | return true; |
| 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | return false; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| Krzysztof Parzyszek | 06a2b6b | 2016-07-27 21:20:54 +0000 | [diff] [blame] | 1830 | /// createPreheaderForLoop - Create a preheader for a given loop. |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1831 | MachineBasicBlock *HexagonHardwareLoops::createPreheaderForLoop( |
| 1832 | MachineLoop *L) { |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1833 | if (MachineBasicBlock *TmpPH = MLI->findLoopPreheader(L, SpecPreheader)) |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1834 | return TmpPH; |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1835 | if (!HWCreatePreheader) |
| 1836 | return nullptr; |
| 1837 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1838 | MachineBasicBlock *Header = L->getHeader(); |
| 1839 | MachineBasicBlock *Latch = L->getLoopLatch(); |
| Sjoerd Meijer | 5815671 | 2016-08-15 08:22:42 +0000 | [diff] [blame] | 1840 | MachineBasicBlock *ExitingBlock = L->findLoopControlBlock(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1841 | MachineFunction *MF = Header->getParent(); |
| 1842 | DebugLoc DL; |
| 1843 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1844 | #ifndef NDEBUG |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1845 | if ((!PHFn.empty()) && (PHFn != MF->getName())) |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1846 | return nullptr; |
| 1847 | #endif |
| 1848 | |
| 1849 | if (!Latch || !ExitingBlock || Header->hasAddressTaken()) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1850 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1851 | |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1852 | using instr_iterator = MachineBasicBlock::instr_iterator; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1853 | |
| 1854 | // Verify that all existing predecessors have analyzable branches |
| 1855 | // (or no branches at all). |
| Eugene Zelenko | 4d060b7 | 2017-07-29 00:56:56 +0000 | [diff] [blame] | 1856 | using MBBVector = std::vector<MachineBasicBlock *>; |
| 1857 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1858 | MBBVector Preds(Header->pred_begin(), Header->pred_end()); |
| 1859 | SmallVector<MachineOperand,2> Tmp1; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1860 | MachineBasicBlock *TB = nullptr, *FB = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1861 | |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1862 | if (TII->analyzeBranch(*ExitingBlock, TB, FB, Tmp1, false)) |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1863 | return nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1864 | |
| 1865 | for (MBBVector::iterator I = Preds.begin(), E = Preds.end(); I != E; ++I) { |
| 1866 | MachineBasicBlock *PB = *I; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1867 | bool NotAnalyzed = TII->analyzeBranch(*PB, TB, FB, Tmp1, false); |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1868 | if (NotAnalyzed) |
| 1869 | return nullptr; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1870 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1871 | |
| 1872 | MachineBasicBlock *NewPH = MF->CreateMachineBasicBlock(); |
| Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 1873 | MF->insert(Header->getIterator(), NewPH); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1874 | |
| 1875 | if (Header->pred_size() > 2) { |
| 1876 | // Ensure that the header has only two predecessors: the preheader and |
| 1877 | // the loop latch. Any additional predecessors of the header should |
| Brendon Cahoon | d11c92a | 2015-05-13 17:56:03 +0000 | [diff] [blame] | 1878 | // join at the newly created preheader. Inspect all PHI nodes from the |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1879 | // header and create appropriate corresponding PHI nodes in the preheader. |
| 1880 | |
| 1881 | for (instr_iterator I = Header->instr_begin(), E = Header->instr_end(); |
| 1882 | I != E && I->isPHI(); ++I) { |
| 1883 | MachineInstr *PN = &*I; |
| 1884 | |
| 1885 | const MCInstrDesc &PD = TII->get(TargetOpcode::PHI); |
| 1886 | MachineInstr *NewPN = MF->CreateMachineInstr(PD, DL); |
| 1887 | NewPH->insert(NewPH->end(), NewPN); |
| 1888 | |
| 1889 | unsigned PR = PN->getOperand(0).getReg(); |
| 1890 | const TargetRegisterClass *RC = MRI->getRegClass(PR); |
| 1891 | unsigned NewPR = MRI->createVirtualRegister(RC); |
| 1892 | NewPN->addOperand(MachineOperand::CreateReg(NewPR, true)); |
| 1893 | |
| 1894 | // Copy all non-latch operands of a header's PHI node to the newly |
| 1895 | // created PHI node in the preheader. |
| 1896 | for (unsigned i = 1, n = PN->getNumOperands(); i < n; i += 2) { |
| 1897 | unsigned PredR = PN->getOperand(i).getReg(); |
| Brendon Cahoon | 485bea74 | 2015-05-14 17:31:40 +0000 | [diff] [blame] | 1898 | unsigned PredRSub = PN->getOperand(i).getSubReg(); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1899 | MachineBasicBlock *PredB = PN->getOperand(i+1).getMBB(); |
| 1900 | if (PredB == Latch) |
| 1901 | continue; |
| 1902 | |
| Brendon Cahoon | 485bea74 | 2015-05-14 17:31:40 +0000 | [diff] [blame] | 1903 | MachineOperand MO = MachineOperand::CreateReg(PredR, false); |
| 1904 | MO.setSubReg(PredRSub); |
| 1905 | NewPN->addOperand(MO); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1906 | NewPN->addOperand(MachineOperand::CreateMBB(PredB)); |
| 1907 | } |
| 1908 | |
| 1909 | // Remove copied operands from the old PHI node and add the value |
| 1910 | // coming from the preheader's PHI. |
| 1911 | for (int i = PN->getNumOperands()-2; i > 0; i -= 2) { |
| 1912 | MachineBasicBlock *PredB = PN->getOperand(i+1).getMBB(); |
| 1913 | if (PredB != Latch) { |
| 1914 | PN->RemoveOperand(i+1); |
| 1915 | PN->RemoveOperand(i); |
| 1916 | } |
| 1917 | } |
| 1918 | PN->addOperand(MachineOperand::CreateReg(NewPR, false)); |
| 1919 | PN->addOperand(MachineOperand::CreateMBB(NewPH)); |
| 1920 | } |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1921 | } else { |
| 1922 | assert(Header->pred_size() == 2); |
| 1923 | |
| 1924 | // The header has only two predecessors, but the non-latch predecessor |
| 1925 | // is not a preheader (e.g. it has other successors, etc.) |
| 1926 | // In such a case we don't need any extra PHI nodes in the new preheader, |
| 1927 | // all we need is to adjust existing PHIs in the header to now refer to |
| 1928 | // the new preheader. |
| 1929 | for (instr_iterator I = Header->instr_begin(), E = Header->instr_end(); |
| 1930 | I != E && I->isPHI(); ++I) { |
| 1931 | MachineInstr *PN = &*I; |
| 1932 | for (unsigned i = 1, n = PN->getNumOperands(); i < n; i += 2) { |
| 1933 | MachineOperand &MO = PN->getOperand(i+1); |
| 1934 | if (MO.getMBB() != Latch) |
| 1935 | MO.setMBB(NewPH); |
| 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | // "Reroute" the CFG edges to link in the new preheader. |
| 1941 | // If any of the predecessors falls through to the header, insert a branch |
| 1942 | // to the new preheader in that place. |
| 1943 | SmallVector<MachineOperand,1> Tmp2; |
| 1944 | SmallVector<MachineOperand,1> EmptyCond; |
| 1945 | |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1946 | TB = FB = nullptr; |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1947 | |
| 1948 | for (MBBVector::iterator I = Preds.begin(), E = Preds.end(); I != E; ++I) { |
| 1949 | MachineBasicBlock *PB = *I; |
| 1950 | if (PB != Latch) { |
| 1951 | Tmp2.clear(); |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1952 | bool NotAnalyzed = TII->analyzeBranch(*PB, TB, FB, Tmp2, false); |
| Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1953 | (void)NotAnalyzed; // suppress compiler warning |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1954 | assert (!NotAnalyzed && "Should be analyzable!"); |
| 1955 | if (TB != Header && (Tmp2.empty() || FB != Header)) |
| Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1956 | TII->insertBranch(*PB, NewPH, nullptr, EmptyCond, DL); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1957 | PB->ReplaceUsesOfBlockWith(Header, NewPH); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | // It can happen that the latch block will fall through into the header. |
| 1962 | // Insert an unconditional branch to the header. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1963 | TB = FB = nullptr; |
| Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1964 | bool LatchNotAnalyzed = TII->analyzeBranch(*Latch, TB, FB, Tmp2, false); |
| Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1965 | (void)LatchNotAnalyzed; // suppress compiler warning |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1966 | assert (!LatchNotAnalyzed && "Should be analyzable!"); |
| 1967 | if (!TB && !FB) |
| Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1968 | TII->insertBranch(*Latch, Header, nullptr, EmptyCond, DL); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Finally, the branch from the preheader to the header. |
| Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1971 | TII->insertBranch(*NewPH, Header, nullptr, EmptyCond, DL); |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1972 | NewPH->addSuccessor(Header); |
| 1973 | |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1974 | MachineLoop *ParentLoop = L->getParentLoop(); |
| 1975 | if (ParentLoop) |
| 1976 | ParentLoop->addBasicBlockToLoop(NewPH, MLI->getBase()); |
| 1977 | |
| 1978 | // Update the dominator information with the new preheader. |
| 1979 | if (MDT) { |
| Krzysztof Parzyszek | 06a2b6b | 2016-07-27 21:20:54 +0000 | [diff] [blame] | 1980 | if (MachineDomTreeNode *HN = MDT->getNode(Header)) { |
| 1981 | if (MachineDomTreeNode *DHN = HN->getIDom()) { |
| 1982 | MDT->addNewBlock(NewPH, DHN->getBlock()); |
| 1983 | MDT->changeImmediateDominator(Header, NewPH); |
| 1984 | } |
| 1985 | } |
| Brendon Cahoon | bece8ed | 2015-05-08 20:18:21 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| Krzysztof Parzyszek | 9a278f1 | 2013-02-11 21:37:55 +0000 | [diff] [blame] | 1988 | return NewPH; |
| Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 1989 | } |