xref: /aosp_15_r20/external/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //==-- AArch64ExpandPseudoInsts.cpp - Expand pseudo instructions --*- C++ -*-=//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains a pass that expands pseudo instructions into target
11*9880d681SAndroid Build Coastguard Worker // instructions to allow proper scheduling and other late optimizations.  This
12*9880d681SAndroid Build Coastguard Worker // pass should be run after register allocation but before the post-regalloc
13*9880d681SAndroid Build Coastguard Worker // scheduling pass.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/AArch64AddressingModes.h"
18*9880d681SAndroid Build Coastguard Worker #include "AArch64InstrInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "AArch64Subtarget.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LivePhysRegs.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
24*9880d681SAndroid Build Coastguard Worker using namespace llvm;
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker namespace llvm {
27*9880d681SAndroid Build Coastguard Worker void initializeAArch64ExpandPseudoPass(PassRegistry &);
28*9880d681SAndroid Build Coastguard Worker }
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker #define AARCH64_EXPAND_PSEUDO_NAME "AArch64 pseudo instruction expansion pass"
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker namespace {
33*9880d681SAndroid Build Coastguard Worker class AArch64ExpandPseudo : public MachineFunctionPass {
34*9880d681SAndroid Build Coastguard Worker public:
35*9880d681SAndroid Build Coastguard Worker   static char ID;
AArch64ExpandPseudo()36*9880d681SAndroid Build Coastguard Worker   AArch64ExpandPseudo() : MachineFunctionPass(ID) {
37*9880d681SAndroid Build Coastguard Worker     initializeAArch64ExpandPseudoPass(*PassRegistry::getPassRegistry());
38*9880d681SAndroid Build Coastguard Worker   }
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker   const AArch64InstrInfo *TII;
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &Fn) override;
43*9880d681SAndroid Build Coastguard Worker 
getPassName() const44*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
45*9880d681SAndroid Build Coastguard Worker     return AARCH64_EXPAND_PSEUDO_NAME;
46*9880d681SAndroid Build Coastguard Worker   }
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker private:
49*9880d681SAndroid Build Coastguard Worker   bool expandMBB(MachineBasicBlock &MBB);
50*9880d681SAndroid Build Coastguard Worker   bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
51*9880d681SAndroid Build Coastguard Worker                 MachineBasicBlock::iterator &NextMBBI);
52*9880d681SAndroid Build Coastguard Worker   bool expandMOVImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
53*9880d681SAndroid Build Coastguard Worker                     unsigned BitSize);
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker   bool expandCMP_SWAP(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
56*9880d681SAndroid Build Coastguard Worker                       unsigned LdarOp, unsigned StlrOp, unsigned CmpOp,
57*9880d681SAndroid Build Coastguard Worker                       unsigned ExtendImm, unsigned ZeroReg,
58*9880d681SAndroid Build Coastguard Worker                       MachineBasicBlock::iterator &NextMBBI);
59*9880d681SAndroid Build Coastguard Worker   bool expandCMP_SWAP_128(MachineBasicBlock &MBB,
60*9880d681SAndroid Build Coastguard Worker                           MachineBasicBlock::iterator MBBI,
61*9880d681SAndroid Build Coastguard Worker                           MachineBasicBlock::iterator &NextMBBI);
62*9880d681SAndroid Build Coastguard Worker };
63*9880d681SAndroid Build Coastguard Worker char AArch64ExpandPseudo::ID = 0;
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(AArch64ExpandPseudo, "aarch64-expand-pseudo",
67*9880d681SAndroid Build Coastguard Worker                 AARCH64_EXPAND_PSEUDO_NAME, false, false)
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker /// \brief Transfer implicit operands on the pseudo instruction to the
70*9880d681SAndroid Build Coastguard Worker /// instructions created from the expansion.
transferImpOps(MachineInstr & OldMI,MachineInstrBuilder & UseMI,MachineInstrBuilder & DefMI)71*9880d681SAndroid Build Coastguard Worker static void transferImpOps(MachineInstr &OldMI, MachineInstrBuilder &UseMI,
72*9880d681SAndroid Build Coastguard Worker                            MachineInstrBuilder &DefMI) {
73*9880d681SAndroid Build Coastguard Worker   const MCInstrDesc &Desc = OldMI.getDesc();
74*9880d681SAndroid Build Coastguard Worker   for (unsigned i = Desc.getNumOperands(), e = OldMI.getNumOperands(); i != e;
75*9880d681SAndroid Build Coastguard Worker        ++i) {
76*9880d681SAndroid Build Coastguard Worker     const MachineOperand &MO = OldMI.getOperand(i);
77*9880d681SAndroid Build Coastguard Worker     assert(MO.isReg() && MO.getReg());
78*9880d681SAndroid Build Coastguard Worker     if (MO.isUse())
79*9880d681SAndroid Build Coastguard Worker       UseMI.addOperand(MO);
80*9880d681SAndroid Build Coastguard Worker     else
81*9880d681SAndroid Build Coastguard Worker       DefMI.addOperand(MO);
82*9880d681SAndroid Build Coastguard Worker   }
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker /// \brief Helper function which extracts the specified 16-bit chunk from a
86*9880d681SAndroid Build Coastguard Worker /// 64-bit value.
getChunk(uint64_t Imm,unsigned ChunkIdx)87*9880d681SAndroid Build Coastguard Worker static uint64_t getChunk(uint64_t Imm, unsigned ChunkIdx) {
88*9880d681SAndroid Build Coastguard Worker   assert(ChunkIdx < 4 && "Out of range chunk index specified!");
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker   return (Imm >> (ChunkIdx * 16)) & 0xFFFF;
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker /// \brief Helper function which replicates a 16-bit chunk within a 64-bit
94*9880d681SAndroid Build Coastguard Worker /// value. Indices correspond to element numbers in a v4i16.
replicateChunk(uint64_t Imm,unsigned FromIdx,unsigned ToIdx)95*9880d681SAndroid Build Coastguard Worker static uint64_t replicateChunk(uint64_t Imm, unsigned FromIdx, unsigned ToIdx) {
96*9880d681SAndroid Build Coastguard Worker   assert((FromIdx < 4) && (ToIdx < 4) && "Out of range chunk index specified!");
97*9880d681SAndroid Build Coastguard Worker   const unsigned ShiftAmt = ToIdx * 16;
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   // Replicate the source chunk to the destination position.
100*9880d681SAndroid Build Coastguard Worker   const uint64_t Chunk = getChunk(Imm, FromIdx) << ShiftAmt;
101*9880d681SAndroid Build Coastguard Worker   // Clear the destination chunk.
102*9880d681SAndroid Build Coastguard Worker   Imm &= ~(0xFFFFLL << ShiftAmt);
103*9880d681SAndroid Build Coastguard Worker   // Insert the replicated chunk.
104*9880d681SAndroid Build Coastguard Worker   return Imm | Chunk;
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker 
107*9880d681SAndroid Build Coastguard Worker /// \brief Helper function which tries to materialize a 64-bit value with an
108*9880d681SAndroid Build Coastguard Worker /// ORR + MOVK instruction sequence.
tryOrrMovk(uint64_t UImm,uint64_t OrrImm,MachineInstr & MI,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const AArch64InstrInfo * TII,unsigned ChunkIdx)109*9880d681SAndroid Build Coastguard Worker static bool tryOrrMovk(uint64_t UImm, uint64_t OrrImm, MachineInstr &MI,
110*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock &MBB,
111*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock::iterator &MBBI,
112*9880d681SAndroid Build Coastguard Worker                        const AArch64InstrInfo *TII, unsigned ChunkIdx) {
113*9880d681SAndroid Build Coastguard Worker   assert(ChunkIdx < 4 && "Out of range chunk index specified!");
114*9880d681SAndroid Build Coastguard Worker   const unsigned ShiftAmt = ChunkIdx * 16;
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker   uint64_t Encoding;
117*9880d681SAndroid Build Coastguard Worker   if (AArch64_AM::processLogicalImmediate(OrrImm, 64, Encoding)) {
118*9880d681SAndroid Build Coastguard Worker     // Create the ORR-immediate instruction.
119*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB =
120*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXri))
121*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(0))
122*9880d681SAndroid Build Coastguard Worker             .addReg(AArch64::XZR)
123*9880d681SAndroid Build Coastguard Worker             .addImm(Encoding);
124*9880d681SAndroid Build Coastguard Worker 
125*9880d681SAndroid Build Coastguard Worker     // Create the MOVK instruction.
126*9880d681SAndroid Build Coastguard Worker     const unsigned Imm16 = getChunk(UImm, ChunkIdx);
127*9880d681SAndroid Build Coastguard Worker     const unsigned DstReg = MI.getOperand(0).getReg();
128*9880d681SAndroid Build Coastguard Worker     const bool DstIsDead = MI.getOperand(0).isDead();
129*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB1 =
130*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi))
131*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
132*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg)
133*9880d681SAndroid Build Coastguard Worker             .addImm(Imm16)
134*9880d681SAndroid Build Coastguard Worker             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt));
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB, MIB1);
137*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
138*9880d681SAndroid Build Coastguard Worker     return true;
139*9880d681SAndroid Build Coastguard Worker   }
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker   return false;
142*9880d681SAndroid Build Coastguard Worker }
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker /// \brief Check whether the given 16-bit chunk replicated to full 64-bit width
145*9880d681SAndroid Build Coastguard Worker /// can be materialized with an ORR instruction.
canUseOrr(uint64_t Chunk,uint64_t & Encoding)146*9880d681SAndroid Build Coastguard Worker static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) {
147*9880d681SAndroid Build Coastguard Worker   Chunk = (Chunk << 48) | (Chunk << 32) | (Chunk << 16) | Chunk;
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker   return AArch64_AM::processLogicalImmediate(Chunk, 64, Encoding);
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker /// \brief Check for identical 16-bit chunks within the constant and if so
153*9880d681SAndroid Build Coastguard Worker /// materialize them with a single ORR instruction. The remaining one or two
154*9880d681SAndroid Build Coastguard Worker /// 16-bit chunks will be materialized with MOVK instructions.
155*9880d681SAndroid Build Coastguard Worker ///
156*9880d681SAndroid Build Coastguard Worker /// This allows us to materialize constants like |A|B|A|A| or |A|B|C|A| (order
157*9880d681SAndroid Build Coastguard Worker /// of the chunks doesn't matter), assuming |A|A|A|A| can be materialized with
158*9880d681SAndroid Build Coastguard Worker /// an ORR instruction.
159*9880d681SAndroid Build Coastguard Worker ///
tryToreplicateChunks(uint64_t UImm,MachineInstr & MI,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const AArch64InstrInfo * TII)160*9880d681SAndroid Build Coastguard Worker static bool tryToreplicateChunks(uint64_t UImm, MachineInstr &MI,
161*9880d681SAndroid Build Coastguard Worker                                  MachineBasicBlock &MBB,
162*9880d681SAndroid Build Coastguard Worker                                  MachineBasicBlock::iterator &MBBI,
163*9880d681SAndroid Build Coastguard Worker                                  const AArch64InstrInfo *TII) {
164*9880d681SAndroid Build Coastguard Worker   typedef DenseMap<uint64_t, unsigned> CountMap;
165*9880d681SAndroid Build Coastguard Worker   CountMap Counts;
166*9880d681SAndroid Build Coastguard Worker 
167*9880d681SAndroid Build Coastguard Worker   // Scan the constant and count how often every chunk occurs.
168*9880d681SAndroid Build Coastguard Worker   for (unsigned Idx = 0; Idx < 4; ++Idx)
169*9880d681SAndroid Build Coastguard Worker     ++Counts[getChunk(UImm, Idx)];
170*9880d681SAndroid Build Coastguard Worker 
171*9880d681SAndroid Build Coastguard Worker   // Traverse the chunks to find one which occurs more than once.
172*9880d681SAndroid Build Coastguard Worker   for (CountMap::const_iterator Chunk = Counts.begin(), End = Counts.end();
173*9880d681SAndroid Build Coastguard Worker        Chunk != End; ++Chunk) {
174*9880d681SAndroid Build Coastguard Worker     const uint64_t ChunkVal = Chunk->first;
175*9880d681SAndroid Build Coastguard Worker     const unsigned Count = Chunk->second;
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker     uint64_t Encoding = 0;
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker     // We are looking for chunks which have two or three instances and can be
180*9880d681SAndroid Build Coastguard Worker     // materialized with an ORR instruction.
181*9880d681SAndroid Build Coastguard Worker     if ((Count != 2 && Count != 3) || !canUseOrr(ChunkVal, Encoding))
182*9880d681SAndroid Build Coastguard Worker       continue;
183*9880d681SAndroid Build Coastguard Worker 
184*9880d681SAndroid Build Coastguard Worker     const bool CountThree = Count == 3;
185*9880d681SAndroid Build Coastguard Worker     // Create the ORR-immediate instruction.
186*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB =
187*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXri))
188*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(0))
189*9880d681SAndroid Build Coastguard Worker             .addReg(AArch64::XZR)
190*9880d681SAndroid Build Coastguard Worker             .addImm(Encoding);
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker     const unsigned DstReg = MI.getOperand(0).getReg();
193*9880d681SAndroid Build Coastguard Worker     const bool DstIsDead = MI.getOperand(0).isDead();
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker     unsigned ShiftAmt = 0;
196*9880d681SAndroid Build Coastguard Worker     uint64_t Imm16 = 0;
197*9880d681SAndroid Build Coastguard Worker     // Find the first chunk not materialized with the ORR instruction.
198*9880d681SAndroid Build Coastguard Worker     for (; ShiftAmt < 64; ShiftAmt += 16) {
199*9880d681SAndroid Build Coastguard Worker       Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker       if (Imm16 != ChunkVal)
202*9880d681SAndroid Build Coastguard Worker         break;
203*9880d681SAndroid Build Coastguard Worker     }
204*9880d681SAndroid Build Coastguard Worker 
205*9880d681SAndroid Build Coastguard Worker     // Create the first MOVK instruction.
206*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB1 =
207*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi))
208*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg,
209*9880d681SAndroid Build Coastguard Worker                     RegState::Define | getDeadRegState(DstIsDead && CountThree))
210*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg)
211*9880d681SAndroid Build Coastguard Worker             .addImm(Imm16)
212*9880d681SAndroid Build Coastguard Worker             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt));
213*9880d681SAndroid Build Coastguard Worker 
214*9880d681SAndroid Build Coastguard Worker     // In case we have three instances the whole constant is now materialized
215*9880d681SAndroid Build Coastguard Worker     // and we can exit.
216*9880d681SAndroid Build Coastguard Worker     if (CountThree) {
217*9880d681SAndroid Build Coastguard Worker       transferImpOps(MI, MIB, MIB1);
218*9880d681SAndroid Build Coastguard Worker       MI.eraseFromParent();
219*9880d681SAndroid Build Coastguard Worker       return true;
220*9880d681SAndroid Build Coastguard Worker     }
221*9880d681SAndroid Build Coastguard Worker 
222*9880d681SAndroid Build Coastguard Worker     // Find the remaining chunk which needs to be materialized.
223*9880d681SAndroid Build Coastguard Worker     for (ShiftAmt += 16; ShiftAmt < 64; ShiftAmt += 16) {
224*9880d681SAndroid Build Coastguard Worker       Imm16 = (UImm >> ShiftAmt) & 0xFFFF;
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker       if (Imm16 != ChunkVal)
227*9880d681SAndroid Build Coastguard Worker         break;
228*9880d681SAndroid Build Coastguard Worker     }
229*9880d681SAndroid Build Coastguard Worker 
230*9880d681SAndroid Build Coastguard Worker     // Create the second MOVK instruction.
231*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB2 =
232*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi))
233*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
234*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg)
235*9880d681SAndroid Build Coastguard Worker             .addImm(Imm16)
236*9880d681SAndroid Build Coastguard Worker             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt));
237*9880d681SAndroid Build Coastguard Worker 
238*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB, MIB2);
239*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
240*9880d681SAndroid Build Coastguard Worker     return true;
241*9880d681SAndroid Build Coastguard Worker   }
242*9880d681SAndroid Build Coastguard Worker 
243*9880d681SAndroid Build Coastguard Worker   return false;
244*9880d681SAndroid Build Coastguard Worker }
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker /// \brief Check whether this chunk matches the pattern '1...0...'. This pattern
247*9880d681SAndroid Build Coastguard Worker /// starts a contiguous sequence of ones if we look at the bits from the LSB
248*9880d681SAndroid Build Coastguard Worker /// towards the MSB.
isStartChunk(uint64_t Chunk)249*9880d681SAndroid Build Coastguard Worker static bool isStartChunk(uint64_t Chunk) {
250*9880d681SAndroid Build Coastguard Worker   if (Chunk == 0 || Chunk == UINT64_MAX)
251*9880d681SAndroid Build Coastguard Worker     return false;
252*9880d681SAndroid Build Coastguard Worker 
253*9880d681SAndroid Build Coastguard Worker   return isMask_64(~Chunk);
254*9880d681SAndroid Build Coastguard Worker }
255*9880d681SAndroid Build Coastguard Worker 
256*9880d681SAndroid Build Coastguard Worker /// \brief Check whether this chunk matches the pattern '0...1...' This pattern
257*9880d681SAndroid Build Coastguard Worker /// ends a contiguous sequence of ones if we look at the bits from the LSB
258*9880d681SAndroid Build Coastguard Worker /// towards the MSB.
isEndChunk(uint64_t Chunk)259*9880d681SAndroid Build Coastguard Worker static bool isEndChunk(uint64_t Chunk) {
260*9880d681SAndroid Build Coastguard Worker   if (Chunk == 0 || Chunk == UINT64_MAX)
261*9880d681SAndroid Build Coastguard Worker     return false;
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker   return isMask_64(Chunk);
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker 
266*9880d681SAndroid Build Coastguard Worker /// \brief Clear or set all bits in the chunk at the given index.
updateImm(uint64_t Imm,unsigned Idx,bool Clear)267*9880d681SAndroid Build Coastguard Worker static uint64_t updateImm(uint64_t Imm, unsigned Idx, bool Clear) {
268*9880d681SAndroid Build Coastguard Worker   const uint64_t Mask = 0xFFFF;
269*9880d681SAndroid Build Coastguard Worker 
270*9880d681SAndroid Build Coastguard Worker   if (Clear)
271*9880d681SAndroid Build Coastguard Worker     // Clear chunk in the immediate.
272*9880d681SAndroid Build Coastguard Worker     Imm &= ~(Mask << (Idx * 16));
273*9880d681SAndroid Build Coastguard Worker   else
274*9880d681SAndroid Build Coastguard Worker     // Set all bits in the immediate for the particular chunk.
275*9880d681SAndroid Build Coastguard Worker     Imm |= Mask << (Idx * 16);
276*9880d681SAndroid Build Coastguard Worker 
277*9880d681SAndroid Build Coastguard Worker   return Imm;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker 
280*9880d681SAndroid Build Coastguard Worker /// \brief Check whether the constant contains a sequence of contiguous ones,
281*9880d681SAndroid Build Coastguard Worker /// which might be interrupted by one or two chunks. If so, materialize the
282*9880d681SAndroid Build Coastguard Worker /// sequence of contiguous ones with an ORR instruction.
283*9880d681SAndroid Build Coastguard Worker /// Materialize the chunks which are either interrupting the sequence or outside
284*9880d681SAndroid Build Coastguard Worker /// of the sequence with a MOVK instruction.
285*9880d681SAndroid Build Coastguard Worker ///
286*9880d681SAndroid Build Coastguard Worker /// Assuming S is a chunk which starts the sequence (1...0...), E is a chunk
287*9880d681SAndroid Build Coastguard Worker /// which ends the sequence (0...1...). Then we are looking for constants which
288*9880d681SAndroid Build Coastguard Worker /// contain at least one S and E chunk.
289*9880d681SAndroid Build Coastguard Worker /// E.g. |E|A|B|S|, |A|E|B|S| or |A|B|E|S|.
290*9880d681SAndroid Build Coastguard Worker ///
291*9880d681SAndroid Build Coastguard Worker /// We are also looking for constants like |S|A|B|E| where the contiguous
292*9880d681SAndroid Build Coastguard Worker /// sequence of ones wraps around the MSB into the LSB.
293*9880d681SAndroid Build Coastguard Worker ///
trySequenceOfOnes(uint64_t UImm,MachineInstr & MI,MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const AArch64InstrInfo * TII)294*9880d681SAndroid Build Coastguard Worker static bool trySequenceOfOnes(uint64_t UImm, MachineInstr &MI,
295*9880d681SAndroid Build Coastguard Worker                               MachineBasicBlock &MBB,
296*9880d681SAndroid Build Coastguard Worker                               MachineBasicBlock::iterator &MBBI,
297*9880d681SAndroid Build Coastguard Worker                               const AArch64InstrInfo *TII) {
298*9880d681SAndroid Build Coastguard Worker   const int NotSet = -1;
299*9880d681SAndroid Build Coastguard Worker   const uint64_t Mask = 0xFFFF;
300*9880d681SAndroid Build Coastguard Worker 
301*9880d681SAndroid Build Coastguard Worker   int StartIdx = NotSet;
302*9880d681SAndroid Build Coastguard Worker   int EndIdx = NotSet;
303*9880d681SAndroid Build Coastguard Worker   // Try to find the chunks which start/end a contiguous sequence of ones.
304*9880d681SAndroid Build Coastguard Worker   for (int Idx = 0; Idx < 4; ++Idx) {
305*9880d681SAndroid Build Coastguard Worker     int64_t Chunk = getChunk(UImm, Idx);
306*9880d681SAndroid Build Coastguard Worker     // Sign extend the 16-bit chunk to 64-bit.
307*9880d681SAndroid Build Coastguard Worker     Chunk = (Chunk << 48) >> 48;
308*9880d681SAndroid Build Coastguard Worker 
309*9880d681SAndroid Build Coastguard Worker     if (isStartChunk(Chunk))
310*9880d681SAndroid Build Coastguard Worker       StartIdx = Idx;
311*9880d681SAndroid Build Coastguard Worker     else if (isEndChunk(Chunk))
312*9880d681SAndroid Build Coastguard Worker       EndIdx = Idx;
313*9880d681SAndroid Build Coastguard Worker   }
314*9880d681SAndroid Build Coastguard Worker 
315*9880d681SAndroid Build Coastguard Worker   // Early exit in case we can't find a start/end chunk.
316*9880d681SAndroid Build Coastguard Worker   if (StartIdx == NotSet || EndIdx == NotSet)
317*9880d681SAndroid Build Coastguard Worker     return false;
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker   // Outside of the contiguous sequence of ones everything needs to be zero.
320*9880d681SAndroid Build Coastguard Worker   uint64_t Outside = 0;
321*9880d681SAndroid Build Coastguard Worker   // Chunks between the start and end chunk need to have all their bits set.
322*9880d681SAndroid Build Coastguard Worker   uint64_t Inside = Mask;
323*9880d681SAndroid Build Coastguard Worker 
324*9880d681SAndroid Build Coastguard Worker   // If our contiguous sequence of ones wraps around from the MSB into the LSB,
325*9880d681SAndroid Build Coastguard Worker   // just swap indices and pretend we are materializing a contiguous sequence
326*9880d681SAndroid Build Coastguard Worker   // of zeros surrounded by a contiguous sequence of ones.
327*9880d681SAndroid Build Coastguard Worker   if (StartIdx > EndIdx) {
328*9880d681SAndroid Build Coastguard Worker     std::swap(StartIdx, EndIdx);
329*9880d681SAndroid Build Coastguard Worker     std::swap(Outside, Inside);
330*9880d681SAndroid Build Coastguard Worker   }
331*9880d681SAndroid Build Coastguard Worker 
332*9880d681SAndroid Build Coastguard Worker   uint64_t OrrImm = UImm;
333*9880d681SAndroid Build Coastguard Worker   int FirstMovkIdx = NotSet;
334*9880d681SAndroid Build Coastguard Worker   int SecondMovkIdx = NotSet;
335*9880d681SAndroid Build Coastguard Worker 
336*9880d681SAndroid Build Coastguard Worker   // Find out which chunks we need to patch up to obtain a contiguous sequence
337*9880d681SAndroid Build Coastguard Worker   // of ones.
338*9880d681SAndroid Build Coastguard Worker   for (int Idx = 0; Idx < 4; ++Idx) {
339*9880d681SAndroid Build Coastguard Worker     const uint64_t Chunk = getChunk(UImm, Idx);
340*9880d681SAndroid Build Coastguard Worker 
341*9880d681SAndroid Build Coastguard Worker     // Check whether we are looking at a chunk which is not part of the
342*9880d681SAndroid Build Coastguard Worker     // contiguous sequence of ones.
343*9880d681SAndroid Build Coastguard Worker     if ((Idx < StartIdx || EndIdx < Idx) && Chunk != Outside) {
344*9880d681SAndroid Build Coastguard Worker       OrrImm = updateImm(OrrImm, Idx, Outside == 0);
345*9880d681SAndroid Build Coastguard Worker 
346*9880d681SAndroid Build Coastguard Worker       // Remember the index we need to patch.
347*9880d681SAndroid Build Coastguard Worker       if (FirstMovkIdx == NotSet)
348*9880d681SAndroid Build Coastguard Worker         FirstMovkIdx = Idx;
349*9880d681SAndroid Build Coastguard Worker       else
350*9880d681SAndroid Build Coastguard Worker         SecondMovkIdx = Idx;
351*9880d681SAndroid Build Coastguard Worker 
352*9880d681SAndroid Build Coastguard Worker       // Check whether we are looking a chunk which is part of the contiguous
353*9880d681SAndroid Build Coastguard Worker       // sequence of ones.
354*9880d681SAndroid Build Coastguard Worker     } else if (Idx > StartIdx && Idx < EndIdx && Chunk != Inside) {
355*9880d681SAndroid Build Coastguard Worker       OrrImm = updateImm(OrrImm, Idx, Inside != Mask);
356*9880d681SAndroid Build Coastguard Worker 
357*9880d681SAndroid Build Coastguard Worker       // Remember the index we need to patch.
358*9880d681SAndroid Build Coastguard Worker       if (FirstMovkIdx == NotSet)
359*9880d681SAndroid Build Coastguard Worker         FirstMovkIdx = Idx;
360*9880d681SAndroid Build Coastguard Worker       else
361*9880d681SAndroid Build Coastguard Worker         SecondMovkIdx = Idx;
362*9880d681SAndroid Build Coastguard Worker     }
363*9880d681SAndroid Build Coastguard Worker   }
364*9880d681SAndroid Build Coastguard Worker   assert(FirstMovkIdx != NotSet && "Constant materializable with single ORR!");
365*9880d681SAndroid Build Coastguard Worker 
366*9880d681SAndroid Build Coastguard Worker   // Create the ORR-immediate instruction.
367*9880d681SAndroid Build Coastguard Worker   uint64_t Encoding = 0;
368*9880d681SAndroid Build Coastguard Worker   AArch64_AM::processLogicalImmediate(OrrImm, 64, Encoding);
369*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder MIB =
370*9880d681SAndroid Build Coastguard Worker       BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ORRXri))
371*9880d681SAndroid Build Coastguard Worker           .addOperand(MI.getOperand(0))
372*9880d681SAndroid Build Coastguard Worker           .addReg(AArch64::XZR)
373*9880d681SAndroid Build Coastguard Worker           .addImm(Encoding);
374*9880d681SAndroid Build Coastguard Worker 
375*9880d681SAndroid Build Coastguard Worker   const unsigned DstReg = MI.getOperand(0).getReg();
376*9880d681SAndroid Build Coastguard Worker   const bool DstIsDead = MI.getOperand(0).isDead();
377*9880d681SAndroid Build Coastguard Worker 
378*9880d681SAndroid Build Coastguard Worker   const bool SingleMovk = SecondMovkIdx == NotSet;
379*9880d681SAndroid Build Coastguard Worker   // Create the first MOVK instruction.
380*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder MIB1 =
381*9880d681SAndroid Build Coastguard Worker       BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi))
382*9880d681SAndroid Build Coastguard Worker           .addReg(DstReg,
383*9880d681SAndroid Build Coastguard Worker                   RegState::Define | getDeadRegState(DstIsDead && SingleMovk))
384*9880d681SAndroid Build Coastguard Worker           .addReg(DstReg)
385*9880d681SAndroid Build Coastguard Worker           .addImm(getChunk(UImm, FirstMovkIdx))
386*9880d681SAndroid Build Coastguard Worker           .addImm(
387*9880d681SAndroid Build Coastguard Worker               AArch64_AM::getShifterImm(AArch64_AM::LSL, FirstMovkIdx * 16));
388*9880d681SAndroid Build Coastguard Worker 
389*9880d681SAndroid Build Coastguard Worker   // Early exit in case we only need to emit a single MOVK instruction.
390*9880d681SAndroid Build Coastguard Worker   if (SingleMovk) {
391*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB, MIB1);
392*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
393*9880d681SAndroid Build Coastguard Worker     return true;
394*9880d681SAndroid Build Coastguard Worker   }
395*9880d681SAndroid Build Coastguard Worker 
396*9880d681SAndroid Build Coastguard Worker   // Create the second MOVK instruction.
397*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder MIB2 =
398*9880d681SAndroid Build Coastguard Worker       BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi))
399*9880d681SAndroid Build Coastguard Worker           .addReg(DstReg, RegState::Define | getDeadRegState(DstIsDead))
400*9880d681SAndroid Build Coastguard Worker           .addReg(DstReg)
401*9880d681SAndroid Build Coastguard Worker           .addImm(getChunk(UImm, SecondMovkIdx))
402*9880d681SAndroid Build Coastguard Worker           .addImm(
403*9880d681SAndroid Build Coastguard Worker               AArch64_AM::getShifterImm(AArch64_AM::LSL, SecondMovkIdx * 16));
404*9880d681SAndroid Build Coastguard Worker 
405*9880d681SAndroid Build Coastguard Worker   transferImpOps(MI, MIB, MIB2);
406*9880d681SAndroid Build Coastguard Worker   MI.eraseFromParent();
407*9880d681SAndroid Build Coastguard Worker   return true;
408*9880d681SAndroid Build Coastguard Worker }
409*9880d681SAndroid Build Coastguard Worker 
410*9880d681SAndroid Build Coastguard Worker /// \brief Expand a MOVi32imm or MOVi64imm pseudo instruction to one or more
411*9880d681SAndroid Build Coastguard Worker /// real move-immediate instructions to synthesize the immediate.
expandMOVImm(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,unsigned BitSize)412*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::expandMOVImm(MachineBasicBlock &MBB,
413*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock::iterator MBBI,
414*9880d681SAndroid Build Coastguard Worker                                        unsigned BitSize) {
415*9880d681SAndroid Build Coastguard Worker   MachineInstr &MI = *MBBI;
416*9880d681SAndroid Build Coastguard Worker   unsigned DstReg = MI.getOperand(0).getReg();
417*9880d681SAndroid Build Coastguard Worker   uint64_t Imm = MI.getOperand(1).getImm();
418*9880d681SAndroid Build Coastguard Worker   const unsigned Mask = 0xFFFF;
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker   if (DstReg == AArch64::XZR || DstReg == AArch64::WZR) {
421*9880d681SAndroid Build Coastguard Worker     // Useless def, and we don't want to risk creating an invalid ORR (which
422*9880d681SAndroid Build Coastguard Worker     // would really write to sp).
423*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
424*9880d681SAndroid Build Coastguard Worker     return true;
425*9880d681SAndroid Build Coastguard Worker   }
426*9880d681SAndroid Build Coastguard Worker 
427*9880d681SAndroid Build Coastguard Worker   // Try a MOVI instruction (aka ORR-immediate with the zero register).
428*9880d681SAndroid Build Coastguard Worker   uint64_t UImm = Imm << (64 - BitSize) >> (64 - BitSize);
429*9880d681SAndroid Build Coastguard Worker   uint64_t Encoding;
430*9880d681SAndroid Build Coastguard Worker   if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
431*9880d681SAndroid Build Coastguard Worker     unsigned Opc = (BitSize == 32 ? AArch64::ORRWri : AArch64::ORRXri);
432*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB =
433*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
434*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(0))
435*9880d681SAndroid Build Coastguard Worker             .addReg(BitSize == 32 ? AArch64::WZR : AArch64::XZR)
436*9880d681SAndroid Build Coastguard Worker             .addImm(Encoding);
437*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB, MIB);
438*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
439*9880d681SAndroid Build Coastguard Worker     return true;
440*9880d681SAndroid Build Coastguard Worker   }
441*9880d681SAndroid Build Coastguard Worker 
442*9880d681SAndroid Build Coastguard Worker   // Scan the immediate and count the number of 16-bit chunks which are either
443*9880d681SAndroid Build Coastguard Worker   // all ones or all zeros.
444*9880d681SAndroid Build Coastguard Worker   unsigned OneChunks = 0;
445*9880d681SAndroid Build Coastguard Worker   unsigned ZeroChunks = 0;
446*9880d681SAndroid Build Coastguard Worker   for (unsigned Shift = 0; Shift < BitSize; Shift += 16) {
447*9880d681SAndroid Build Coastguard Worker     const unsigned Chunk = (Imm >> Shift) & Mask;
448*9880d681SAndroid Build Coastguard Worker     if (Chunk == Mask)
449*9880d681SAndroid Build Coastguard Worker       OneChunks++;
450*9880d681SAndroid Build Coastguard Worker     else if (Chunk == 0)
451*9880d681SAndroid Build Coastguard Worker       ZeroChunks++;
452*9880d681SAndroid Build Coastguard Worker   }
453*9880d681SAndroid Build Coastguard Worker 
454*9880d681SAndroid Build Coastguard Worker   // Since we can't materialize the constant with a single ORR instruction,
455*9880d681SAndroid Build Coastguard Worker   // let's see whether we can materialize 3/4 of the constant with an ORR
456*9880d681SAndroid Build Coastguard Worker   // instruction and use an additional MOVK instruction to materialize the
457*9880d681SAndroid Build Coastguard Worker   // remaining 1/4.
458*9880d681SAndroid Build Coastguard Worker   //
459*9880d681SAndroid Build Coastguard Worker   // We are looking for constants with a pattern like: |A|X|B|X| or |X|A|X|B|.
460*9880d681SAndroid Build Coastguard Worker   //
461*9880d681SAndroid Build Coastguard Worker   // E.g. assuming |A|X|A|X| is a pattern which can be materialized with ORR,
462*9880d681SAndroid Build Coastguard Worker   // we would create the following instruction sequence:
463*9880d681SAndroid Build Coastguard Worker   //
464*9880d681SAndroid Build Coastguard Worker   // ORR x0, xzr, |A|X|A|X|
465*9880d681SAndroid Build Coastguard Worker   // MOVK x0, |B|, LSL #16
466*9880d681SAndroid Build Coastguard Worker   //
467*9880d681SAndroid Build Coastguard Worker   // Only look at 64-bit constants which can't be materialized with a single
468*9880d681SAndroid Build Coastguard Worker   // instruction e.g. which have less than either three all zero or all one
469*9880d681SAndroid Build Coastguard Worker   // chunks.
470*9880d681SAndroid Build Coastguard Worker   //
471*9880d681SAndroid Build Coastguard Worker   // Ignore 32-bit constants here, they always can be materialized with a
472*9880d681SAndroid Build Coastguard Worker   // MOVZ/MOVN + MOVK pair. Since the 32-bit constant can't be materialized
473*9880d681SAndroid Build Coastguard Worker   // with a single ORR, the best sequence we can achieve is a ORR + MOVK pair.
474*9880d681SAndroid Build Coastguard Worker   // Thus we fall back to the default code below which in the best case creates
475*9880d681SAndroid Build Coastguard Worker   // a single MOVZ/MOVN instruction (in case one chunk is all zero or all one).
476*9880d681SAndroid Build Coastguard Worker   //
477*9880d681SAndroid Build Coastguard Worker   if (BitSize == 64 && OneChunks < 3 && ZeroChunks < 3) {
478*9880d681SAndroid Build Coastguard Worker     // If we interpret the 64-bit constant as a v4i16, are elements 0 and 2
479*9880d681SAndroid Build Coastguard Worker     // identical?
480*9880d681SAndroid Build Coastguard Worker     if (getChunk(UImm, 0) == getChunk(UImm, 2)) {
481*9880d681SAndroid Build Coastguard Worker       // See if we can come up with a constant which can be materialized with
482*9880d681SAndroid Build Coastguard Worker       // ORR-immediate by replicating element 3 into element 1.
483*9880d681SAndroid Build Coastguard Worker       uint64_t OrrImm = replicateChunk(UImm, 3, 1);
484*9880d681SAndroid Build Coastguard Worker       if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 1))
485*9880d681SAndroid Build Coastguard Worker         return true;
486*9880d681SAndroid Build Coastguard Worker 
487*9880d681SAndroid Build Coastguard Worker       // See if we can come up with a constant which can be materialized with
488*9880d681SAndroid Build Coastguard Worker       // ORR-immediate by replicating element 1 into element 3.
489*9880d681SAndroid Build Coastguard Worker       OrrImm = replicateChunk(UImm, 1, 3);
490*9880d681SAndroid Build Coastguard Worker       if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 3))
491*9880d681SAndroid Build Coastguard Worker         return true;
492*9880d681SAndroid Build Coastguard Worker 
493*9880d681SAndroid Build Coastguard Worker       // If we interpret the 64-bit constant as a v4i16, are elements 1 and 3
494*9880d681SAndroid Build Coastguard Worker       // identical?
495*9880d681SAndroid Build Coastguard Worker     } else if (getChunk(UImm, 1) == getChunk(UImm, 3)) {
496*9880d681SAndroid Build Coastguard Worker       // See if we can come up with a constant which can be materialized with
497*9880d681SAndroid Build Coastguard Worker       // ORR-immediate by replicating element 2 into element 0.
498*9880d681SAndroid Build Coastguard Worker       uint64_t OrrImm = replicateChunk(UImm, 2, 0);
499*9880d681SAndroid Build Coastguard Worker       if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 0))
500*9880d681SAndroid Build Coastguard Worker         return true;
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker       // See if we can come up with a constant which can be materialized with
503*9880d681SAndroid Build Coastguard Worker       // ORR-immediate by replicating element 1 into element 3.
504*9880d681SAndroid Build Coastguard Worker       OrrImm = replicateChunk(UImm, 0, 2);
505*9880d681SAndroid Build Coastguard Worker       if (tryOrrMovk(UImm, OrrImm, MI, MBB, MBBI, TII, 2))
506*9880d681SAndroid Build Coastguard Worker         return true;
507*9880d681SAndroid Build Coastguard Worker     }
508*9880d681SAndroid Build Coastguard Worker   }
509*9880d681SAndroid Build Coastguard Worker 
510*9880d681SAndroid Build Coastguard Worker   // Check for identical 16-bit chunks within the constant and if so materialize
511*9880d681SAndroid Build Coastguard Worker   // them with a single ORR instruction. The remaining one or two 16-bit chunks
512*9880d681SAndroid Build Coastguard Worker   // will be materialized with MOVK instructions.
513*9880d681SAndroid Build Coastguard Worker   if (BitSize == 64 && tryToreplicateChunks(UImm, MI, MBB, MBBI, TII))
514*9880d681SAndroid Build Coastguard Worker     return true;
515*9880d681SAndroid Build Coastguard Worker 
516*9880d681SAndroid Build Coastguard Worker   // Check whether the constant contains a sequence of contiguous ones, which
517*9880d681SAndroid Build Coastguard Worker   // might be interrupted by one or two chunks. If so, materialize the sequence
518*9880d681SAndroid Build Coastguard Worker   // of contiguous ones with an ORR instruction. Materialize the chunks which
519*9880d681SAndroid Build Coastguard Worker   // are either interrupting the sequence or outside of the sequence with a
520*9880d681SAndroid Build Coastguard Worker   // MOVK instruction.
521*9880d681SAndroid Build Coastguard Worker   if (BitSize == 64 && trySequenceOfOnes(UImm, MI, MBB, MBBI, TII))
522*9880d681SAndroid Build Coastguard Worker     return true;
523*9880d681SAndroid Build Coastguard Worker 
524*9880d681SAndroid Build Coastguard Worker   // Use a MOVZ or MOVN instruction to set the high bits, followed by one or
525*9880d681SAndroid Build Coastguard Worker   // more MOVK instructions to insert additional 16-bit portions into the
526*9880d681SAndroid Build Coastguard Worker   // lower bits.
527*9880d681SAndroid Build Coastguard Worker   bool isNeg = false;
528*9880d681SAndroid Build Coastguard Worker 
529*9880d681SAndroid Build Coastguard Worker   // Use MOVN to materialize the high bits if we have more all one chunks
530*9880d681SAndroid Build Coastguard Worker   // than all zero chunks.
531*9880d681SAndroid Build Coastguard Worker   if (OneChunks > ZeroChunks) {
532*9880d681SAndroid Build Coastguard Worker     isNeg = true;
533*9880d681SAndroid Build Coastguard Worker     Imm = ~Imm;
534*9880d681SAndroid Build Coastguard Worker   }
535*9880d681SAndroid Build Coastguard Worker 
536*9880d681SAndroid Build Coastguard Worker   unsigned FirstOpc;
537*9880d681SAndroid Build Coastguard Worker   if (BitSize == 32) {
538*9880d681SAndroid Build Coastguard Worker     Imm &= (1LL << 32) - 1;
539*9880d681SAndroid Build Coastguard Worker     FirstOpc = (isNeg ? AArch64::MOVNWi : AArch64::MOVZWi);
540*9880d681SAndroid Build Coastguard Worker   } else {
541*9880d681SAndroid Build Coastguard Worker     FirstOpc = (isNeg ? AArch64::MOVNXi : AArch64::MOVZXi);
542*9880d681SAndroid Build Coastguard Worker   }
543*9880d681SAndroid Build Coastguard Worker   unsigned Shift = 0;     // LSL amount for high bits with MOVZ/MOVN
544*9880d681SAndroid Build Coastguard Worker   unsigned LastShift = 0; // LSL amount for last MOVK
545*9880d681SAndroid Build Coastguard Worker   if (Imm != 0) {
546*9880d681SAndroid Build Coastguard Worker     unsigned LZ = countLeadingZeros(Imm);
547*9880d681SAndroid Build Coastguard Worker     unsigned TZ = countTrailingZeros(Imm);
548*9880d681SAndroid Build Coastguard Worker     Shift = ((63 - LZ) / 16) * 16;
549*9880d681SAndroid Build Coastguard Worker     LastShift = (TZ / 16) * 16;
550*9880d681SAndroid Build Coastguard Worker   }
551*9880d681SAndroid Build Coastguard Worker   unsigned Imm16 = (Imm >> Shift) & Mask;
552*9880d681SAndroid Build Coastguard Worker   bool DstIsDead = MI.getOperand(0).isDead();
553*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder MIB1 =
554*9880d681SAndroid Build Coastguard Worker       BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(FirstOpc))
555*9880d681SAndroid Build Coastguard Worker           .addReg(DstReg, RegState::Define |
556*9880d681SAndroid Build Coastguard Worker                               getDeadRegState(DstIsDead && Shift == LastShift))
557*9880d681SAndroid Build Coastguard Worker           .addImm(Imm16)
558*9880d681SAndroid Build Coastguard Worker           .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift));
559*9880d681SAndroid Build Coastguard Worker 
560*9880d681SAndroid Build Coastguard Worker   // If a MOVN was used for the high bits of a negative value, flip the rest
561*9880d681SAndroid Build Coastguard Worker   // of the bits back for use with MOVK.
562*9880d681SAndroid Build Coastguard Worker   if (isNeg)
563*9880d681SAndroid Build Coastguard Worker     Imm = ~Imm;
564*9880d681SAndroid Build Coastguard Worker 
565*9880d681SAndroid Build Coastguard Worker   if (Shift == LastShift) {
566*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB1, MIB1);
567*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
568*9880d681SAndroid Build Coastguard Worker     return true;
569*9880d681SAndroid Build Coastguard Worker   }
570*9880d681SAndroid Build Coastguard Worker 
571*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder MIB2;
572*9880d681SAndroid Build Coastguard Worker   unsigned Opc = (BitSize == 32 ? AArch64::MOVKWi : AArch64::MOVKXi);
573*9880d681SAndroid Build Coastguard Worker   while (Shift != LastShift) {
574*9880d681SAndroid Build Coastguard Worker     Shift -= 16;
575*9880d681SAndroid Build Coastguard Worker     Imm16 = (Imm >> Shift) & Mask;
576*9880d681SAndroid Build Coastguard Worker     if (Imm16 == (isNeg ? Mask : 0))
577*9880d681SAndroid Build Coastguard Worker       continue; // This 16-bit portion is already set correctly.
578*9880d681SAndroid Build Coastguard Worker     MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc))
579*9880d681SAndroid Build Coastguard Worker                .addReg(DstReg,
580*9880d681SAndroid Build Coastguard Worker                        RegState::Define |
581*9880d681SAndroid Build Coastguard Worker                            getDeadRegState(DstIsDead && Shift == LastShift))
582*9880d681SAndroid Build Coastguard Worker                .addReg(DstReg)
583*9880d681SAndroid Build Coastguard Worker                .addImm(Imm16)
584*9880d681SAndroid Build Coastguard Worker                .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift));
585*9880d681SAndroid Build Coastguard Worker   }
586*9880d681SAndroid Build Coastguard Worker 
587*9880d681SAndroid Build Coastguard Worker   transferImpOps(MI, MIB1, MIB2);
588*9880d681SAndroid Build Coastguard Worker   MI.eraseFromParent();
589*9880d681SAndroid Build Coastguard Worker   return true;
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker 
addPostLoopLiveIns(MachineBasicBlock * MBB,LivePhysRegs & LiveRegs)592*9880d681SAndroid Build Coastguard Worker static void addPostLoopLiveIns(MachineBasicBlock *MBB, LivePhysRegs &LiveRegs) {
593*9880d681SAndroid Build Coastguard Worker   for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
594*9880d681SAndroid Build Coastguard Worker     MBB->addLiveIn(*I);
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker 
expandCMP_SWAP(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,unsigned LdarOp,unsigned StlrOp,unsigned CmpOp,unsigned ExtendImm,unsigned ZeroReg,MachineBasicBlock::iterator & NextMBBI)597*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::expandCMP_SWAP(
598*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned LdarOp,
599*9880d681SAndroid Build Coastguard Worker     unsigned StlrOp, unsigned CmpOp, unsigned ExtendImm, unsigned ZeroReg,
600*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock::iterator &NextMBBI) {
601*9880d681SAndroid Build Coastguard Worker   MachineInstr &MI = *MBBI;
602*9880d681SAndroid Build Coastguard Worker   DebugLoc DL = MI.getDebugLoc();
603*9880d681SAndroid Build Coastguard Worker   MachineOperand &Dest = MI.getOperand(0);
604*9880d681SAndroid Build Coastguard Worker   unsigned StatusReg = MI.getOperand(1).getReg();
605*9880d681SAndroid Build Coastguard Worker   MachineOperand &Addr = MI.getOperand(2);
606*9880d681SAndroid Build Coastguard Worker   MachineOperand &Desired = MI.getOperand(3);
607*9880d681SAndroid Build Coastguard Worker   MachineOperand &New = MI.getOperand(4);
608*9880d681SAndroid Build Coastguard Worker 
609*9880d681SAndroid Build Coastguard Worker   LivePhysRegs LiveRegs(&TII->getRegisterInfo());
610*9880d681SAndroid Build Coastguard Worker   LiveRegs.addLiveOuts(MBB);
611*9880d681SAndroid Build Coastguard Worker   for (auto I = std::prev(MBB.end()); I != MBBI; --I)
612*9880d681SAndroid Build Coastguard Worker     LiveRegs.stepBackward(*I);
613*9880d681SAndroid Build Coastguard Worker 
614*9880d681SAndroid Build Coastguard Worker   MachineFunction *MF = MBB.getParent();
615*9880d681SAndroid Build Coastguard Worker   auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
616*9880d681SAndroid Build Coastguard Worker   auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
617*9880d681SAndroid Build Coastguard Worker   auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
618*9880d681SAndroid Build Coastguard Worker 
619*9880d681SAndroid Build Coastguard Worker   MF->insert(++MBB.getIterator(), LoadCmpBB);
620*9880d681SAndroid Build Coastguard Worker   MF->insert(++LoadCmpBB->getIterator(), StoreBB);
621*9880d681SAndroid Build Coastguard Worker   MF->insert(++StoreBB->getIterator(), DoneBB);
622*9880d681SAndroid Build Coastguard Worker 
623*9880d681SAndroid Build Coastguard Worker   // .Lloadcmp:
624*9880d681SAndroid Build Coastguard Worker   //     ldaxr xDest, [xAddr]
625*9880d681SAndroid Build Coastguard Worker   //     cmp xDest, xDesired
626*9880d681SAndroid Build Coastguard Worker   //     b.ne .Ldone
627*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(Addr.getReg());
628*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(Dest.getReg());
629*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(Desired.getReg());
630*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(LoadCmpBB, LiveRegs);
631*9880d681SAndroid Build Coastguard Worker 
632*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(LdarOp), Dest.getReg())
633*9880d681SAndroid Build Coastguard Worker       .addReg(Addr.getReg());
634*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(CmpOp), ZeroReg)
635*9880d681SAndroid Build Coastguard Worker       .addReg(Dest.getReg(), getKillRegState(Dest.isDead()))
636*9880d681SAndroid Build Coastguard Worker       .addOperand(Desired)
637*9880d681SAndroid Build Coastguard Worker       .addImm(ExtendImm);
638*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(AArch64::Bcc))
639*9880d681SAndroid Build Coastguard Worker       .addImm(AArch64CC::NE)
640*9880d681SAndroid Build Coastguard Worker       .addMBB(DoneBB)
641*9880d681SAndroid Build Coastguard Worker       .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
642*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addSuccessor(DoneBB);
643*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addSuccessor(StoreBB);
644*9880d681SAndroid Build Coastguard Worker 
645*9880d681SAndroid Build Coastguard Worker   // .Lstore:
646*9880d681SAndroid Build Coastguard Worker   //     stlxr wStatus, xNew, [xAddr]
647*9880d681SAndroid Build Coastguard Worker   //     cbnz wStatus, .Lloadcmp
648*9880d681SAndroid Build Coastguard Worker   StoreBB->addLiveIn(Addr.getReg());
649*9880d681SAndroid Build Coastguard Worker   StoreBB->addLiveIn(New.getReg());
650*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(StoreBB, LiveRegs);
651*9880d681SAndroid Build Coastguard Worker 
652*9880d681SAndroid Build Coastguard Worker   BuildMI(StoreBB, DL, TII->get(StlrOp), StatusReg)
653*9880d681SAndroid Build Coastguard Worker       .addOperand(New)
654*9880d681SAndroid Build Coastguard Worker       .addOperand(Addr);
655*9880d681SAndroid Build Coastguard Worker   BuildMI(StoreBB, DL, TII->get(AArch64::CBNZW))
656*9880d681SAndroid Build Coastguard Worker       .addReg(StatusReg, RegState::Kill)
657*9880d681SAndroid Build Coastguard Worker       .addMBB(LoadCmpBB);
658*9880d681SAndroid Build Coastguard Worker   StoreBB->addSuccessor(LoadCmpBB);
659*9880d681SAndroid Build Coastguard Worker   StoreBB->addSuccessor(DoneBB);
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker   DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
662*9880d681SAndroid Build Coastguard Worker   DoneBB->transferSuccessors(&MBB);
663*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(DoneBB, LiveRegs);
664*9880d681SAndroid Build Coastguard Worker 
665*9880d681SAndroid Build Coastguard Worker   MBB.addSuccessor(LoadCmpBB);
666*9880d681SAndroid Build Coastguard Worker 
667*9880d681SAndroid Build Coastguard Worker   NextMBBI = MBB.end();
668*9880d681SAndroid Build Coastguard Worker   MI.eraseFromParent();
669*9880d681SAndroid Build Coastguard Worker   return true;
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker 
expandCMP_SWAP_128(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)672*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::expandCMP_SWAP_128(
673*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
674*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock::iterator &NextMBBI) {
675*9880d681SAndroid Build Coastguard Worker 
676*9880d681SAndroid Build Coastguard Worker   MachineInstr &MI = *MBBI;
677*9880d681SAndroid Build Coastguard Worker   DebugLoc DL = MI.getDebugLoc();
678*9880d681SAndroid Build Coastguard Worker   MachineOperand &DestLo = MI.getOperand(0);
679*9880d681SAndroid Build Coastguard Worker   MachineOperand &DestHi = MI.getOperand(1);
680*9880d681SAndroid Build Coastguard Worker   unsigned StatusReg = MI.getOperand(2).getReg();
681*9880d681SAndroid Build Coastguard Worker   MachineOperand &Addr = MI.getOperand(3);
682*9880d681SAndroid Build Coastguard Worker   MachineOperand &DesiredLo = MI.getOperand(4);
683*9880d681SAndroid Build Coastguard Worker   MachineOperand &DesiredHi = MI.getOperand(5);
684*9880d681SAndroid Build Coastguard Worker   MachineOperand &NewLo = MI.getOperand(6);
685*9880d681SAndroid Build Coastguard Worker   MachineOperand &NewHi = MI.getOperand(7);
686*9880d681SAndroid Build Coastguard Worker 
687*9880d681SAndroid Build Coastguard Worker   LivePhysRegs LiveRegs(&TII->getRegisterInfo());
688*9880d681SAndroid Build Coastguard Worker   LiveRegs.addLiveOuts(MBB);
689*9880d681SAndroid Build Coastguard Worker   for (auto I = std::prev(MBB.end()); I != MBBI; --I)
690*9880d681SAndroid Build Coastguard Worker     LiveRegs.stepBackward(*I);
691*9880d681SAndroid Build Coastguard Worker 
692*9880d681SAndroid Build Coastguard Worker   MachineFunction *MF = MBB.getParent();
693*9880d681SAndroid Build Coastguard Worker   auto LoadCmpBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
694*9880d681SAndroid Build Coastguard Worker   auto StoreBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
695*9880d681SAndroid Build Coastguard Worker   auto DoneBB = MF->CreateMachineBasicBlock(MBB.getBasicBlock());
696*9880d681SAndroid Build Coastguard Worker 
697*9880d681SAndroid Build Coastguard Worker   MF->insert(++MBB.getIterator(), LoadCmpBB);
698*9880d681SAndroid Build Coastguard Worker   MF->insert(++LoadCmpBB->getIterator(), StoreBB);
699*9880d681SAndroid Build Coastguard Worker   MF->insert(++StoreBB->getIterator(), DoneBB);
700*9880d681SAndroid Build Coastguard Worker 
701*9880d681SAndroid Build Coastguard Worker   // .Lloadcmp:
702*9880d681SAndroid Build Coastguard Worker   //     ldaxp xDestLo, xDestHi, [xAddr]
703*9880d681SAndroid Build Coastguard Worker   //     cmp xDestLo, xDesiredLo
704*9880d681SAndroid Build Coastguard Worker   //     sbcs xDestHi, xDesiredHi
705*9880d681SAndroid Build Coastguard Worker   //     b.ne .Ldone
706*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(Addr.getReg());
707*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(DestLo.getReg());
708*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(DestHi.getReg());
709*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(DesiredLo.getReg());
710*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addLiveIn(DesiredHi.getReg());
711*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(LoadCmpBB, LiveRegs);
712*9880d681SAndroid Build Coastguard Worker 
713*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(AArch64::LDAXPX))
714*9880d681SAndroid Build Coastguard Worker       .addReg(DestLo.getReg(), RegState::Define)
715*9880d681SAndroid Build Coastguard Worker       .addReg(DestHi.getReg(), RegState::Define)
716*9880d681SAndroid Build Coastguard Worker       .addReg(Addr.getReg());
717*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(AArch64::SUBSXrs), AArch64::XZR)
718*9880d681SAndroid Build Coastguard Worker       .addReg(DestLo.getReg(), getKillRegState(DestLo.isDead()))
719*9880d681SAndroid Build Coastguard Worker       .addOperand(DesiredLo)
720*9880d681SAndroid Build Coastguard Worker       .addImm(0);
721*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(AArch64::SBCSXr), AArch64::XZR)
722*9880d681SAndroid Build Coastguard Worker       .addReg(DestHi.getReg(), getKillRegState(DestHi.isDead()))
723*9880d681SAndroid Build Coastguard Worker       .addOperand(DesiredHi);
724*9880d681SAndroid Build Coastguard Worker   BuildMI(LoadCmpBB, DL, TII->get(AArch64::Bcc))
725*9880d681SAndroid Build Coastguard Worker       .addImm(AArch64CC::NE)
726*9880d681SAndroid Build Coastguard Worker       .addMBB(DoneBB)
727*9880d681SAndroid Build Coastguard Worker       .addReg(AArch64::NZCV, RegState::Implicit | RegState::Kill);
728*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addSuccessor(DoneBB);
729*9880d681SAndroid Build Coastguard Worker   LoadCmpBB->addSuccessor(StoreBB);
730*9880d681SAndroid Build Coastguard Worker 
731*9880d681SAndroid Build Coastguard Worker   // .Lstore:
732*9880d681SAndroid Build Coastguard Worker   //     stlxp wStatus, xNewLo, xNewHi, [xAddr]
733*9880d681SAndroid Build Coastguard Worker   //     cbnz wStatus, .Lloadcmp
734*9880d681SAndroid Build Coastguard Worker   StoreBB->addLiveIn(Addr.getReg());
735*9880d681SAndroid Build Coastguard Worker   StoreBB->addLiveIn(NewLo.getReg());
736*9880d681SAndroid Build Coastguard Worker   StoreBB->addLiveIn(NewHi.getReg());
737*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(StoreBB, LiveRegs);
738*9880d681SAndroid Build Coastguard Worker   BuildMI(StoreBB, DL, TII->get(AArch64::STLXPX), StatusReg)
739*9880d681SAndroid Build Coastguard Worker       .addOperand(NewLo)
740*9880d681SAndroid Build Coastguard Worker       .addOperand(NewHi)
741*9880d681SAndroid Build Coastguard Worker       .addOperand(Addr);
742*9880d681SAndroid Build Coastguard Worker   BuildMI(StoreBB, DL, TII->get(AArch64::CBNZW))
743*9880d681SAndroid Build Coastguard Worker       .addReg(StatusReg, RegState::Kill)
744*9880d681SAndroid Build Coastguard Worker       .addMBB(LoadCmpBB);
745*9880d681SAndroid Build Coastguard Worker   StoreBB->addSuccessor(LoadCmpBB);
746*9880d681SAndroid Build Coastguard Worker   StoreBB->addSuccessor(DoneBB);
747*9880d681SAndroid Build Coastguard Worker 
748*9880d681SAndroid Build Coastguard Worker   DoneBB->splice(DoneBB->end(), &MBB, MI, MBB.end());
749*9880d681SAndroid Build Coastguard Worker   DoneBB->transferSuccessors(&MBB);
750*9880d681SAndroid Build Coastguard Worker   addPostLoopLiveIns(DoneBB, LiveRegs);
751*9880d681SAndroid Build Coastguard Worker 
752*9880d681SAndroid Build Coastguard Worker   MBB.addSuccessor(LoadCmpBB);
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker   NextMBBI = MBB.end();
755*9880d681SAndroid Build Coastguard Worker   MI.eraseFromParent();
756*9880d681SAndroid Build Coastguard Worker   return true;
757*9880d681SAndroid Build Coastguard Worker }
758*9880d681SAndroid Build Coastguard Worker 
759*9880d681SAndroid Build Coastguard Worker /// \brief If MBBI references a pseudo instruction that should be expanded here,
760*9880d681SAndroid Build Coastguard Worker /// do the expansion and return true.  Otherwise return false.
expandMI(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)761*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::expandMI(MachineBasicBlock &MBB,
762*9880d681SAndroid Build Coastguard Worker                                    MachineBasicBlock::iterator MBBI,
763*9880d681SAndroid Build Coastguard Worker                                    MachineBasicBlock::iterator &NextMBBI) {
764*9880d681SAndroid Build Coastguard Worker   MachineInstr &MI = *MBBI;
765*9880d681SAndroid Build Coastguard Worker   unsigned Opcode = MI.getOpcode();
766*9880d681SAndroid Build Coastguard Worker   switch (Opcode) {
767*9880d681SAndroid Build Coastguard Worker   default:
768*9880d681SAndroid Build Coastguard Worker     break;
769*9880d681SAndroid Build Coastguard Worker 
770*9880d681SAndroid Build Coastguard Worker   case AArch64::ADDWrr:
771*9880d681SAndroid Build Coastguard Worker   case AArch64::SUBWrr:
772*9880d681SAndroid Build Coastguard Worker   case AArch64::ADDXrr:
773*9880d681SAndroid Build Coastguard Worker   case AArch64::SUBXrr:
774*9880d681SAndroid Build Coastguard Worker   case AArch64::ADDSWrr:
775*9880d681SAndroid Build Coastguard Worker   case AArch64::SUBSWrr:
776*9880d681SAndroid Build Coastguard Worker   case AArch64::ADDSXrr:
777*9880d681SAndroid Build Coastguard Worker   case AArch64::SUBSXrr:
778*9880d681SAndroid Build Coastguard Worker   case AArch64::ANDWrr:
779*9880d681SAndroid Build Coastguard Worker   case AArch64::ANDXrr:
780*9880d681SAndroid Build Coastguard Worker   case AArch64::BICWrr:
781*9880d681SAndroid Build Coastguard Worker   case AArch64::BICXrr:
782*9880d681SAndroid Build Coastguard Worker   case AArch64::ANDSWrr:
783*9880d681SAndroid Build Coastguard Worker   case AArch64::ANDSXrr:
784*9880d681SAndroid Build Coastguard Worker   case AArch64::BICSWrr:
785*9880d681SAndroid Build Coastguard Worker   case AArch64::BICSXrr:
786*9880d681SAndroid Build Coastguard Worker   case AArch64::EONWrr:
787*9880d681SAndroid Build Coastguard Worker   case AArch64::EONXrr:
788*9880d681SAndroid Build Coastguard Worker   case AArch64::EORWrr:
789*9880d681SAndroid Build Coastguard Worker   case AArch64::EORXrr:
790*9880d681SAndroid Build Coastguard Worker   case AArch64::ORNWrr:
791*9880d681SAndroid Build Coastguard Worker   case AArch64::ORNXrr:
792*9880d681SAndroid Build Coastguard Worker   case AArch64::ORRWrr:
793*9880d681SAndroid Build Coastguard Worker   case AArch64::ORRXrr: {
794*9880d681SAndroid Build Coastguard Worker     unsigned Opcode;
795*9880d681SAndroid Build Coastguard Worker     switch (MI.getOpcode()) {
796*9880d681SAndroid Build Coastguard Worker     default:
797*9880d681SAndroid Build Coastguard Worker       return false;
798*9880d681SAndroid Build Coastguard Worker     case AArch64::ADDWrr:      Opcode = AArch64::ADDWrs; break;
799*9880d681SAndroid Build Coastguard Worker     case AArch64::SUBWrr:      Opcode = AArch64::SUBWrs; break;
800*9880d681SAndroid Build Coastguard Worker     case AArch64::ADDXrr:      Opcode = AArch64::ADDXrs; break;
801*9880d681SAndroid Build Coastguard Worker     case AArch64::SUBXrr:      Opcode = AArch64::SUBXrs; break;
802*9880d681SAndroid Build Coastguard Worker     case AArch64::ADDSWrr:     Opcode = AArch64::ADDSWrs; break;
803*9880d681SAndroid Build Coastguard Worker     case AArch64::SUBSWrr:     Opcode = AArch64::SUBSWrs; break;
804*9880d681SAndroid Build Coastguard Worker     case AArch64::ADDSXrr:     Opcode = AArch64::ADDSXrs; break;
805*9880d681SAndroid Build Coastguard Worker     case AArch64::SUBSXrr:     Opcode = AArch64::SUBSXrs; break;
806*9880d681SAndroid Build Coastguard Worker     case AArch64::ANDWrr:      Opcode = AArch64::ANDWrs; break;
807*9880d681SAndroid Build Coastguard Worker     case AArch64::ANDXrr:      Opcode = AArch64::ANDXrs; break;
808*9880d681SAndroid Build Coastguard Worker     case AArch64::BICWrr:      Opcode = AArch64::BICWrs; break;
809*9880d681SAndroid Build Coastguard Worker     case AArch64::BICXrr:      Opcode = AArch64::BICXrs; break;
810*9880d681SAndroid Build Coastguard Worker     case AArch64::ANDSWrr:     Opcode = AArch64::ANDSWrs; break;
811*9880d681SAndroid Build Coastguard Worker     case AArch64::ANDSXrr:     Opcode = AArch64::ANDSXrs; break;
812*9880d681SAndroid Build Coastguard Worker     case AArch64::BICSWrr:     Opcode = AArch64::BICSWrs; break;
813*9880d681SAndroid Build Coastguard Worker     case AArch64::BICSXrr:     Opcode = AArch64::BICSXrs; break;
814*9880d681SAndroid Build Coastguard Worker     case AArch64::EONWrr:      Opcode = AArch64::EONWrs; break;
815*9880d681SAndroid Build Coastguard Worker     case AArch64::EONXrr:      Opcode = AArch64::EONXrs; break;
816*9880d681SAndroid Build Coastguard Worker     case AArch64::EORWrr:      Opcode = AArch64::EORWrs; break;
817*9880d681SAndroid Build Coastguard Worker     case AArch64::EORXrr:      Opcode = AArch64::EORXrs; break;
818*9880d681SAndroid Build Coastguard Worker     case AArch64::ORNWrr:      Opcode = AArch64::ORNWrs; break;
819*9880d681SAndroid Build Coastguard Worker     case AArch64::ORNXrr:      Opcode = AArch64::ORNXrs; break;
820*9880d681SAndroid Build Coastguard Worker     case AArch64::ORRWrr:      Opcode = AArch64::ORRWrs; break;
821*9880d681SAndroid Build Coastguard Worker     case AArch64::ORRXrr:      Opcode = AArch64::ORRXrs; break;
822*9880d681SAndroid Build Coastguard Worker     }
823*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB1 =
824*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opcode),
825*9880d681SAndroid Build Coastguard Worker                 MI.getOperand(0).getReg())
826*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(1))
827*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(2))
828*9880d681SAndroid Build Coastguard Worker             .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
829*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB1, MIB1);
830*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
831*9880d681SAndroid Build Coastguard Worker     return true;
832*9880d681SAndroid Build Coastguard Worker   }
833*9880d681SAndroid Build Coastguard Worker 
834*9880d681SAndroid Build Coastguard Worker   case AArch64::LOADgot: {
835*9880d681SAndroid Build Coastguard Worker     // Expand into ADRP + LDR.
836*9880d681SAndroid Build Coastguard Worker     unsigned DstReg = MI.getOperand(0).getReg();
837*9880d681SAndroid Build Coastguard Worker     const MachineOperand &MO1 = MI.getOperand(1);
838*9880d681SAndroid Build Coastguard Worker     unsigned Flags = MO1.getTargetFlags();
839*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB1 =
840*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg);
841*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB2 =
842*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::LDRXui))
843*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(0))
844*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg);
845*9880d681SAndroid Build Coastguard Worker 
846*9880d681SAndroid Build Coastguard Worker     if (MO1.isGlobal()) {
847*9880d681SAndroid Build Coastguard Worker       MIB1.addGlobalAddress(MO1.getGlobal(), 0, Flags | AArch64II::MO_PAGE);
848*9880d681SAndroid Build Coastguard Worker       MIB2.addGlobalAddress(MO1.getGlobal(), 0,
849*9880d681SAndroid Build Coastguard Worker                             Flags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
850*9880d681SAndroid Build Coastguard Worker     } else if (MO1.isSymbol()) {
851*9880d681SAndroid Build Coastguard Worker       MIB1.addExternalSymbol(MO1.getSymbolName(), Flags | AArch64II::MO_PAGE);
852*9880d681SAndroid Build Coastguard Worker       MIB2.addExternalSymbol(MO1.getSymbolName(),
853*9880d681SAndroid Build Coastguard Worker                              Flags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
854*9880d681SAndroid Build Coastguard Worker     } else {
855*9880d681SAndroid Build Coastguard Worker       assert(MO1.isCPI() &&
856*9880d681SAndroid Build Coastguard Worker              "Only expect globals, externalsymbols, or constant pools");
857*9880d681SAndroid Build Coastguard Worker       MIB1.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
858*9880d681SAndroid Build Coastguard Worker                                 Flags | AArch64II::MO_PAGE);
859*9880d681SAndroid Build Coastguard Worker       MIB2.addConstantPoolIndex(MO1.getIndex(), MO1.getOffset(),
860*9880d681SAndroid Build Coastguard Worker                                 Flags | AArch64II::MO_PAGEOFF |
861*9880d681SAndroid Build Coastguard Worker                                     AArch64II::MO_NC);
862*9880d681SAndroid Build Coastguard Worker     }
863*9880d681SAndroid Build Coastguard Worker 
864*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB1, MIB2);
865*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
866*9880d681SAndroid Build Coastguard Worker     return true;
867*9880d681SAndroid Build Coastguard Worker   }
868*9880d681SAndroid Build Coastguard Worker 
869*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddr:
870*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddrJT:
871*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddrCP:
872*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddrBA:
873*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddrTLS:
874*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVaddrEXT: {
875*9880d681SAndroid Build Coastguard Worker     // Expand into ADRP + ADD.
876*9880d681SAndroid Build Coastguard Worker     unsigned DstReg = MI.getOperand(0).getReg();
877*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB1 =
878*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg)
879*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(1));
880*9880d681SAndroid Build Coastguard Worker 
881*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB2 =
882*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
883*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(0))
884*9880d681SAndroid Build Coastguard Worker             .addReg(DstReg)
885*9880d681SAndroid Build Coastguard Worker             .addOperand(MI.getOperand(2))
886*9880d681SAndroid Build Coastguard Worker             .addImm(0);
887*9880d681SAndroid Build Coastguard Worker 
888*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB1, MIB2);
889*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
890*9880d681SAndroid Build Coastguard Worker     return true;
891*9880d681SAndroid Build Coastguard Worker   }
892*9880d681SAndroid Build Coastguard Worker 
893*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVi32imm:
894*9880d681SAndroid Build Coastguard Worker     return expandMOVImm(MBB, MBBI, 32);
895*9880d681SAndroid Build Coastguard Worker   case AArch64::MOVi64imm:
896*9880d681SAndroid Build Coastguard Worker     return expandMOVImm(MBB, MBBI, 64);
897*9880d681SAndroid Build Coastguard Worker   case AArch64::RET_ReallyLR: {
898*9880d681SAndroid Build Coastguard Worker     MachineInstrBuilder MIB =
899*9880d681SAndroid Build Coastguard Worker         BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::RET))
900*9880d681SAndroid Build Coastguard Worker           .addReg(AArch64::LR);
901*9880d681SAndroid Build Coastguard Worker     transferImpOps(MI, MIB, MIB);
902*9880d681SAndroid Build Coastguard Worker     MI.eraseFromParent();
903*9880d681SAndroid Build Coastguard Worker     return true;
904*9880d681SAndroid Build Coastguard Worker   }
905*9880d681SAndroid Build Coastguard Worker   case AArch64::CMP_SWAP_8:
906*9880d681SAndroid Build Coastguard Worker     return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRB, AArch64::STLXRB,
907*9880d681SAndroid Build Coastguard Worker                           AArch64::SUBSWrx,
908*9880d681SAndroid Build Coastguard Worker                           AArch64_AM::getArithExtendImm(AArch64_AM::UXTB, 0),
909*9880d681SAndroid Build Coastguard Worker                           AArch64::WZR, NextMBBI);
910*9880d681SAndroid Build Coastguard Worker   case AArch64::CMP_SWAP_16:
911*9880d681SAndroid Build Coastguard Worker     return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRH, AArch64::STLXRH,
912*9880d681SAndroid Build Coastguard Worker                           AArch64::SUBSWrx,
913*9880d681SAndroid Build Coastguard Worker                           AArch64_AM::getArithExtendImm(AArch64_AM::UXTH, 0),
914*9880d681SAndroid Build Coastguard Worker                           AArch64::WZR, NextMBBI);
915*9880d681SAndroid Build Coastguard Worker   case AArch64::CMP_SWAP_32:
916*9880d681SAndroid Build Coastguard Worker     return expandCMP_SWAP(MBB, MBBI, AArch64::LDAXRW, AArch64::STLXRW,
917*9880d681SAndroid Build Coastguard Worker                           AArch64::SUBSWrs,
918*9880d681SAndroid Build Coastguard Worker                           AArch64_AM::getShifterImm(AArch64_AM::LSL, 0),
919*9880d681SAndroid Build Coastguard Worker                           AArch64::WZR, NextMBBI);
920*9880d681SAndroid Build Coastguard Worker   case AArch64::CMP_SWAP_64:
921*9880d681SAndroid Build Coastguard Worker     return expandCMP_SWAP(MBB, MBBI,
922*9880d681SAndroid Build Coastguard Worker                           AArch64::LDAXRX, AArch64::STLXRX, AArch64::SUBSXrs,
923*9880d681SAndroid Build Coastguard Worker                           AArch64_AM::getShifterImm(AArch64_AM::LSL, 0),
924*9880d681SAndroid Build Coastguard Worker                           AArch64::XZR, NextMBBI);
925*9880d681SAndroid Build Coastguard Worker   case AArch64::CMP_SWAP_128:
926*9880d681SAndroid Build Coastguard Worker     return expandCMP_SWAP_128(MBB, MBBI, NextMBBI);
927*9880d681SAndroid Build Coastguard Worker   }
928*9880d681SAndroid Build Coastguard Worker   return false;
929*9880d681SAndroid Build Coastguard Worker }
930*9880d681SAndroid Build Coastguard Worker 
931*9880d681SAndroid Build Coastguard Worker /// \brief Iterate over the instructions in basic block MBB and expand any
932*9880d681SAndroid Build Coastguard Worker /// pseudo instructions.  Return true if anything was modified.
expandMBB(MachineBasicBlock & MBB)933*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::expandMBB(MachineBasicBlock &MBB) {
934*9880d681SAndroid Build Coastguard Worker   bool Modified = false;
935*9880d681SAndroid Build Coastguard Worker 
936*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
937*9880d681SAndroid Build Coastguard Worker   while (MBBI != E) {
938*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock::iterator NMBBI = std::next(MBBI);
939*9880d681SAndroid Build Coastguard Worker     Modified |= expandMI(MBB, MBBI, NMBBI);
940*9880d681SAndroid Build Coastguard Worker     MBBI = NMBBI;
941*9880d681SAndroid Build Coastguard Worker   }
942*9880d681SAndroid Build Coastguard Worker 
943*9880d681SAndroid Build Coastguard Worker   return Modified;
944*9880d681SAndroid Build Coastguard Worker }
945*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)946*9880d681SAndroid Build Coastguard Worker bool AArch64ExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
947*9880d681SAndroid Build Coastguard Worker   TII = static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo());
948*9880d681SAndroid Build Coastguard Worker 
949*9880d681SAndroid Build Coastguard Worker   bool Modified = false;
950*9880d681SAndroid Build Coastguard Worker   for (auto &MBB : MF)
951*9880d681SAndroid Build Coastguard Worker     Modified |= expandMBB(MBB);
952*9880d681SAndroid Build Coastguard Worker   return Modified;
953*9880d681SAndroid Build Coastguard Worker }
954*9880d681SAndroid Build Coastguard Worker 
955*9880d681SAndroid Build Coastguard Worker /// \brief Returns an instance of the pseudo instruction expansion pass.
createAArch64ExpandPseudoPass()956*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAArch64ExpandPseudoPass() {
957*9880d681SAndroid Build Coastguard Worker   return new AArch64ExpandPseudo();
958*9880d681SAndroid Build Coastguard Worker }
959