LLVM API Documentation

CallingConvLower.cpp

Go to the documentation of this file.
00001 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
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 CCState class, used for lowering and implementing
00011 // calling conventions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/CodeGen/CallingConvLower.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 #include "llvm/Target/TargetRegisterInfo.h"
00019 #include "llvm/Target/TargetData.h"
00020 #include "llvm/Target/TargetMachine.h"
00021 using namespace llvm;
00022 
00023 CCState::CCState(unsigned CC, bool isVarArg, const TargetMachine &tm,
00024                  SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
00025   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
00026     TRI(*TM.getRegisterInfo()), Locs(locs), Context(C) {
00027   // No stack is used.
00028   StackOffset = 0;
00029   
00030   UsedRegs.resize((TRI.getNumRegs()+31)/32);
00031 }
00032 
00033 // HandleByVal - Allocate a stack slot large enough to pass an argument by
00034 // value. The size and alignment information of the argument is encoded in its
00035 // parameter attribute.
00036 void CCState::HandleByVal(unsigned ValNo, EVT ValVT,
00037                           EVT LocVT, CCValAssign::LocInfo LocInfo,
00038                           int MinSize, int MinAlign,
00039                           ISD::ArgFlagsTy ArgFlags) {
00040   unsigned Align = ArgFlags.getByValAlign();
00041   unsigned Size  = ArgFlags.getByValSize();
00042   if (MinSize > (int)Size)
00043     Size = MinSize;
00044   if (MinAlign > (int)Align)
00045     Align = MinAlign;
00046   unsigned Offset = AllocateStack(Size, Align);
00047 
00048   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
00049 }
00050 
00051 /// MarkAllocated - Mark a register and all of its aliases as allocated.
00052 void CCState::MarkAllocated(unsigned Reg) {
00053   UsedRegs[Reg/32] |= 1 << (Reg&31);
00054   
00055   if (const unsigned *RegAliases = TRI.getAliasSet(Reg))
00056     for (; (Reg = *RegAliases); ++RegAliases)
00057       UsedRegs[Reg/32] |= 1 << (Reg&31);
00058 }
00059 
00060 /// AnalyzeFormalArguments - Analyze an array of argument values,
00061 /// incorporating info about the formals into this state.
00062 void
00063 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
00064                                 CCAssignFn Fn) {
00065   unsigned NumArgs = Ins.size();
00066 
00067   for (unsigned i = 0; i != NumArgs; ++i) {
00068     EVT ArgVT = Ins[i].VT;
00069     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
00070     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00071 #ifndef NDEBUG
00072       cerr << "Formal argument #" << i << " has unhandled type "
00073            << ArgVT.getEVTString();
00074 #endif
00075       llvm_unreachable(0);
00076     }
00077   }
00078 }
00079 
00080 /// AnalyzeReturn - Analyze the returned values of a return,
00081 /// incorporating info about the result values into this state.
00082 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
00083                             CCAssignFn Fn) {
00084   // Determine which register each value should be copied into.
00085   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
00086     EVT VT = Outs[i].Val.getValueType();
00087     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
00088     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
00089 #ifndef NDEBUG
00090       cerr << "Return operand #" << i << " has unhandled type "
00091            << VT.getEVTString();
00092 #endif
00093       llvm_unreachable(0);
00094     }
00095   }
00096 }
00097 
00098 
00099 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
00100 /// incorporating info about the passed values into this state.
00101 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
00102                                   CCAssignFn Fn) {
00103   unsigned NumOps = Outs.size();
00104   for (unsigned i = 0; i != NumOps; ++i) {
00105     EVT ArgVT = Outs[i].Val.getValueType();
00106     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
00107     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00108 #ifndef NDEBUG
00109       cerr << "Call operand #" << i << " has unhandled type "
00110            << ArgVT.getEVTString();
00111 #endif
00112       llvm_unreachable(0);
00113     }
00114   }
00115 }
00116 
00117 /// AnalyzeCallOperands - Same as above except it takes vectors of types
00118 /// and argument flags.
00119 void CCState::AnalyzeCallOperands(SmallVectorImpl<EVT> &ArgVTs,
00120                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
00121                                   CCAssignFn Fn) {
00122   unsigned NumOps = ArgVTs.size();
00123   for (unsigned i = 0; i != NumOps; ++i) {
00124     EVT ArgVT = ArgVTs[i];
00125     ISD::ArgFlagsTy ArgFlags = Flags[i];
00126     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
00127 #ifndef NDEBUG
00128       cerr << "Call operand #" << i << " has unhandled type "
00129            << ArgVT.getEVTString();
00130 #endif
00131       llvm_unreachable(0);
00132     }
00133   }
00134 }
00135 
00136 /// AnalyzeCallResult - Analyze the return values of a call,
00137 /// incorporating info about the passed values into this state.
00138 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
00139                                 CCAssignFn Fn) {
00140   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
00141     EVT VT = Ins[i].VT;
00142     ISD::ArgFlagsTy Flags = Ins[i].Flags;
00143     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
00144 #ifndef NDEBUG
00145       cerr << "Call result #" << i << " has unhandled type "
00146            << VT.getEVTString();
00147 #endif
00148       llvm_unreachable(0);
00149     }
00150   }
00151 }
00152 
00153 /// AnalyzeCallResult - Same as above except it's specialized for calls which
00154 /// produce a single value.
00155 void CCState::AnalyzeCallResult(EVT VT, CCAssignFn Fn) {
00156   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
00157 #ifndef NDEBUG
00158     cerr << "Call result has unhandled type "
00159          << VT.getEVTString();
00160 #endif
00161     llvm_unreachable(0);
00162   }
00163 }