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