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