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