LLVM API Documentation

HexagonCallingConvLower.cpp
Go to the documentation of this file.
00001 //===-- llvm/CallingConvLower.cpp - Calling Convention lowering -----------===//
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 //
00010 // This file implements the Hexagon_CCState class, used for lowering and
00011 // implementing calling conventions. Adapted from the machine independent
00012 // version of the class (CCState) but this handles calls to varargs functions
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "HexagonCallingConvLower.h"
00017 #include "Hexagon.h"
00018 #include "llvm/IR/DataLayout.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetRegisterInfo.h"
00024 #include "llvm/Target/TargetSubtargetInfo.h"
00025 using namespace llvm;
00026 
00027 Hexagon_CCState::Hexagon_CCState(CallingConv::ID CC, bool isVarArg,
00028                                  const TargetMachine &tm,
00029                                  SmallVectorImpl<CCValAssign> &locs,
00030                                  LLVMContext &c)
00031   : CallingConv(CC), IsVarArg(isVarArg), TM(tm), Locs(locs), Context(c) {
00032   // No stack is used.
00033   StackOffset = 0;
00034 
00035   UsedRegs.resize(
00036       (TM.getSubtargetImpl()->getRegisterInfo()->getNumRegs() + 31) / 32);
00037 }
00038 
00039 // HandleByVal - Allocate a stack slot large enough to pass an argument by
00040 // value. The size and alignment information of the argument is encoded in its
00041 // parameter attribute.
00042 void Hexagon_CCState::HandleByVal(unsigned ValNo, EVT ValVT,
00043                                 EVT LocVT, CCValAssign::LocInfo LocInfo,
00044                                 int MinSize, int MinAlign,
00045                                 ISD::ArgFlagsTy ArgFlags) {
00046   unsigned Align = ArgFlags.getByValAlign();
00047   unsigned Size  = ArgFlags.getByValSize();
00048   if (MinSize > (int)Size)
00049     Size = MinSize;
00050   if (MinAlign > (int)Align)
00051     Align = MinAlign;
00052   unsigned Offset = AllocateStack(Size, Align);
00053 
00054   addLoc(CCValAssign::getMem(ValNo, ValVT.getSimpleVT(), Offset,
00055                              LocVT.getSimpleVT(), LocInfo));
00056 }
00057 
00058 /// MarkAllocated - Mark a register and all of its aliases as allocated.
00059 void Hexagon_CCState::MarkAllocated(unsigned Reg) {
00060   const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
00061   for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
00062     UsedRegs[*AI/32] |= 1 << (*AI&31);
00063 }
00064 
00065 /// AnalyzeFormalArguments - Analyze an ISD::FORMAL_ARGUMENTS node,
00066 /// incorporating info about the formals into this state.
00067 void
00068 Hexagon_CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg>
00069                                         &Ins,
00070                                         Hexagon_CCAssignFn Fn,
00071                                         unsigned SretValueInRegs) {
00072   unsigned NumArgs = Ins.size();
00073   unsigned i = 0;
00074 
00075   // If the function returns a small struct in registers, skip
00076   // over the first (dummy) argument.
00077   if (SretValueInRegs != 0) {
00078     ++i;
00079   }
00080 
00081 
00082   for (; i != NumArgs; ++i) {
00083     EVT ArgVT = Ins[i].VT;
00084     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
00085     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this, 0, 0, false)) {
00086       dbgs() << "Formal argument #" << i << " has unhandled type "
00087              << ArgVT.getEVTString() << "\n";
00088       abort();
00089     }
00090   }
00091 }
00092 
00093 /// AnalyzeReturn - Analyze the returned values of an ISD::RET node,
00094 /// incorporating info about the result values into this state.
00095 void
00096 Hexagon_CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
00097                                Hexagon_CCAssignFn Fn,
00098                                unsigned SretValueInRegs) {
00099 
00100   // For Hexagon, Return small structures in registers.
00101   if (SretValueInRegs != 0) {
00102     if (SretValueInRegs <= 32) {
00103       unsigned Reg = Hexagon::R0;
00104       addLoc(CCValAssign::getReg(0, MVT::i32, Reg, MVT::i32,
00105                                  CCValAssign::Full));
00106       return;
00107     }
00108     if (SretValueInRegs <= 64) {
00109       unsigned Reg = Hexagon::D0;
00110       addLoc(CCValAssign::getReg(0, MVT::i64, Reg, MVT::i64,
00111                                  CCValAssign::Full));
00112       return;
00113     }
00114   }
00115 
00116 
00117   // Determine which register each value should be copied into.
00118   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
00119     EVT VT = Outs[i].VT;
00120     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
00121     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this, -1, -1, false)){
00122       dbgs() << "Return operand #" << i << " has unhandled type "
00123            << VT.getEVTString() << "\n";
00124       abort();
00125     }
00126   }
00127 }
00128 
00129 
00130 /// AnalyzeCallOperands - Analyze an ISD::CALL node, incorporating info
00131 /// about the passed values into this state.
00132 void
00133 Hexagon_CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg>
00134                                      &Outs,
00135                                      Hexagon_CCAssignFn Fn,
00136                                      int NonVarArgsParams,
00137                                      unsigned SretValueSize) {
00138   unsigned NumOps = Outs.size();
00139 
00140   unsigned i = 0;
00141   // If the called function returns a small struct in registers, skip
00142   // the first actual parameter. We do not want to pass a pointer to
00143   // the stack location.
00144   if (SretValueSize != 0) {
00145     ++i;
00146   }
00147 
00148   for (; i != NumOps; ++i) {
00149     EVT ArgVT = Outs[i].VT;
00150     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
00151     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this,
00152            NonVarArgsParams, i+1, false)) {
00153       dbgs() << "Call operand #" << i << " has unhandled type "
00154            << ArgVT.getEVTString() << "\n";
00155       abort();
00156     }
00157   }
00158 }
00159 
00160 /// AnalyzeCallOperands - Same as above except it takes vectors of types
00161 /// and argument flags.
00162 void
00163 Hexagon_CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
00164                                      SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00165                                      Hexagon_CCAssignFn Fn) {
00166   unsigned NumOps = ArgVTs.size();
00167   for (unsigned i = 0; i != NumOps; ++i) {
00168     EVT ArgVT = ArgVTs[i];
00169     ISD::ArgFlagsTy ArgFlags = Flags[i];
00170     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this, -1, -1,
00171            false)) {
00172       dbgs() << "Call operand #" << i << " has unhandled type "
00173            << ArgVT.getEVTString() << "\n";
00174       abort();
00175     }
00176   }
00177 }
00178 
00179 /// AnalyzeCallResult - Analyze the return values of an ISD::CALL node,
00180 /// incorporating info about the passed values into this state.
00181 void
00182 Hexagon_CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
00183                                    Hexagon_CCAssignFn Fn,
00184                                    unsigned SretValueInRegs) {
00185 
00186   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
00187     EVT VT = Ins[i].VT;
00188     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
00189       if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this, -1, -1, false)) {
00190         dbgs() << "Call result #" << i << " has unhandled type "
00191                << VT.getEVTString() << "\n";
00192       abort();
00193     }
00194   }
00195 }
00196 
00197 /// AnalyzeCallResult - Same as above except it's specialized for calls which
00198 /// produce a single value.
00199 void Hexagon_CCState::AnalyzeCallResult(EVT VT, Hexagon_CCAssignFn Fn) {
00200   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this, -1, -1,
00201          false)) {
00202     dbgs() << "Call result has unhandled type "
00203          << VT.getEVTString() << "\n";
00204     abort();
00205   }
00206 }