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