Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame^] | 1 | //===-- ARM64ExpandPseudoInsts.cpp - Expand pseudo instructions ---*- C++ -*-=// |
| 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 file contains a pass that expands pseudo instructions into target |
| 11 | // instructions to allow proper scheduling and other late optimizations. This |
| 12 | // pass should be run after register allocation but before the post-regalloc |
| 13 | // scheduling pass. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "MCTargetDesc/ARM64AddressingModes.h" |
| 18 | #include "ARM64InstrInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/Support/MathExtras.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | class ARM64ExpandPseudo : public MachineFunctionPass { |
| 26 | public: |
| 27 | static char ID; |
| 28 | ARM64ExpandPseudo() : MachineFunctionPass(ID) {} |
| 29 | |
| 30 | const ARM64InstrInfo *TII; |
| 31 | |
| 32 | virtual bool runOnMachineFunction(MachineFunction &Fn); |
| 33 | |
| 34 | virtual const char *getPassName() const { |
| 35 | return "ARM64 pseudo instruction expansion pass"; |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | bool expandMBB(MachineBasicBlock &MBB); |
| 40 | bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); |
| 41 | bool expandMOVImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 42 | unsigned BitSize); |
| 43 | }; |
| 44 | char ARM64ExpandPseudo::ID = 0; |
| 45 | } |
| 46 | |
| 47 | /// \brief Transfer implicit operands on the pseudo instruction to the |
| 48 | /// instructions created from the expansion. |
| 49 | static void transferImpOps(MachineInstr &OldMI, MachineInstrBuilder &UseMI, |
| 50 | MachineInstrBuilder &DefMI) { |
| 51 | const MCInstrDesc &Desc = OldMI.getDesc(); |
| 52 | for (unsigned i = Desc.getNumOperands(), e = OldMI.getNumOperands(); i != e; |
| 53 | ++i) { |
| 54 | const MachineOperand &MO = OldMI.getOperand(i); |
| 55 | assert(MO.isReg() && MO.getReg()); |
| 56 | if (MO.isUse()) |
| 57 | UseMI.addOperand(MO); |
| 58 | else |
| 59 | DefMI.addOperand(MO); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// \brief Helper function which extracts the specified 16-bit chunk from a |
| 64 | /// 64-bit value. |
| 65 | static uint64_t getChunk(uint64_t Imm, unsigned ChunkIdx) { |
| 66 | assert(ChunkIdx < 4 && "Out of range chunk index specified!"); |
| 67 | |
| 68 | return (Imm >> (ChunkIdx * 16)) & 0xFFFF; |
| 69 | } |
| 70 | |
| 71 | /// \brief Helper function which replicates a 16-bit chunk within a 64-bit |
| 72 | /// value. Indices correspond to element numbers in a v4i16. |
| 73 | static uint64_t replicateChunk(uint64_t Imm, unsigned FromIdx, unsigned ToIdx) { |
| 74 | assert((FromIdx < 4) && (ToIdx < 4) && "Out of range chunk index specified!"); |
| 75 | const unsigned ShiftAmt = ToIdx * 16; |
| 76 | |
| 77 | // Replicate the source chunk to the destination position. |
| 78 | const uint64_t Chunk = getChunk(Imm, FromIdx) << ShiftAmt; |
| 79 | // Clear the destination chunk. |
| 80 | Imm &= ~(0xFFFFLL << ShiftAmt); |
| 81 | // Insert the replicated chunk. |
| 82 | return Imm | Chunk; |
| 83 | } |
| 84 | |
| 85 | /// \brief Helper function which tries to materialize a 64-bit value with an |
| 86 | /// ORR + MOVK instruction sequence. |
| 87 | static bool tryOrrMovk(uint64_t UImm, uint64_t OrrImm, MachineInstr &MI, |
| 88 | MachineBasicBlock &MBB, |
| 89 | MachineBasicBlock::iterator &MBBI, |
| 90 | const ARM64InstrInfo *TII, unsigned ChunkIdx) { |
| 91 | assert(ChunkIdx < 4 && "Out of range chunk index specified!"); |
| 92 | const unsigned ShiftAmt = ChunkIdx * 16; |
| 93 | |
| 94 | uint64_t Encoding; |
| 95 | if (ARM64_AM::processLogicalImmediate(OrrImm, 64, Encoding)) { |
| 96 | // Create the ORR-immediate instruction. |
| 97 | MachineInstrBuilder MIB = |
| 98 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ORRXri)) |
| 99 | .addOperand(MI.getOperand(0)) |
| 100 | .addReg(ARM64::XZR) |
| 101 | .addImm(Encoding); |
| 102 | |
| 103 | // Create the MOVK instruction. |
| 104 | const unsigned Imm16 = getChunk(UImm, ChunkIdx); |
| 105 | const unsigned DstReg = MI.getOperand(0).getReg(); |
| 106 | const bool DstIsDead = MI.getOperand(0).isDead(); |
| 107 | MachineInstrBuilder MIB1 = |
| 108 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::MOVKXi)) |
| 109 | .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) |
| 110 | .addReg(DstReg) |
| 111 | .addImm(Imm16) |
| 112 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, ShiftAmt)); |
| 113 | |
| 114 | transferImpOps(MI, MIB, MIB1); |
| 115 | MI.eraseFromParent(); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /// \brief Check whether the given 16-bit chunk replicated to full 64-bit width |
| 123 | /// can be materialized with an ORR instruction. |
| 124 | static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) { |
| 125 | Chunk = (Chunk << 48) | (Chunk << 32) | (Chunk << 16) | Chunk; |
| 126 | |
| 127 | return ARM64_AM::processLogicalImmediate(Chunk, 64, Encoding); |
| 128 | } |
| 129 | |
| 130 | /// \brief Check for identical 16-bit chunks within the constant and if so |
| 131 | /// materialize them with a single ORR instruction. The remaining one or two |
| 132 | /// 16-bit chunks will be materialized with MOVK instructions. |
| 133 | /// |
| 134 | /// This allows us to materialize constants like |A|B|A|A| or |A|B|C|A| (order |
| 135 | /// of the chunks doesn't matter), assuming |A|A|A|A| can be materialized with |
| 136 | /// an ORR instruction. |
| 137 | /// |
| 138 | static bool tryToreplicateChunks(uint64_t UImm, MachineInstr &MI, |
| 139 | MachineBasicBlock &MBB, |
| 140 | MachineBasicBlock::iterator &MBBI, |
| 141 | const ARM64InstrInfo *TII) { |
| 142 | typedef DenseMap<uint64_t, unsigned> CountMap; |
| 143 | CountMap Counts; |
| 144 | |
| 145 | // Scan the constant and count how often every chunk occurs. |
| 146 | for (unsigned Idx = 0; Idx < 4; ++Idx) |
| 147 | ++Counts[getChunk(UImm, Idx)]; |
| 148 | |
| 149 | // Traverse the chunks to find one which occurs more than once. |
| 150 | for (CountMap::const_iterator Chunk = Counts.begin(), End = Counts.end(); |
| 151 | Chunk != End; ++Chunk) { |
| 152 | const uint64_t ChunkVal = Chunk->first; |
| 153 | const unsigned Count = Chunk->second; |
| 154 | |
| 155 | uint64_t Encoding = 0; |
| 156 | |
| 157 | // We are looking for chunks which have two or three instances and can be |
| 158 | // materialized with an ORR instruction. |
| 159 | if ((Count != 2 && Count != 3) || !canUseOrr(ChunkVal, Encoding)) |
| 160 | continue; |
| 161 | |
| 162 | const bool CountThree = Count == 3; |
| 163 | // Create the ORR-immediate instruction. |
| 164 | MachineInstrBuilder MIB = |
| 165 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ORRXri)) |
| 166 | .addOperand(MI.getOperand(0)) |
| 167 | .addReg(ARM64::XZR) |
| 168 | .addImm(Encoding); |
| 169 | |
| 170 | const unsigned DstReg = MI.getOperand(0).getReg(); |
| 171 | const bool DstIsDead = MI.getOperand(0).isDead(); |
| 172 | |
| 173 | unsigned ShiftAmt = 0; |
| 174 | uint64_t Imm16 = 0; |
| 175 | // Find the first chunk not materialized with the ORR instruction. |
| 176 | for (; ShiftAmt < 64; ShiftAmt += 16) { |
| 177 | Imm16 = (UImm >> ShiftAmt) & 0xFFFF; |
| 178 | |
| 179 | if (Imm16 != ChunkVal) |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | // Create the first MOVK instruction. |
| 184 | MachineInstrBuilder MIB1 = |
| 185 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::MOVKXi)) |
| 186 | .addReg(DstReg, |
| 187 | RegState::Define | getDeadRegState(DstIsDead && CountThree)) |
| 188 | .addReg(DstReg) |
| 189 | .addImm(Imm16) |
| 190 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, ShiftAmt)); |
| 191 | |
| 192 | // In case we have three instances the whole constant is now materialized |
| 193 | // and we can exit. |
| 194 | if (CountThree) { |
| 195 | transferImpOps(MI, MIB, MIB1); |
| 196 | MI.eraseFromParent(); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | // Find the remaining chunk which needs to be materialized. |
| 201 | for (ShiftAmt += 16; ShiftAmt < 64; ShiftAmt += 16) { |
| 202 | Imm16 = (UImm >> ShiftAmt) & 0xFFFF; |
| 203 | |
| 204 | if (Imm16 != ChunkVal) |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | // Create the second MOVK instruction. |
| 209 | MachineInstrBuilder MIB2 = |
| 210 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::MOVKXi)) |
| 211 | .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) |
| 212 | .addReg(DstReg) |
| 213 | .addImm(Imm16) |
| 214 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, ShiftAmt)); |
| 215 | |
| 216 | transferImpOps(MI, MIB, MIB2); |
| 217 | MI.eraseFromParent(); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | /// \brief Check whether this chunk matches the pattern '1...0...'. This pattern |
| 225 | /// starts a contiguous sequence of ones if we look at the bits from the LSB |
| 226 | /// towards the MSB. |
| 227 | static bool isStartChunk(uint64_t Chunk) { |
| 228 | if (Chunk == 0 || Chunk == UINT64_MAX) |
| 229 | return false; |
| 230 | |
| 231 | return (CountLeadingOnes_64(Chunk) + countTrailingZeros(Chunk)) == 64; |
| 232 | } |
| 233 | |
| 234 | /// \brief Check whether this chunk matches the pattern '0...1...' This pattern |
| 235 | /// ends a contiguous sequence of ones if we look at the bits from the LSB |
| 236 | /// towards the MSB. |
| 237 | static bool isEndChunk(uint64_t Chunk) { |
| 238 | if (Chunk == 0 || Chunk == UINT64_MAX) |
| 239 | return false; |
| 240 | |
| 241 | return (countLeadingZeros(Chunk) + CountTrailingOnes_64(Chunk)) == 64; |
| 242 | } |
| 243 | |
| 244 | /// \brief Clear or set all bits in the chunk at the given index. |
| 245 | static uint64_t updateImm(uint64_t Imm, unsigned Idx, bool Clear) { |
| 246 | const uint64_t Mask = 0xFFFF; |
| 247 | |
| 248 | if (Clear) |
| 249 | // Clear chunk in the immediate. |
| 250 | Imm &= ~(Mask << (Idx * 16)); |
| 251 | else |
| 252 | // Set all bits in the immediate for the particular chunk. |
| 253 | Imm |= Mask << (Idx * 16); |
| 254 | |
| 255 | return Imm; |
| 256 | } |
| 257 | |
| 258 | /// \brief Check whether the constant contains a sequence of contiguous ones, |
| 259 | /// which might be interrupted by one or two chunks. If so, materialize the |
| 260 | /// sequence of contiguous ones with an ORR instruction. |
| 261 | /// Materialize the chunks which are either interrupting the sequence or outside |
| 262 | /// of the sequence with a MOVK instruction. |
| 263 | /// |
| 264 | /// Assuming S is a chunk which starts the sequence (1...0...), E is a chunk |
| 265 | /// which ends the sequence (0...1...). Then we are looking for constants which |
| 266 | /// contain at least one S and E chunk. |
| 267 | /// E.g. |E|A|B|S|, |A|E|B|S| or |A|B|E|S|. |
| 268 | /// |
| 269 | /// We are also looking for constants like |S|A|B|E| where the contiguous |
| 270 | /// sequence of ones wraps around the MSB into the LSB. |
| 271 | /// |
| 272 | static bool trySequenceOfOnes(uint64_t UImm, MachineInstr &MI, |
| 273 | MachineBasicBlock &MBB, |
| 274 | MachineBasicBlock::iterator &MBBI, |
| 275 | const ARM64InstrInfo *TII) { |
| 276 | const int NotSet = -1; |
| 277 | const uint64_t Mask = 0xFFFF; |
| 278 | |
| 279 | int StartIdx = NotSet; |
| 280 | int EndIdx = NotSet; |
| 281 | // Try to find the chunks which start/end a contiguous sequence of ones. |
| 282 | for (int Idx = 0; Idx < 4; ++Idx) { |
| 283 | int64_t Chunk = getChunk(UImm, Idx); |
| 284 | // Sign extend the 16-bit chunk to 64-bit. |
| 285 | Chunk = (Chunk << 48) >> 48; |
| 286 | |
| 287 | if (isStartChunk(Chunk)) |
| 288 | StartIdx = Idx; |
| 289 | else if (isEndChunk(Chunk)) |
| 290 | EndIdx = Idx; |
| 291 | } |
| 292 | |
| 293 | // Early exit in case we can't find a start/end chunk. |
| 294 | if (StartIdx == NotSet || EndIdx == NotSet) |
| 295 | return false; |
| 296 | |
| 297 | // Outside of the contiguous sequence of ones everything needs to be zero. |
| 298 | uint64_t Outside = 0; |
| 299 | // Chunks between the start and end chunk need to have all their bits set. |
| 300 | uint64_t Inside = Mask; |
| 301 | |
| 302 | // If our contiguous sequence of ones wraps around from the MSB into the LSB, |
| 303 | // just swap indices and pretend we are materializing a contiguous sequence |
| 304 | // of zeros surrounded by a contiguous sequence of ones. |
| 305 | if (StartIdx > EndIdx) { |
| 306 | std::swap(StartIdx, EndIdx); |
| 307 | std::swap(Outside, Inside); |
| 308 | } |
| 309 | |
| 310 | uint64_t OrrImm = UImm; |
| 311 | int FirstMovkIdx = NotSet; |
| 312 | int SecondMovkIdx = NotSet; |
| 313 | |
| 314 | // Find out which chunks we need to patch up to obtain a contiguous sequence |
| 315 | // of ones. |
| 316 | for (int Idx = 0; Idx < 4; ++Idx) { |
| 317 | const uint64_t Chunk = getChunk(UImm, Idx); |
| 318 | |
| 319 | // Check whether we are looking at a chunk which is not part of the |
| 320 | // contiguous sequence of ones. |
| 321 | if ((Idx < StartIdx || EndIdx < Idx) && Chunk != Outside) { |
| 322 | OrrImm = updateImm(OrrImm, Idx, Outside == 0); |
| 323 | |
| 324 | // Remember the index we need to patch. |
| 325 | if (FirstMovkIdx == NotSet) |
| 326 | FirstMovkIdx = Idx; |
| 327 | else |
| 328 | SecondMovkIdx = Idx; |
| 329 | |
| 330 | // Check whether we are looking a chunk which is part of the contiguous |
| 331 | // sequence of ones. |
| 332 | } else if (Idx > StartIdx && Idx < EndIdx && Chunk != Inside) { |
| 333 | OrrImm = updateImm(OrrImm, Idx, Inside != Mask); |
| 334 | |
| 335 | // Remember the index we need to patch. |
| 336 | if (FirstMovkIdx == NotSet) |
| 337 | FirstMovkIdx = Idx; |
| 338 | else |
| 339 | SecondMovkIdx = Idx; |
| 340 | } |
| 341 | } |
| 342 | assert(FirstMovkIdx != NotSet && "Constant materializable with single ORR!"); |
| 343 | |
| 344 | // Create the ORR-immediate instruction. |
| 345 | uint64_t Encoding = 0; |
| 346 | ARM64_AM::processLogicalImmediate(OrrImm, 64, Encoding); |
| 347 | MachineInstrBuilder MIB = |
| 348 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ORRXri)) |
| 349 | .addOperand(MI.getOperand(0)) |
| 350 | .addReg(ARM64::XZR) |
| 351 | .addImm(Encoding); |
| 352 | |
| 353 | const unsigned DstReg = MI.getOperand(0).getReg(); |
| 354 | const bool DstIsDead = MI.getOperand(0).isDead(); |
| 355 | |
| 356 | const bool SingleMovk = SecondMovkIdx == NotSet; |
| 357 | // Create the first MOVK instruction. |
| 358 | MachineInstrBuilder MIB1 = |
| 359 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::MOVKXi)) |
| 360 | .addReg(DstReg, |
| 361 | RegState::Define | getDeadRegState(DstIsDead && SingleMovk)) |
| 362 | .addReg(DstReg) |
| 363 | .addImm(getChunk(UImm, FirstMovkIdx)) |
| 364 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, FirstMovkIdx * 16)); |
| 365 | |
| 366 | // Early exit in case we only need to emit a single MOVK instruction. |
| 367 | if (SingleMovk) { |
| 368 | transferImpOps(MI, MIB, MIB1); |
| 369 | MI.eraseFromParent(); |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | // Create the second MOVK instruction. |
| 374 | MachineInstrBuilder MIB2 = |
| 375 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::MOVKXi)) |
| 376 | .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead)) |
| 377 | .addReg(DstReg) |
| 378 | .addImm(getChunk(UImm, SecondMovkIdx)) |
| 379 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, SecondMovkIdx * 16)); |
| 380 | |
| 381 | transferImpOps(MI, MIB, MIB2); |
| 382 | MI.eraseFromParent(); |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | /// \brief Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more |
| 387 | /// real move-immediate instructions to synthesize the immediate. |
| 388 | bool ARM64ExpandPseudo::expandMOVImm(MachineBasicBlock &MBB, |
| 389 | MachineBasicBlock::iterator MBBI, |
| 390 | unsigned BitSize) { |
| 391 | MachineInstr &MI = *MBBI; |
| 392 | uint64_t Imm = MI.getOperand(1).getImm(); |
| 393 | const unsigned Mask = 0xFFFF; |
| 394 | |
| 395 | // Try a MOVI instruction (aka ORR-immediate with the zero register). |
| 396 | uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize); |
| 397 | uint64_t Encoding; |
| 398 | if (ARM64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) { |
| 399 | unsigned Opc = (BitSize == 32 ? ARM64::ORRWri : ARM64::ORRXri); |
| 400 | MachineInstrBuilder MIB = |
| 401 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)) |
| 402 | .addOperand(MI.getOperand(0)) |
| 403 | .addReg(BitSize == 32 ? ARM64::WZR : ARM64::XZR) |
| 404 | .addImm(Encoding); |
| 405 | transferImpOps(MI, MIB, MIB); |
| 406 | MI.eraseFromParent(); |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | // Scan the immediate and count the number of 16-bit chunks which are either |
| 411 | // all ones or all zeros. |
| 412 | unsigned OneChunks = 0; |
| 413 | unsigned ZeroChunks = 0; |
| 414 | for (unsigned Shift = 0; Shift < BitSize; Shift += 16) { |
| 415 | const unsigned Chunk = (Imm >> Shift) & Mask; |
| 416 | if (Chunk == Mask) |
| 417 | OneChunks++; |
| 418 | else if (Chunk == 0) |
| 419 | ZeroChunks++; |
| 420 | } |
| 421 | |
| 422 | // Since we can't materialize the constant with a single ORR instruction, |
| 423 | // let's see whether we can materialize 3/4 of the constant with an ORR |
| 424 | // instruction and use an additional MOVK instruction to materialize the |
| 425 | // remaining 1/4. |
| 426 | // |
| 427 | // We are looking for constants with a pattern like: |A|X|B|X| or |X|A|X|B|. |
| 428 | // |
| 429 | // E.g. assuming |A|X|A|X| is a pattern which can be materialized with ORR, |
| 430 | // we would create the following instruction sequence: |
| 431 | // |
| 432 | // ORR x0, xzr, |A|X|A|X| |
| 433 | // MOVK x0, |B|, LSL #16 |
| 434 | // |
| 435 | // Only look at 64-bit constants which can't be materialized with a single |
| 436 | // instruction e.g. which have less than either three all zero or all one |
| 437 | // chunks. |
| 438 | // |
| 439 | // Ignore 32-bit constants here, they always can be materialized with a |
| 440 | // MOVZ/MOVN + MOVK pair. Since the 32-bit constant can't be materialized |
| 441 | // with a single ORR, the best sequence we can achieve is a ORR + MOVK pair. |
| 442 | // Thus we fall back to the default code below which in the best case creates |
| 443 | // a single MOVZ/MOVN instruction (in case one chunk is all zero or all one). |
| 444 | // |
| 445 | if (BitSize == 64 && OneChunks < 3 && ZeroChunks < 3) { |
| 446 | // If we interpret the 64-bit constant as a v4i16, are elements 0 and 2 |
| 447 | // identical? |
| 448 | if (getChunk(UImm, 0) == getChunk(UImm, 2)) { |
| 449 | // See if we can come up with a constant which can be materialized with |
| 450 | // ORR-immediate by replicating element 3 into element 1. |
| 451 | uint64_t OrrImm = replicateChunk(UImm, 3, 1); |
| 452 | if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 1)) |
| 453 | return true; |
| 454 | |
| 455 | // See if we can come up with a constant which can be materialized with |
| 456 | // ORR-immediate by replicating element 1 into element 3. |
| 457 | OrrImm = replicateChunk(UImm, 1, 3); |
| 458 | if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 3)) |
| 459 | return true; |
| 460 | |
| 461 | // If we interpret the 64-bit constant as a v4i16, are elements 1 and 3 |
| 462 | // identical? |
| 463 | } else if (getChunk(UImm, 1) == getChunk(UImm, 3)) { |
| 464 | // See if we can come up with a constant which can be materialized with |
| 465 | // ORR-immediate by replicating element 2 into element 0. |
| 466 | uint64_t OrrImm = replicateChunk(UImm, 2, 0); |
| 467 | if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 0)) |
| 468 | return true; |
| 469 | |
| 470 | // See if we can come up with a constant which can be materialized with |
| 471 | // ORR-immediate by replicating element 1 into element 3. |
| 472 | OrrImm = replicateChunk(UImm, 0, 2); |
| 473 | if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 2)) |
| 474 | return true; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // Check for identical 16-bit chunks within the constant and if so materialize |
| 479 | // them with a single ORR instruction. The remaining one or two 16-bit chunks |
| 480 | // will be materialized with MOVK instructions. |
| 481 | if (BitSize == 64 && tryToreplicateChunks(UImm, MI, MBB, MBBI, TII)) |
| 482 | return true; |
| 483 | |
| 484 | // Check whether the constant contains a sequence of contiguous ones, which |
| 485 | // might be interrupted by one or two chunks. If so, materialize the sequence |
| 486 | // of contiguous ones with an ORR instruction. Materialize the chunks which |
| 487 | // are either interrupting the sequence or outside of the sequence with a |
| 488 | // MOVK instruction. |
| 489 | if (BitSize == 64 && trySequenceOfOnes(UImm, MI, MBB, MBBI, TII)) |
| 490 | return true; |
| 491 | |
| 492 | // Use a MOVZ or MOVN instruction to set the high bits, followed by one or |
| 493 | // more MOVK instructions to insert additional 16-bit portions into the |
| 494 | // lower bits. |
| 495 | bool isNeg = false; |
| 496 | |
| 497 | // Use MOVN to materialize the high bits if we have more all one chunks |
| 498 | // than all zero chunks. |
| 499 | if (OneChunks > ZeroChunks) { |
| 500 | isNeg = true; |
| 501 | Imm = ~Imm; |
| 502 | } |
| 503 | |
| 504 | unsigned FirstOpc; |
| 505 | if (BitSize == 32) { |
| 506 | Imm &= (1LL << 32) - 1; |
| 507 | FirstOpc = (isNeg ? ARM64::MOVNWi : ARM64::MOVZWi); |
| 508 | } else { |
| 509 | FirstOpc = (isNeg ? ARM64::MOVNXi : ARM64::MOVZXi); |
| 510 | } |
| 511 | unsigned Shift = 0; // LSL amount for high bits with MOVZ/MOVN |
| 512 | unsigned LastShift = 0; // LSL amount for last MOVK |
| 513 | if (Imm != 0) { |
| 514 | unsigned LZ = countLeadingZeros(Imm); |
| 515 | unsigned TZ = countTrailingZeros(Imm); |
| 516 | Shift = ((63 - LZ) / 16) * 16; |
| 517 | LastShift = (TZ / 16) * 16; |
| 518 | } |
| 519 | unsigned Imm16 = (Imm >> Shift) & Mask; |
| 520 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 521 | bool DstIsDead = MI.getOperand(0).isDead(); |
| 522 | MachineInstrBuilder MIB1 = |
| 523 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(FirstOpc)) |
| 524 | .addReg(DstReg, RegState::Define | |
| 525 | getDeadRegState(DstIsDead && Shift == LastShift)) |
| 526 | .addImm(Imm16) |
| 527 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, Shift)); |
| 528 | |
| 529 | // If a MOVN was used for the high bits of a negative value, flip the rest |
| 530 | // of the bits back for use with MOVK. |
| 531 | if (isNeg) |
| 532 | Imm = ~Imm; |
| 533 | |
| 534 | if (Shift == LastShift) { |
| 535 | transferImpOps(MI, MIB1, MIB1); |
| 536 | MI.eraseFromParent(); |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | MachineInstrBuilder MIB2; |
| 541 | unsigned Opc = (BitSize == 32 ? ARM64::MOVKWi : ARM64::MOVKXi); |
| 542 | while (Shift != LastShift) { |
| 543 | Shift -= 16; |
| 544 | Imm16 = (Imm >> Shift) & Mask; |
| 545 | if (Imm16 == (isNeg ? Mask : 0)) |
| 546 | continue; // This 16-bit portion is already set correctly. |
| 547 | MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc)) |
| 548 | .addReg(DstReg, |
| 549 | RegState::Define | |
| 550 | getDeadRegState(DstIsDead && Shift == LastShift)) |
| 551 | .addReg(DstReg) |
| 552 | .addImm(Imm16) |
| 553 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, Shift)); |
| 554 | } |
| 555 | |
| 556 | transferImpOps(MI, MIB1, MIB2); |
| 557 | MI.eraseFromParent(); |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | /// \brief If MBBI references a pseudo instruction that should be expanded here, |
| 562 | /// do the expansion and return true. Otherwise return false. |
| 563 | bool ARM64ExpandPseudo::expandMI(MachineBasicBlock &MBB, |
| 564 | MachineBasicBlock::iterator MBBI) { |
| 565 | MachineInstr &MI = *MBBI; |
| 566 | unsigned Opcode = MI.getOpcode(); |
| 567 | switch (Opcode) { |
| 568 | default: |
| 569 | break; |
| 570 | |
| 571 | case ARM64::ADDWrr: |
| 572 | case ARM64::SUBWrr: |
| 573 | case ARM64::ADDXrr: |
| 574 | case ARM64::SUBXrr: |
| 575 | case ARM64::ADDSWrr: |
| 576 | case ARM64::SUBSWrr: |
| 577 | case ARM64::ADDSXrr: |
| 578 | case ARM64::SUBSXrr: |
| 579 | case ARM64::ANDWrr: |
| 580 | case ARM64::ANDXrr: |
| 581 | case ARM64::BICWrr: |
| 582 | case ARM64::BICXrr: |
| 583 | case ARM64::EONWrr: |
| 584 | case ARM64::EONXrr: |
| 585 | case ARM64::EORWrr: |
| 586 | case ARM64::EORXrr: |
| 587 | case ARM64::ORNWrr: |
| 588 | case ARM64::ORNXrr: |
| 589 | case ARM64::ORRWrr: |
| 590 | case ARM64::ORRXrr: { |
| 591 | unsigned Opcode; |
| 592 | switch (MI.getOpcode()) { |
| 593 | default: |
| 594 | return false; |
| 595 | case ARM64::ADDWrr: Opcode = ARM64::ADDWrs; break; |
| 596 | case ARM64::SUBWrr: Opcode = ARM64::SUBWrs; break; |
| 597 | case ARM64::ADDXrr: Opcode = ARM64::ADDXrs; break; |
| 598 | case ARM64::SUBXrr: Opcode = ARM64::SUBXrs; break; |
| 599 | case ARM64::ADDSWrr: Opcode = ARM64::ADDSWrs; break; |
| 600 | case ARM64::SUBSWrr: Opcode = ARM64::SUBSWrs; break; |
| 601 | case ARM64::ADDSXrr: Opcode = ARM64::ADDSXrs; break; |
| 602 | case ARM64::SUBSXrr: Opcode = ARM64::SUBSXrs; break; |
| 603 | case ARM64::ANDWrr: Opcode = ARM64::ANDWrs; break; |
| 604 | case ARM64::ANDXrr: Opcode = ARM64::ANDXrs; break; |
| 605 | case ARM64::BICWrr: Opcode = ARM64::BICWrs; break; |
| 606 | case ARM64::BICXrr: Opcode = ARM64::BICXrs; break; |
| 607 | case ARM64::EONWrr: Opcode = ARM64::EONWrs; break; |
| 608 | case ARM64::EONXrr: Opcode = ARM64::EONXrs; break; |
| 609 | case ARM64::EORWrr: Opcode = ARM64::EORWrs; break; |
| 610 | case ARM64::EORXrr: Opcode = ARM64::EORXrs; break; |
| 611 | case ARM64::ORNWrr: Opcode = ARM64::ORNWrs; break; |
| 612 | case ARM64::ORNXrr: Opcode = ARM64::ORNXrs; break; |
| 613 | case ARM64::ORRWrr: Opcode = ARM64::ORRWrs; break; |
| 614 | case ARM64::ORRXrr: Opcode = ARM64::ORRXrs; break; |
| 615 | } |
| 616 | MachineInstrBuilder MIB1 = |
| 617 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opcode), |
| 618 | MI.getOperand(0).getReg()) |
| 619 | .addOperand(MI.getOperand(1)) |
| 620 | .addOperand(MI.getOperand(2)) |
| 621 | .addImm(ARM64_AM::getShifterImm(ARM64_AM::LSL, 0)); |
| 622 | transferImpOps(MI, MIB1, MIB1); |
| 623 | MI.eraseFromParent(); |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | case ARM64::LOADgot: { |
| 628 | // Expand into ADRP + LDR. |
| 629 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 630 | const MachineOperand &MO1 = MI.getOperand(1); |
| 631 | unsigned Flags = MO1.getTargetFlags(); |
| 632 | MachineInstrBuilder MIB1 = |
| 633 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ADRP), DstReg); |
| 634 | MachineInstrBuilder MIB2 = |
| 635 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::LDRXui)) |
| 636 | .addOperand(MI.getOperand(0)) |
| 637 | .addReg(DstReg); |
| 638 | |
| 639 | if (MO1.isGlobal()) { |
| 640 | MIB1.addGlobalAddress(MO1.getGlobal(), 0, Flags | ARM64II::MO_PAGE); |
| 641 | MIB2.addGlobalAddress(MO1.getGlobal(), 0, |
| 642 | Flags | ARM64II::MO_PAGEOFF | ARM64II::MO_NC); |
| 643 | } else if (MO1.isSymbol()) { |
| 644 | MIB1.addExternalSymbol(MO1.getSymbolName(), Flags | ARM64II::MO_PAGE); |
| 645 | MIB2.addExternalSymbol(MO1.getSymbolName(), |
| 646 | Flags | ARM64II::MO_PAGEOFF | ARM64II::MO_NC); |
| 647 | } else { |
| 648 | assert(MO1.isCPI() && |
| 649 | "Only expect globals, externalsymbols, or constant pools"); |
| 650 | MIB1.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(), |
| 651 | Flags | ARM64II::MO_PAGE); |
| 652 | MIB2.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(), |
| 653 | Flags | ARM64II::MO_PAGEOFF | ARM64II::MO_NC); |
| 654 | } |
| 655 | |
| 656 | transferImpOps(MI, MIB1, MIB2); |
| 657 | MI.eraseFromParent(); |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | case ARM64::MOVaddr: |
| 662 | case ARM64::MOVaddrJT: |
| 663 | case ARM64::MOVaddrCP: |
| 664 | case ARM64::MOVaddrBA: |
| 665 | case ARM64::MOVaddrTLS: |
| 666 | case ARM64::MOVaddrEXT: { |
| 667 | // Expand into ADRP + ADD. |
| 668 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 669 | MachineInstrBuilder MIB1 = |
| 670 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ADRP), DstReg) |
| 671 | .addOperand(MI.getOperand(1)); |
| 672 | |
| 673 | MachineInstrBuilder MIB2 = |
| 674 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::ADDXri)) |
| 675 | .addOperand(MI.getOperand(0)) |
| 676 | .addReg(DstReg) |
| 677 | .addOperand(MI.getOperand(2)) |
| 678 | .addImm(0); |
| 679 | |
| 680 | transferImpOps(MI, MIB1, MIB2); |
| 681 | MI.eraseFromParent(); |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | case ARM64::MOVi32imm: |
| 686 | return expandMOVImm(MBB, MBBI, 32); |
| 687 | case ARM64::MOVi64imm: |
| 688 | return expandMOVImm(MBB, MBBI, 64); |
| 689 | case ARM64::RET_ReallyLR: |
| 690 | BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM64::RET)) |
| 691 | .addReg(ARM64::LR); |
| 692 | MI.eraseFromParent(); |
| 693 | return true; |
| 694 | } |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | /// \brief Iterate over the instructions in basic block MBB and expand any |
| 699 | /// pseudo instructions. Return true if anything was modified. |
| 700 | bool ARM64ExpandPseudo::expandMBB(MachineBasicBlock &MBB) { |
| 701 | bool Modified = false; |
| 702 | |
| 703 | MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); |
| 704 | while (MBBI != E) { |
| 705 | MachineBasicBlock::iterator NMBBI = std::next(MBBI); |
| 706 | Modified |= expandMI(MBB, MBBI); |
| 707 | MBBI = NMBBI; |
| 708 | } |
| 709 | |
| 710 | return Modified; |
| 711 | } |
| 712 | |
| 713 | bool ARM64ExpandPseudo::runOnMachineFunction(MachineFunction &MF) { |
| 714 | TII = static_cast<const ARM64InstrInfo *>(MF.getTarget().getInstrInfo()); |
| 715 | |
| 716 | bool Modified = false; |
| 717 | for (MachineFunction::iterator MFI = MF.begin(), E = MF.end(); MFI != E; |
| 718 | ++MFI) |
| 719 | Modified |= expandMBB(*MFI); |
| 720 | return Modified; |
| 721 | } |
| 722 | |
| 723 | /// \brief Returns an instance of the pseudo instruction expansion pass. |
| 724 | FunctionPass *llvm::createARM64ExpandPseudoPass() { |
| 725 | return new ARM64ExpandPseudo(); |
| 726 | } |