Turn on FGETSIGN for x86. Followup to 132388. rdar://problem/5660695
[oota-llvm.git] / lib / CodeGen / CallingConvLower.cpp
1 //===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the CCState class, used for lowering and implementing
11 // calling conventions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CodeGen/CallingConvLower.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Target/TargetRegisterInfo.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetLowering.h"
23 using namespace llvm;
24
25 CCState::CCState(CallingConv::ID CC, bool isVarArg, const TargetMachine &tm,
26                  SmallVector<CCValAssign, 16> &locs, LLVMContext &C)
27   : CallingConv(CC), IsVarArg(isVarArg), TM(tm),
28     TRI(*TM.getRegisterInfo()), Locs(locs), Context(C),
29     CallOrPrologue(Invalid) {
30   // No stack is used.
31   StackOffset = 0;
32   
33   clearFirstByValReg();
34   UsedRegs.resize((TRI.getNumRegs()+31)/32);
35 }
36
37 // HandleByVal - Allocate a stack slot large enough to pass an argument by
38 // value. The size and alignment information of the argument is encoded in its
39 // parameter attribute.
40 void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
41                           MVT LocVT, CCValAssign::LocInfo LocInfo,
42                           int MinSize, int MinAlign,
43                           ISD::ArgFlagsTy ArgFlags) {
44   unsigned Align = ArgFlags.getByValAlign();
45   unsigned Size  = ArgFlags.getByValSize();
46   if (MinSize > (int)Size)
47     Size = MinSize;
48   if (MinAlign > (int)Align)
49     Align = MinAlign;
50   TM.getTargetLowering()->HandleByVal(const_cast<CCState*>(this), Size);
51   unsigned Offset = AllocateStack(Size, Align);
52   addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
53 }
54
55 /// MarkAllocated - Mark a register and all of its aliases as allocated.
56 void CCState::MarkAllocated(unsigned Reg) {
57   for (const unsigned *Alias = TRI.getOverlaps(Reg);
58        unsigned Reg = *Alias; ++Alias)
59     UsedRegs[Reg/32] |= 1 << (Reg&31);
60 }
61
62 /// AnalyzeFormalArguments - Analyze an array of argument values,
63 /// incorporating info about the formals into this state.
64 void
65 CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
66                                 CCAssignFn Fn) {
67   unsigned NumArgs = Ins.size();
68
69   for (unsigned i = 0; i != NumArgs; ++i) {
70     MVT ArgVT = Ins[i].VT;
71     ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
72     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
73 #ifndef NDEBUG
74       dbgs() << "Formal argument #" << i << " has unhandled type "
75              << EVT(ArgVT).getEVTString();
76 #endif
77       llvm_unreachable(0);
78     }
79   }
80 }
81
82 /// CheckReturn - Analyze the return values of a function, returning true if
83 /// the return can be performed without sret-demotion, and false otherwise.
84 bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
85                           CCAssignFn Fn) {
86   // Determine which register each value should be copied into.
87   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
88     MVT VT = Outs[i].VT;
89     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
90     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
91       return false;
92   }
93   return true;
94 }
95
96 /// AnalyzeReturn - Analyze the returned values of a return,
97 /// incorporating info about the result values into this state.
98 void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
99                             CCAssignFn Fn) {
100   // Determine which register each value should be copied into.
101   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
102     MVT VT = Outs[i].VT;
103     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
104     if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
105 #ifndef NDEBUG
106       dbgs() << "Return operand #" << i << " has unhandled type "
107              << EVT(VT).getEVTString();
108 #endif
109       llvm_unreachable(0);
110     }
111   }
112 }
113
114 /// AnalyzeCallOperands - Analyze the outgoing arguments to a call,
115 /// incorporating info about the passed values into this state.
116 void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
117                                   CCAssignFn Fn) {
118   unsigned NumOps = Outs.size();
119   for (unsigned i = 0; i != NumOps; ++i) {
120     MVT ArgVT = Outs[i].VT;
121     ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
122     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
123 #ifndef NDEBUG
124       dbgs() << "Call operand #" << i << " has unhandled type "
125              << EVT(ArgVT).getEVTString();
126 #endif
127       llvm_unreachable(0);
128     }
129   }
130 }
131
132 /// AnalyzeCallOperands - Same as above except it takes vectors of types
133 /// and argument flags.
134 void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
135                                   SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
136                                   CCAssignFn Fn) {
137   unsigned NumOps = ArgVTs.size();
138   for (unsigned i = 0; i != NumOps; ++i) {
139     MVT ArgVT = ArgVTs[i];
140     ISD::ArgFlagsTy ArgFlags = Flags[i];
141     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
142 #ifndef NDEBUG
143       dbgs() << "Call operand #" << i << " has unhandled type "
144              << EVT(ArgVT).getEVTString();
145 #endif
146       llvm_unreachable(0);
147     }
148   }
149 }
150
151 /// AnalyzeCallResult - Analyze the return values of a call,
152 /// incorporating info about the passed values into this state.
153 void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
154                                 CCAssignFn Fn) {
155   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
156     MVT VT = Ins[i].VT;
157     ISD::ArgFlagsTy Flags = Ins[i].Flags;
158     if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
159 #ifndef NDEBUG
160       dbgs() << "Call result #" << i << " has unhandled type "
161              << EVT(VT).getEVTString() << "\n";
162 #endif
163       llvm_unreachable(0);
164     }
165   }
166 }
167
168 /// AnalyzeCallResult - Same as above except it's specialized for calls which
169 /// produce a single value.
170 void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
171   if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
172 #ifndef NDEBUG
173     dbgs() << "Call result has unhandled type "
174            << EVT(VT).getEVTString();
175 #endif
176     llvm_unreachable(0);
177   }
178 }