LLVM API Documentation

MipsAnalyzeImmediate.cpp
Go to the documentation of this file.
00001 //===-- MipsAnalyzeImmediate.cpp - Analyze Immediates ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 #include "MipsAnalyzeImmediate.h"
00010 #include "Mips.h"
00011 #include "llvm/Support/MathExtras.h"
00012 
00013 using namespace llvm;
00014 
00015 MipsAnalyzeImmediate::Inst::Inst(unsigned O, unsigned I) : Opc(O), ImmOpnd(I) {}
00016 
00017 // Add I to the instruction sequences.
00018 void MipsAnalyzeImmediate::AddInstr(InstSeqLs &SeqLs, const Inst &I) {
00019   // Add an instruction seqeunce consisting of just I.
00020   if (SeqLs.empty()) {
00021     SeqLs.push_back(InstSeq(1, I));
00022     return;
00023   }
00024 
00025   for (InstSeqLs::iterator Iter = SeqLs.begin(); Iter != SeqLs.end(); ++Iter)
00026     Iter->push_back(I);
00027 }
00028 
00029 void MipsAnalyzeImmediate::GetInstSeqLsADDiu(uint64_t Imm, unsigned RemSize,
00030                                              InstSeqLs &SeqLs) {
00031   GetInstSeqLs((Imm + 0x8000ULL) & 0xffffffffffff0000ULL, RemSize, SeqLs);
00032   AddInstr(SeqLs, Inst(ADDiu, Imm & 0xffffULL));
00033 }
00034 
00035 void MipsAnalyzeImmediate::GetInstSeqLsORi(uint64_t Imm, unsigned RemSize,
00036                                            InstSeqLs &SeqLs) {
00037   GetInstSeqLs(Imm & 0xffffffffffff0000ULL, RemSize, SeqLs);
00038   AddInstr(SeqLs, Inst(ORi, Imm & 0xffffULL));
00039 }
00040 
00041 void MipsAnalyzeImmediate::GetInstSeqLsSLL(uint64_t Imm, unsigned RemSize,
00042                                            InstSeqLs &SeqLs) {
00043   unsigned Shamt = countTrailingZeros(Imm);
00044   GetInstSeqLs(Imm >> Shamt, RemSize - Shamt, SeqLs);
00045   AddInstr(SeqLs, Inst(SLL, Shamt));
00046 }
00047 
00048 void MipsAnalyzeImmediate::GetInstSeqLs(uint64_t Imm, unsigned RemSize,
00049                                         InstSeqLs &SeqLs) {
00050   uint64_t MaskedImm = Imm & (0xffffffffffffffffULL >> (64 - Size));
00051 
00052   // Do nothing if Imm is 0.
00053   if (!MaskedImm)
00054     return;
00055 
00056   // A single ADDiu will do if RemSize <= 16.
00057   if (RemSize <= 16) {
00058     AddInstr(SeqLs, Inst(ADDiu, MaskedImm));
00059     return;
00060   }
00061 
00062   // Shift if the lower 16-bit is cleared.
00063   if (!(Imm & 0xffff)) {
00064     GetInstSeqLsSLL(Imm, RemSize, SeqLs);
00065     return;
00066   }
00067 
00068   GetInstSeqLsADDiu(Imm, RemSize, SeqLs);
00069 
00070   // If bit 15 is cleared, it doesn't make a difference whether the last
00071   // instruction is an ADDiu or ORi. In that case, do not call GetInstSeqLsORi.
00072   if (Imm & 0x8000) {
00073     InstSeqLs SeqLsORi;
00074     GetInstSeqLsORi(Imm, RemSize, SeqLsORi);
00075     SeqLs.insert(SeqLs.end(), SeqLsORi.begin(), SeqLsORi.end());
00076   }
00077 }
00078 
00079 // Replace a ADDiu & SLL pair with a LUi.
00080 // e.g. the following two instructions
00081 //  ADDiu 0x0111
00082 //  SLL 18
00083 // are replaced with
00084 //  LUi 0x444
00085 void MipsAnalyzeImmediate::ReplaceADDiuSLLWithLUi(InstSeq &Seq) {
00086   // Check if the first two instructions are ADDiu and SLL and the shift amount
00087   // is at least 16.
00088   if ((Seq.size() < 2) || (Seq[0].Opc != ADDiu) ||
00089       (Seq[1].Opc != SLL) || (Seq[1].ImmOpnd < 16))
00090     return;
00091 
00092   // Sign-extend and shift operand of ADDiu and see if it still fits in 16-bit.
00093   int64_t Imm = SignExtend64<16>(Seq[0].ImmOpnd);
00094   int64_t ShiftedImm = (uint64_t)Imm << (Seq[1].ImmOpnd - 16);
00095 
00096   if (!isInt<16>(ShiftedImm))
00097     return;
00098 
00099   // Replace the first instruction and erase the second.
00100   Seq[0].Opc = LUi;
00101   Seq[0].ImmOpnd = (unsigned)(ShiftedImm & 0xffff);
00102   Seq.erase(Seq.begin() + 1);
00103 }
00104 
00105 void MipsAnalyzeImmediate::GetShortestSeq(InstSeqLs &SeqLs, InstSeq &Insts) {
00106   InstSeqLs::iterator ShortestSeq = SeqLs.end();
00107   // The length of an instruction sequence is at most 7.
00108   unsigned ShortestLength = 8;
00109 
00110   for (InstSeqLs::iterator S = SeqLs.begin(); S != SeqLs.end(); ++S) {
00111     ReplaceADDiuSLLWithLUi(*S);
00112     assert(S->size() <= 7);
00113 
00114     if (S->size() < ShortestLength) {
00115       ShortestSeq = S;
00116       ShortestLength = S->size();
00117     }
00118   }
00119 
00120   Insts.clear();
00121   Insts.append(ShortestSeq->begin(), ShortestSeq->end());
00122 }
00123 
00124 const MipsAnalyzeImmediate::InstSeq
00125 &MipsAnalyzeImmediate::Analyze(uint64_t Imm, unsigned Size,
00126                                bool LastInstrIsADDiu) {
00127   this->Size = Size;
00128 
00129   if (Size == 32) {
00130     ADDiu = Mips::ADDiu;
00131     ORi = Mips::ORi;
00132     SLL = Mips::SLL;
00133     LUi = Mips::LUi;
00134   } else {
00135     ADDiu = Mips::DADDiu;
00136     ORi = Mips::ORi64;
00137     SLL = Mips::DSLL;
00138     LUi = Mips::LUi64;
00139   }
00140 
00141   InstSeqLs SeqLs;
00142 
00143   // Get the list of instruction sequences.
00144   if (LastInstrIsADDiu | !Imm)
00145     GetInstSeqLsADDiu(Imm, Size, SeqLs);
00146   else
00147     GetInstSeqLs(Imm, Size, SeqLs);
00148 
00149   // Set Insts to the shortest instruction sequence.
00150   GetShortestSeq(SeqLs, Insts);
00151 
00152   return Insts;
00153 }