1 //===-- MipsAnalyzeImmediate.cpp - Analyze Immediates ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "MipsAnalyzeImmediate.h"
11 #include "llvm/Support/MathExtras.h"
15 MipsAnalyzeImmediate::Inst::Inst(unsigned O, unsigned I) : Opc(O), ImmOpnd(I) {}
17 // Add I to the instruction sequences.
18 void MipsAnalyzeImmediate::AddInstr(InstSeqLs &SeqLs, const Inst &I) {
19 // Add an instruction seqeunce consisting of just I.
21 SeqLs.push_back(InstSeq(1, I));
25 for (InstSeqLs::iterator Iter = SeqLs.begin(); Iter != SeqLs.end(); ++Iter)
29 void MipsAnalyzeImmediate::GetInstSeqLsADDiu(uint64_t Imm, unsigned RemSize,
31 GetInstSeqLs((Imm + 0x8000ULL) & 0xffffffffffff0000ULL, RemSize, SeqLs);
32 AddInstr(SeqLs, Inst(ADDiu, Imm & 0xffffULL));
35 void MipsAnalyzeImmediate::GetInstSeqLsORi(uint64_t Imm, unsigned RemSize,
37 GetInstSeqLs(Imm & 0xffffffffffff0000ULL, RemSize, SeqLs);
38 AddInstr(SeqLs, Inst(ORi, Imm & 0xffffULL));
41 void MipsAnalyzeImmediate::GetInstSeqLsSLL(uint64_t Imm, unsigned RemSize,
43 unsigned Shamt = countTrailingZeros(Imm);
44 GetInstSeqLs(Imm >> Shamt, RemSize - Shamt, SeqLs);
45 AddInstr(SeqLs, Inst(SLL, Shamt));
48 void MipsAnalyzeImmediate::GetInstSeqLs(uint64_t Imm, unsigned RemSize,
50 uint64_t MaskedImm = Imm & (0xffffffffffffffffULL >> (64 - Size));
52 // Do nothing if Imm is 0.
56 // A single ADDiu will do if RemSize <= 16.
58 AddInstr(SeqLs, Inst(ADDiu, MaskedImm));
62 // Shift if the lower 16-bit is cleared.
63 if (!(Imm & 0xffff)) {
64 GetInstSeqLsSLL(Imm, RemSize, SeqLs);
68 GetInstSeqLsADDiu(Imm, RemSize, SeqLs);
70 // If bit 15 is cleared, it doesn't make a difference whether the last
71 // instruction is an ADDiu or ORi. In that case, do not call GetInstSeqLsORi.
74 GetInstSeqLsORi(Imm, RemSize, SeqLsORi);
75 SeqLs.append(std::make_move_iterator(SeqLsORi.begin()),
76 std::make_move_iterator(SeqLsORi.end()));
80 // Replace a ADDiu & SLL pair with a LUi.
81 // e.g. the following two instructions
86 void MipsAnalyzeImmediate::ReplaceADDiuSLLWithLUi(InstSeq &Seq) {
87 // Check if the first two instructions are ADDiu and SLL and the shift amount
89 if ((Seq.size() < 2) || (Seq[0].Opc != ADDiu) ||
90 (Seq[1].Opc != SLL) || (Seq[1].ImmOpnd < 16))
93 // Sign-extend and shift operand of ADDiu and see if it still fits in 16-bit.
94 int64_t Imm = SignExtend64<16>(Seq[0].ImmOpnd);
95 int64_t ShiftedImm = (uint64_t)Imm << (Seq[1].ImmOpnd - 16);
97 if (!isInt<16>(ShiftedImm))
100 // Replace the first instruction and erase the second.
102 Seq[0].ImmOpnd = (unsigned)(ShiftedImm & 0xffff);
103 Seq.erase(Seq.begin() + 1);
106 void MipsAnalyzeImmediate::GetShortestSeq(InstSeqLs &SeqLs, InstSeq &Insts) {
107 InstSeqLs::iterator ShortestSeq = SeqLs.end();
108 // The length of an instruction sequence is at most 7.
109 unsigned ShortestLength = 8;
111 for (InstSeqLs::iterator S = SeqLs.begin(); S != SeqLs.end(); ++S) {
112 ReplaceADDiuSLLWithLUi(*S);
113 assert(S->size() <= 7);
115 if (S->size() < ShortestLength) {
117 ShortestLength = S->size();
122 Insts.append(ShortestSeq->begin(), ShortestSeq->end());
125 const MipsAnalyzeImmediate::InstSeq
126 &MipsAnalyzeImmediate::Analyze(uint64_t Imm, unsigned Size,
127 bool LastInstrIsADDiu) {
136 ADDiu = Mips::DADDiu;
144 // Get the list of instruction sequences.
145 if (LastInstrIsADDiu | !Imm)
146 GetInstSeqLsADDiu(Imm, Size, SeqLs);
148 GetInstSeqLs(Imm, Size, SeqLs);
150 // Set Insts to the shortest instruction sequence.
151 GetShortestSeq(SeqLs, Insts);