blob: 816d82a8ac613453173feb1d2cc15f297b705f3b [file] [log] [blame]
Nate Begemana3829d52005-04-05 17:32:30 +00001//===-- PPC64ISelPattern.cpp - A pattern matching inst selector for PPC64 -===//
Nate Begemand3e6b942005-04-05 08:51:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemand3e6b942005-04-05 08:51:15 +00008//===----------------------------------------------------------------------===//
9//
Nate Begemana3829d52005-04-05 17:32:30 +000010// This file defines a pattern matching instruction selector for 64 bit PowerPC.
Nate Begemand3e6b942005-04-05 08:51:15 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC64RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetOptions.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
33#include <algorithm>
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
38namespace {
39 class PPC64TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
41 int ReturnAddrIndex; // FrameIndex for return slot.
42 public:
43 PPC64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000044 // Fold away setcc operations if possible.
45 setSetCCIsExpensive();
46
Nate Begemand3e6b942005-04-05 08:51:15 +000047 // Set up the register classes.
48 addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
49 addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
50 addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000051
Nate Begemand3e6b942005-04-05 08:51:15 +000052 // PowerPC has no intrinsics for these particular operations
Chris Lattner644db4e2005-04-09 03:22:30 +000053 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Nate Begemand3e6b942005-04-05 08:51:15 +000054 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
55 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
56 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
57
Chris Lattner08cae7f2005-04-30 04:26:56 +000058 // We don't support sin/cos/sqrt
59 setOperationAction(ISD::FSIN , MVT::f64, Expand);
60 setOperationAction(ISD::FCOS , MVT::f64, Expand);
61 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
62 setOperationAction(ISD::FSIN , MVT::f32, Expand);
63 setOperationAction(ISD::FCOS , MVT::f32, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65
Nate Begemand3e6b942005-04-05 08:51:15 +000066 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
67 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
68 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
69
Nate Begemane88aa5b2005-04-09 03:05:51 +000070 // PowerPC has no SREM/UREM instructions
71 setOperationAction(ISD::SREM, MVT::i64, Expand);
72 setOperationAction(ISD::UREM, MVT::i64, Expand);
73
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000074 // PowerPC has these, but they are not implemented
75 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
76 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000077 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000078
Nate Begemand3e6b942005-04-05 08:51:15 +000079 setShiftAmountFlavor(Extend); // shl X, 32 == 0
80 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000081 addLegalFPImmediate(-0.0); //
Nate Begemand3e6b942005-04-05 08:51:15 +000082
83 computeRegisterProperties();
84 }
85
86 /// LowerArguments - This hook must be implemented to indicate how we should
87 /// lower the arguments for the specified function, into the specified DAG.
88 virtual std::vector<SDOperand>
89 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000090
Nate Begemand3e6b942005-04-05 08:51:15 +000091 /// LowerCallTo - This hook lowers an abstract call to a function into an
92 /// actual call.
93 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000094 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Nate Begemand3e6b942005-04-05 08:51:15 +000095 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000096
Nate Begemand3e6b942005-04-05 08:51:15 +000097 virtual std::pair<SDOperand, SDOperand>
98 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Nate Begemand3e6b942005-04-05 08:51:15 +0000100 virtual std::pair<SDOperand,SDOperand>
101 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
102 const Type *ArgTy, SelectionDAG &DAG);
103
104 virtual std::pair<SDOperand, SDOperand>
105 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
106 SelectionDAG &DAG);
107 };
108}
109
110
111std::vector<SDOperand>
112PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
113 //
114 // add beautiful description of PPC stack frame format, or at least some docs
115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
118 MachineBasicBlock& BB = MF.front();
119 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000120
121 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemand3e6b942005-04-05 08:51:15 +0000122 // fixed size array of physical args, for the sake of simplicity let the STL
123 // handle tracking them for us.
124 std::vector<unsigned> argVR, argPR, argOp;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000125 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000126 unsigned GPR_remaining = 8;
127 unsigned FPR_remaining = 13;
128 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000129 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +0000130 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
131 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
132 };
133 static const unsigned FPR[] = {
134 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
135 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
136 };
137
138 // Add DAG nodes to load the arguments... On entry to a function on PPC,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000139 // the arguments start at offset 48, although they are likely to be passed
Nate Begemand3e6b942005-04-05 08:51:15 +0000140 // in registers.
141 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
142 SDOperand newroot, argt;
Nate Begemand3e6b942005-04-05 08:51:15 +0000143 bool needsLoad = false;
144 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000145
Nate Begemand3e6b942005-04-05 08:51:15 +0000146 switch (ObjectVT) {
147 default: assert(0 && "Unhandled argument type!");
148 case MVT::i1:
149 case MVT::i8:
150 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000151 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000152 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000153 if (GPR_remaining > 0) {
154 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
155 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
156 DAG.getRoot());
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000157 if (ObjectVT != MVT::i64)
Nate Begemand3e6b942005-04-05 08:51:15 +0000158 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
159 } else {
160 needsLoad = true;
161 }
162 break;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000163 case MVT::f32:
164 case MVT::f64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000165 if (FPR_remaining > 0) {
166 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000167 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemand3e6b942005-04-05 08:51:15 +0000168 DAG.getRoot());
169 --FPR_remaining;
170 ++FPR_idx;
171 } else {
172 needsLoad = true;
173 }
174 break;
175 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000176
Nate Begemand3e6b942005-04-05 08:51:15 +0000177 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000178 // that we ran out of physical registers of the appropriate type
Nate Begemand3e6b942005-04-05 08:51:15 +0000179 if (needsLoad) {
180 unsigned SubregOffset = 0;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000181 switch (ObjectVT) {
182 default: assert(0 && "Unhandled argument type!");
183 case MVT::i1:
184 case MVT::i8: SubregOffset = 7; break;
185 case MVT::i16: SubregOffset = 6; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000186 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000187 case MVT::f32: SubregOffset = 4; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000188 case MVT::i64:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000189 case MVT::f64: SubregOffset = 0; break;
190 }
191 int FI = MFI->CreateFixedObject(8, ArgOffset);
192 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000193 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000194 DAG.getConstant(SubregOffset, MVT::i64));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000195 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000196 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000197
Nate Begemand3e6b942005-04-05 08:51:15 +0000198 // Every 4 bytes of argument space consumes one of the GPRs available for
199 // argument passing.
200 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000201 --GPR_remaining;
202 ++GPR_idx;
Nate Begemand3e6b942005-04-05 08:51:15 +0000203 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000204 ArgOffset += 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000205
Nate Begemand3e6b942005-04-05 08:51:15 +0000206 DAG.setRoot(newroot.getValue(1));
207 ArgValues.push_back(argt);
208 }
209
210 // If the function takes variable number of arguments, make a frame index for
211 // the start of the first vararg value... for expansion of llvm.va_start.
212 if (F.isVarArg()) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000213 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
214 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000215 // If this function is vararg, store any remaining integer argument regs
216 // to their spots on the stack so that they may be loaded by deferencing the
217 // result of va_next.
218 std::vector<SDOperand> MemOps;
219 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
220 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000221 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000222 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000223 Val, FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000224 MemOps.push_back(Store);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000225 // Increment the address by eight for the next argument to store
226 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
Nate Begemand3e6b942005-04-05 08:51:15 +0000227 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
228 }
229 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
230 }
231
232 return ArgValues;
233}
234
235std::pair<SDOperand, SDOperand>
236PPC64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000237 const Type *RetTy, bool isVarArg,
Chris Lattnerc57f6822005-05-12 19:56:45 +0000238 unsigned CallingConv,
Misha Brukman7847fca2005-04-22 17:54:37 +0000239 SDOperand Callee, ArgListTy &Args,
240 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000241 // args_to_use will accumulate outgoing args for the ISD::CALL case in
242 // SelectExpr to use to put the arguments in the appropriate registers.
243 std::vector<SDOperand> args_to_use;
244
245 // Count how many bytes are to be pushed on the stack, including the linkage
246 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000247 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000248
249 if (Args.empty()) {
250 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
251 DAG.getConstant(NumBytes, getPointerTy()));
252 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000253 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000254
255 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000256 // plus 64 bytes of argument space in case any called code gets funky on us.
257 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000258
259 // Adjust the stack pointer for the new arguments...
260 // These operations are automatically eliminated by the prolog/epilog pass
261 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
262 DAG.getConstant(NumBytes, getPointerTy()));
263
264 // Set up a copy of the stack pointer for use loading and storing any
265 // arguments that may not fit in the registers available for argument
266 // passing.
267 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
268 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000269
Nate Begemand3e6b942005-04-05 08:51:15 +0000270 // Figure out which arguments are going to go in registers, and which in
271 // memory. Also, if this is a vararg function, floating point operations
272 // must be stored to our stack, and loaded into integer regs as well, if
273 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000274 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000275 unsigned GPR_remaining = 8;
276 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000277
Nate Begemand3e6b942005-04-05 08:51:15 +0000278 std::vector<SDOperand> MemOps;
279 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
280 // PtrOff will be used to store the current argument to the stack if a
281 // register cannot be found for it.
282 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
283 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
284 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000285
Nate Begemand3e6b942005-04-05 08:51:15 +0000286 switch (ArgVT) {
287 default: assert(0 && "Unexpected ValueType for argument!");
288 case MVT::i1:
289 case MVT::i8:
290 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000291 case MVT::i32:
292 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000293 // sign extend, otherwise use a zero extend.
294 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000295 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000296 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000297 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000298 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000299 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000300 if (GPR_remaining > 0) {
301 args_to_use.push_back(Args[i].first);
302 --GPR_remaining;
303 } else {
304 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000305 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000306 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000307 ArgOffset += 8;
308 break;
309 case MVT::f32:
310 case MVT::f64:
311 if (FPR_remaining > 0) {
312 args_to_use.push_back(Args[i].first);
313 --FPR_remaining;
314 if (isVarArg) {
315 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000316 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000317 MemOps.push_back(Store);
318 // Float varargs are always shadowed in available integer registers
319 if (GPR_remaining > 0) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000320 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000321 MemOps.push_back(Load);
322 args_to_use.push_back(Load);
323 --GPR_remaining;
324 }
325 } else {
326 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000327 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000328 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000329 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000330 --GPR_remaining;
331 }
332 }
333 } else {
334 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000335 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000336 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000337 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000338 break;
339 }
340 }
341 if (!MemOps.empty())
342 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
343 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000344
Nate Begemand3e6b942005-04-05 08:51:15 +0000345 std::vector<MVT::ValueType> RetVals;
346 MVT::ValueType RetTyVT = getValueType(RetTy);
347 if (RetTyVT != MVT::isVoid)
348 RetVals.push_back(RetTyVT);
349 RetVals.push_back(MVT::Other);
350
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000351 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000352 Chain, Callee, args_to_use), 0);
353 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
354 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
355 DAG.getConstant(NumBytes, getPointerTy()));
356 return std::make_pair(TheCall, Chain);
357}
358
359std::pair<SDOperand, SDOperand>
360PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
361 //vastart just returns the address of the VarArgsFrameIndex slot.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000362 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
Nate Begemand3e6b942005-04-05 08:51:15 +0000363}
364
365std::pair<SDOperand,SDOperand> PPC64TargetLowering::
366LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
367 const Type *ArgTy, SelectionDAG &DAG) {
368 MVT::ValueType ArgVT = getValueType(ArgTy);
369 SDOperand Result;
370 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000371 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000372 } else {
Nate Begemand3e6b942005-04-05 08:51:15 +0000373 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000374 DAG.getConstant(8, VAList.getValueType()));
Nate Begemand3e6b942005-04-05 08:51:15 +0000375 }
376 return std::make_pair(Result, Chain);
377}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000378
Nate Begemand3e6b942005-04-05 08:51:15 +0000379
380std::pair<SDOperand, SDOperand> PPC64TargetLowering::
381LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
382 SelectionDAG &DAG) {
383 assert(0 && "LowerFrameReturnAddress unimplemented");
384 abort();
385}
386
387namespace {
388Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
389Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
390//===--------------------------------------------------------------------===//
391/// ISel - PPC32 specific code to select PPC32 machine instructions for
392/// SelectionDAG operations.
393//===--------------------------------------------------------------------===//
394class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000395
Nate Begemand3e6b942005-04-05 08:51:15 +0000396 /// Comment Here.
397 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000398
Nate Begemand3e6b942005-04-05 08:51:15 +0000399 /// ExprMap - As shared expressions are codegen'd, we keep track of which
400 /// vreg the value is produced in, so we only emit one copy of each compiled
401 /// tree.
402 std::map<SDOperand, unsigned> ExprMap;
403
404 unsigned GlobalBaseReg;
405 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000406
Nate Begemand3e6b942005-04-05 08:51:15 +0000407public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000408 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000409 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000410
Nate Begemand3e6b942005-04-05 08:51:15 +0000411 /// runOnFunction - Override this function in order to reset our per-function
412 /// variables.
413 virtual bool runOnFunction(Function &Fn) {
414 // Make sure we re-emit a set of the global base reg if necessary
415 GlobalBaseInitialized = false;
416 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000417 }
418
Nate Begemand3e6b942005-04-05 08:51:15 +0000419 /// InstructionSelectBasicBlock - This callback is invoked by
420 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
421 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
422 DEBUG(BB->dump());
423 // Codegen the basic block.
424 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000425
Nate Begemand3e6b942005-04-05 08:51:15 +0000426 // Clear state used for selection.
427 ExprMap.clear();
428 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000429
Nate Begemand3e6b942005-04-05 08:51:15 +0000430 unsigned getGlobalBaseReg();
431 unsigned getConstDouble(double floatVal, unsigned Result);
432 unsigned SelectSetCR0(SDOperand CC);
433 unsigned SelectExpr(SDOperand N);
434 unsigned SelectExprFP(SDOperand N, unsigned Result);
435 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000436
Nate Begemand3e6b942005-04-05 08:51:15 +0000437 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
438 void SelectBranchCC(SDOperand N);
439};
440
441/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
442/// returns zero when the input is not exactly a power of two.
443static unsigned ExactLog2(unsigned Val) {
444 if (Val == 0 || (Val & (Val-1))) return 0;
445 unsigned Count = 0;
446 while (Val != 1) {
447 Val >>= 1;
448 ++Count;
449 }
450 return Count;
451}
452
453/// getImmediateForOpcode - This method returns a value indicating whether
454/// the ConstantSDNode N can be used as an immediate to Opcode. The return
455/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000456/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000457/// of 1 indicates that the constant may be used in normal immediate form. A
458/// return value of 2 indicates that the constant may be used in shifted
459/// immediate form. A return value of 3 indicates that log base 2 of the
460/// constant may be used.
461///
462static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
463 unsigned& Imm, bool U = false) {
464 if (N.getOpcode() != ISD::Constant) return 0;
465
466 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000467
Nate Begemand3e6b942005-04-05 08:51:15 +0000468 switch(Opcode) {
469 default: return 0;
470 case ISD::ADD:
471 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
472 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
473 break;
474 case ISD::AND:
475 case ISD::XOR:
476 case ISD::OR:
477 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
478 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
479 break;
480 case ISD::MUL:
481 case ISD::SUB:
482 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
483 break;
484 case ISD::SETCC:
485 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
486 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
487 break;
488 case ISD::SDIV:
489 if ((Imm = ExactLog2(v))) { return 3; }
490 break;
491 }
492 return 0;
493}
494
495/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
496/// to Condition. If the Condition is unordered or unsigned, the bool argument
497/// U is set to true, otherwise it is set to false.
498static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
499 U = false;
500 switch (Condition) {
501 default: assert(0 && "Unknown condition!"); abort();
502 case ISD::SETEQ: return PPC::BEQ;
503 case ISD::SETNE: return PPC::BNE;
504 case ISD::SETULT: U = true;
505 case ISD::SETLT: return PPC::BLT;
506 case ISD::SETULE: U = true;
507 case ISD::SETLE: return PPC::BLE;
508 case ISD::SETUGT: U = true;
509 case ISD::SETGT: return PPC::BGT;
510 case ISD::SETUGE: U = true;
511 case ISD::SETGE: return PPC::BGE;
512 }
513 return 0;
514}
515
516/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
517/// and store immediate instructions.
518static unsigned IndexedOpForOp(unsigned Opcode) {
519 switch(Opcode) {
520 default: assert(0 && "Unknown opcode!"); abort();
521 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
522 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
523 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
524 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
525 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
526 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
527 case PPC::LFD: return PPC::LFDX;
528 }
529 return 0;
530}
531}
532
533/// getGlobalBaseReg - Output the instructions required to put the
534/// base address to use for accessing globals into a register.
535///
536unsigned ISel::getGlobalBaseReg() {
537 if (!GlobalBaseInitialized) {
538 // Insert the set of GlobalBaseReg into the first MBB of the function
539 MachineBasicBlock &FirstMBB = BB->getParent()->front();
540 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
541 GlobalBaseReg = MakeReg(MVT::i64);
542 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
543 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
544 GlobalBaseInitialized = true;
545 }
546 return GlobalBaseReg;
547}
548
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000549/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000550/// Constant Pool. Optionally takes a register in which to load the value.
551unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000552 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000553 if (0 == Result) Result = MakeReg(MVT::f64);
554 MachineConstantPool *CP = BB->getParent()->getConstantPool();
555 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
556 unsigned CPI = CP->getConstantPoolIndex(CFP);
557 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
558 .addConstantPoolIndex(CPI);
559 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
560 return Result;
561}
562
563unsigned ISel::SelectSetCR0(SDOperand CC) {
564 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000565 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000566 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000567
Nate Begemand3e6b942005-04-05 08:51:15 +0000568 // If the first operand to the select is a SETCC node, then we can fold it
569 // into the branch that selects which value to return.
570 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
571 if (SetCC && CC.getOpcode() == ISD::SETCC) {
572 bool U;
573 Opc = getBCCForSetCC(SetCC->getCondition(), U);
574 Tmp1 = SelectExpr(SetCC->getOperand(0));
575
576 // Pass the optional argument U to getImmediateForOpcode for SETCC,
577 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000578 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000579 Tmp2, U)) {
580 if (U)
581 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
582 else
583 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
584 } else {
585 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
586 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
587 Tmp2 = SelectExpr(SetCC->getOperand(1));
588 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
589 }
590 } else {
591 Tmp1 = SelectExpr(CC);
592 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
593 Opc = PPC::BNE;
594 }
595 return Opc;
596}
597
598/// Check to see if the load is a constant offset from a base register
599bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
600{
601 unsigned imm = 0, opcode = N.getOpcode();
602 if (N.getOpcode() == ISD::ADD) {
603 Reg = SelectExpr(N.getOperand(0));
604 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
605 offset = imm;
606 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000607 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000608 offset = SelectExpr(N.getOperand(1));
609 return true;
610 }
611 Reg = SelectExpr(N);
612 offset = 0;
613 return false;
614}
615
616void ISel::SelectBranchCC(SDOperand N)
617{
618 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000619 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000620 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
621
622 // Get the MBB we will fall through to so that we can hand it off to the
623 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
624 //ilist<MachineBasicBlock>::iterator It = BB;
625 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000626
Nate Begemand3e6b942005-04-05 08:51:15 +0000627 Select(N.getOperand(0)); //chain
628 unsigned Opc = SelectSetCR0(N.getOperand(1));
629 // FIXME: Use this once we have something approximating two-way branches
630 // We cannot currently use this in case the ISel hands us something like
631 // BRcc MBBx
632 // BR MBBy
633 // since the fallthrough basic block for the conditional branch does not start
634 // with the unconditional branch (it is skipped over).
635 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
636 // .addMBB(Dest).addMBB(Fallthrough);
637 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
638 return;
639}
640
641unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
642{
643 unsigned Tmp1, Tmp2, Tmp3;
644 unsigned Opc = 0;
645 SDNode *Node = N.Val;
646 MVT::ValueType DestType = N.getValueType();
647 unsigned opcode = N.getOpcode();
648
649 switch (opcode) {
650 default:
651 Node->dump();
652 assert(0 && "Node not handled!\n");
653
654 case ISD::SELECT: {
655 // Attempt to generate FSEL. We can do this whenever we have an FP result,
656 // and an FP comparison in the SetCC node.
657 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
658 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
659 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
660 SetCC->getCondition() != ISD::SETEQ &&
661 SetCC->getCondition() != ISD::SETNE) {
662 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
663 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
664 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
665 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000666
Nate Begemand3e6b942005-04-05 08:51:15 +0000667 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
668 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
669 switch(SetCC->getCondition()) {
670 default: assert(0 && "Invalid FSEL condition"); abort();
671 case ISD::SETULT:
672 case ISD::SETLT:
673 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
674 return Result;
675 case ISD::SETUGE:
676 case ISD::SETGE:
677 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
678 return Result;
679 case ISD::SETUGT:
680 case ISD::SETGT: {
681 Tmp2 = MakeReg(VT);
682 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
683 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
684 return Result;
685 }
686 case ISD::SETULE:
687 case ISD::SETLE: {
688 Tmp2 = MakeReg(VT);
689 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
690 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
691 return Result;
692 }
693 }
694 } else {
695 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
696 Tmp2 = SelectExpr(SetCC->getOperand(1));
697 Tmp3 = MakeReg(VT);
698 switch(SetCC->getCondition()) {
699 default: assert(0 && "Invalid FSEL condition"); abort();
700 case ISD::SETULT:
701 case ISD::SETLT:
702 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
703 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
704 return Result;
705 case ISD::SETUGE:
706 case ISD::SETGE:
707 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
708 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
709 return Result;
710 case ISD::SETUGT:
711 case ISD::SETGT:
712 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
713 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
714 return Result;
715 case ISD::SETULE:
716 case ISD::SETLE:
717 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
718 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
719 return Result;
720 }
721 }
722 assert(0 && "Should never get here");
723 return 0;
724 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000725
Nate Begemand3e6b942005-04-05 08:51:15 +0000726 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
727 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
728 Opc = SelectSetCR0(N.getOperand(0));
729
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000730 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000731 // value and the MBB to hold the PHI instruction for this SetCC.
732 MachineBasicBlock *thisMBB = BB;
733 const BasicBlock *LLVM_BB = BB->getBasicBlock();
734 ilist<MachineBasicBlock>::iterator It = BB;
735 ++It;
736
737 // thisMBB:
738 // ...
739 // TrueVal = ...
740 // cmpTY cr0, r1, r2
741 // bCC copy1MBB
742 // fallthrough --> copy0MBB
743 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
744 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
745 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
746 MachineFunction *F = BB->getParent();
747 F->getBasicBlockList().insert(It, copy0MBB);
748 F->getBasicBlockList().insert(It, sinkMBB);
749 // Update machine-CFG edges
750 BB->addSuccessor(copy0MBB);
751 BB->addSuccessor(sinkMBB);
752
753 // copy0MBB:
754 // %FalseValue = ...
755 // # fallthrough to sinkMBB
756 BB = copy0MBB;
757 // Update machine-CFG edges
758 BB->addSuccessor(sinkMBB);
759
760 // sinkMBB:
761 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
762 // ...
763 BB = sinkMBB;
764 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
765 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
766 return Result;
767 }
768
769 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000770 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000771 ISD::ADD == N.getOperand(0).getOpcode() &&
772 N.getOperand(0).Val->hasOneUse() &&
773 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
774 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
775 ++FusedFP; // Statistic
776 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
777 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
778 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
779 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
780 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000781 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000782 ISD::SUB == N.getOperand(0).getOpcode() &&
783 N.getOperand(0).Val->hasOneUse() &&
784 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
785 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
786 ++FusedFP; // Statistic
787 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
788 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
789 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
790 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
791 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
792 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
793 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
794 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
795 } else {
796 Tmp1 = SelectExpr(N.getOperand(0));
797 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
798 }
799 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000800
Nate Begemand3e6b942005-04-05 08:51:15 +0000801 case ISD::FABS:
802 Tmp1 = SelectExpr(N.getOperand(0));
803 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
804 return Result;
805
806 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000807 assert (DestType == MVT::f32 &&
808 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000809 "only f64 to f32 conversion supported here");
810 Tmp1 = SelectExpr(N.getOperand(0));
811 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
812 return Result;
813
814 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000815 assert (DestType == MVT::f64 &&
816 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000817 "only f32 to f64 conversion supported here");
818 Tmp1 = SelectExpr(N.getOperand(0));
819 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
820 return Result;
821
822 case ISD::CopyFromReg:
823 if (Result == 1)
824 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
825 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
826 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
827 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000828
Nate Begemand3e6b942005-04-05 08:51:15 +0000829 case ISD::ConstantFP: {
830 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
831 Result = getConstDouble(CN->getValue(), Result);
832 return Result;
833 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000834
Nate Begemand3e6b942005-04-05 08:51:15 +0000835 case ISD::ADD:
836 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
837 N.getOperand(0).Val->hasOneUse()) {
838 ++FusedFP; // Statistic
839 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
840 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
841 Tmp3 = SelectExpr(N.getOperand(1));
842 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
843 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
844 return Result;
845 }
846 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
847 Tmp1 = SelectExpr(N.getOperand(0));
848 Tmp2 = SelectExpr(N.getOperand(1));
849 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
850 return Result;
851
852 case ISD::SUB:
853 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
854 N.getOperand(0).Val->hasOneUse()) {
855 ++FusedFP; // Statistic
856 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
857 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
858 Tmp3 = SelectExpr(N.getOperand(1));
859 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
860 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
861 return Result;
862 }
863 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
864 Tmp1 = SelectExpr(N.getOperand(0));
865 Tmp2 = SelectExpr(N.getOperand(1));
866 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
867 return Result;
868
869 case ISD::MUL:
870 case ISD::SDIV:
871 switch( opcode ) {
872 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
873 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
874 };
875 Tmp1 = SelectExpr(N.getOperand(0));
876 Tmp2 = SelectExpr(N.getOperand(1));
877 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
878 return Result;
879
880 case ISD::UINT_TO_FP:
881 case ISD::SINT_TO_FP: {
882 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
883 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
884 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
885 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
886 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000887
Nate Begemand3e6b942005-04-05 08:51:15 +0000888 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
889 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000890
Nate Begemand3e6b942005-04-05 08:51:15 +0000891 // FIXME: pull this FP constant generation stuff out into something like
892 // the simple ISel's getReg.
893 if (IsUnsigned) {
894 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
895 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
896 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
897 } else {
898 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
899 unsigned CPI = CP->getConstantPoolIndex(CFP);
900 // Load constant fp value
901 unsigned Tmp4 = MakeReg(MVT::i32);
902 unsigned TmpL = MakeReg(MVT::i32);
903 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
904 .addConstantPoolIndex(CPI);
905 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
906 // Store the hi & low halves of the fp value, currently in int regs
907 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
908 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
909 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
910 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
911 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
912 // Generate the return value with a subtract
913 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
914 }
915 return Result;
916 }
917 }
918 assert(0 && "Should never get here");
919 return 0;
920}
921
922unsigned ISel::SelectExpr(SDOperand N) {
923 unsigned Result;
924 unsigned Tmp1, Tmp2, Tmp3;
925 unsigned Opc = 0;
926 unsigned opcode = N.getOpcode();
927
928 SDNode *Node = N.Val;
929 MVT::ValueType DestType = N.getValueType();
930
931 unsigned &Reg = ExprMap[N];
932 if (Reg) return Reg;
933
934 switch (N.getOpcode()) {
935 default:
936 Reg = Result = (N.getValueType() != MVT::Other) ?
937 MakeReg(N.getValueType()) : 1;
938 break;
939 case ISD::CALL:
940 // If this is a call instruction, make sure to prepare ALL of the result
941 // values as well as the chain.
942 if (Node->getNumValues() == 1)
943 Reg = Result = 1; // Void call, just a chain.
944 else {
945 Result = MakeReg(Node->getValueType(0));
946 ExprMap[N.getValue(0)] = Result;
947 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
948 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
949 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
950 }
951 break;
952 }
953
954 if (ISD::CopyFromReg == opcode)
955 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000956
Nate Begemand3e6b942005-04-05 08:51:15 +0000957 if (DestType == MVT::f64 || DestType == MVT::f32)
958 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
959 return SelectExprFP(N, Result);
960
961 switch (opcode) {
962 default:
963 Node->dump();
964 assert(0 && "Node not handled!\n");
965 case ISD::UNDEF:
966 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
967 return Result;
968 case ISD::DYNAMIC_STACKALLOC:
969 // Generate both result values. FIXME: Need a better commment here?
970 if (Result != 1)
971 ExprMap[N.getValue(1)] = 1;
972 else
973 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
974
975 // FIXME: We are currently ignoring the requested alignment for handling
976 // greater than the stack alignment. This will need to be revisited at some
977 // point. Align = N.getOperand(2);
978 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
979 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
980 std::cerr << "Cannot allocate stack object with greater alignment than"
981 << " the stack alignment yet!";
982 abort();
983 }
984 Select(N.getOperand(0));
985 Tmp1 = SelectExpr(N.getOperand(1));
986 // Subtract size from stack pointer, thereby allocating some space.
987 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
988 // Put a pointer to the space into the result register by copying the SP
989 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
990 return Result;
991
992 case ISD::ConstantPool:
993 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
994 Tmp2 = MakeReg(MVT::i64);
995 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
996 .addConstantPoolIndex(Tmp1);
997 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
998 return Result;
999
1000 case ISD::FrameIndex:
1001 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1002 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1003 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001004
Nate Begemand3e6b942005-04-05 08:51:15 +00001005 case ISD::GlobalAddress: {
1006 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1007 Tmp1 = MakeReg(MVT::i64);
1008 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1009 .addGlobalAddress(GV);
1010 if (GV->hasWeakLinkage() || GV->isExternal()) {
Nate Begemana9532d52005-04-08 23:45:01 +00001011 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001012 } else {
1013 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1014 }
1015 return Result;
1016 }
1017
1018 case ISD::LOAD:
1019 case ISD::EXTLOAD:
1020 case ISD::ZEXTLOAD:
1021 case ISD::SEXTLOAD: {
1022 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1023 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1024 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001025
Nate Begemand3e6b942005-04-05 08:51:15 +00001026 // Make sure we generate both values.
1027 if (Result != 1)
1028 ExprMap[N.getValue(1)] = 1; // Generate the token
1029 else
1030 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1031
1032 SDOperand Chain = N.getOperand(0);
1033 SDOperand Address = N.getOperand(1);
1034 Select(Chain);
1035
1036 switch (TypeBeingLoaded) {
1037 default: Node->dump(); assert(0 && "Cannot load this type!");
1038 case MVT::i1: Opc = PPC::LBZ; break;
1039 case MVT::i8: Opc = PPC::LBZ; break;
1040 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1041 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1042 case MVT::i64: Opc = PPC::LD; break;
1043 case MVT::f32: Opc = PPC::LFS; break;
1044 case MVT::f64: Opc = PPC::LFD; break;
1045 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001046
Nate Begemand3e6b942005-04-05 08:51:15 +00001047 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1048 Tmp1 = MakeReg(MVT::i64);
1049 int CPI = CP->getIndex();
1050 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1051 .addConstantPoolIndex(CPI);
1052 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1053 }
1054 else if(Address.getOpcode() == ISD::FrameIndex) {
1055 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1056 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1057 } else {
1058 int offset;
1059 bool idx = SelectAddr(Address, Tmp1, offset);
1060 if (idx) {
1061 Opc = IndexedOpForOp(Opc);
1062 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1063 } else {
1064 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1065 }
1066 }
1067 return Result;
1068 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001069
Nate Begemand3e6b942005-04-05 08:51:15 +00001070 case ISD::CALL: {
1071 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001072 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001073 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1074 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1075 };
1076 static const unsigned FPR[] = {
1077 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1078 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1079 };
1080
1081 // Lower the chain for this call.
1082 Select(N.getOperand(0));
1083 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1084
1085 MachineInstr *CallMI;
1086 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001087 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001088 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001089 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001090 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001091 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001092 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001093 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001094 true);
1095 } else {
1096 Tmp1 = SelectExpr(N.getOperand(1));
1097 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1098 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1099 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1100 .addReg(PPC::R12);
1101 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001102
Nate Begemand3e6b942005-04-05 08:51:15 +00001103 // Load the register args to virtual regs
1104 std::vector<unsigned> ArgVR;
1105 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1106 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1107
1108 // Copy the virtual registers into the appropriate argument register
1109 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1110 switch(N.getOperand(i+2).getValueType()) {
1111 default: Node->dump(); assert(0 && "Unknown value type for call");
1112 case MVT::i1:
1113 case MVT::i8:
1114 case MVT::i16:
1115 case MVT::i32:
1116 case MVT::i64:
1117 assert(GPR_idx < 8 && "Too many int args");
1118 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1119 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1120 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1121 }
1122 ++GPR_idx;
1123 break;
1124 case MVT::f64:
1125 case MVT::f32:
1126 assert(FPR_idx < 13 && "Too many fp args");
1127 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1128 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1129 ++FPR_idx;
1130 break;
1131 }
1132 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001133
Nate Begemand3e6b942005-04-05 08:51:15 +00001134 // Put the call instruction in the correct place in the MachineBasicBlock
1135 BB->push_back(CallMI);
1136
1137 switch (Node->getValueType(0)) {
1138 default: assert(0 && "Unknown value type for call result!");
1139 case MVT::Other: return 1;
1140 case MVT::i1:
1141 case MVT::i8:
1142 case MVT::i16:
1143 case MVT::i32:
1144 case MVT::i64:
1145 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1146 break;
1147 case MVT::f32:
1148 case MVT::f64:
1149 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1150 break;
1151 }
1152 return Result+N.ResNo;
1153 }
1154
1155 case ISD::SIGN_EXTEND:
1156 case ISD::SIGN_EXTEND_INREG:
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1159 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1160 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001161 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001162 break;
1163 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001164 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001165 break;
1166 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001167 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001168 break;
1169 case MVT::i1:
1170 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1171 break;
1172 }
1173 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001174
Nate Begemand3e6b942005-04-05 08:51:15 +00001175 case ISD::CopyFromReg:
1176 if (Result == 1)
1177 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1178 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1179 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1180 return Result;
1181
1182 case ISD::SHL:
1183 Tmp1 = SelectExpr(N.getOperand(0));
1184 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001185 Tmp2 = CN->getValue() & 0x3F;
1186 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1187 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001188 } else {
1189 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001190 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001191 }
1192 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001193
Nate Begemand3e6b942005-04-05 08:51:15 +00001194 case ISD::SRL:
1195 Tmp1 = SelectExpr(N.getOperand(0));
1196 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001197 Tmp2 = CN->getValue() & 0x3F;
1198 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1199 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001200 } else {
1201 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001202 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001203 }
1204 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001205
Nate Begemand3e6b942005-04-05 08:51:15 +00001206 case ISD::SRA:
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001209 Tmp2 = CN->getValue() & 0x3F;
1210 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001211 } else {
1212 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001213 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001214 }
1215 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001216
Nate Begemand3e6b942005-04-05 08:51:15 +00001217 case ISD::ADD:
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1220 default: assert(0 && "unhandled result code");
1221 case 0: // No immediate
1222 Tmp2 = SelectExpr(N.getOperand(1));
1223 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1224 break;
1225 case 1: // Low immediate
1226 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1227 break;
1228 case 2: // Shifted immediate
1229 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1230 break;
1231 }
1232 return Result;
1233
1234 case ISD::AND:
1235 case ISD::OR:
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1238 default: assert(0 && "unhandled result code");
1239 case 0: // No immediate
1240 Tmp2 = SelectExpr(N.getOperand(1));
1241 switch (opcode) {
1242 case ISD::AND: Opc = PPC::AND; break;
1243 case ISD::OR: Opc = PPC::OR; break;
1244 }
1245 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 break;
1247 case 1: // Low immediate
1248 switch (opcode) {
1249 case ISD::AND: Opc = PPC::ANDIo; break;
1250 case ISD::OR: Opc = PPC::ORI; break;
1251 }
1252 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1253 break;
1254 case 2: // Shifted immediate
1255 switch (opcode) {
1256 case ISD::AND: Opc = PPC::ANDISo; break;
1257 case ISD::OR: Opc = PPC::ORIS; break;
1258 }
1259 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1260 break;
1261 }
1262 return Result;
1263
1264 case ISD::XOR: {
1265 // Check for EQV: xor, (xor a, -1), b
1266 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1267 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1268 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1269 ++NotLogic;
1270 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1271 Tmp2 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1273 return Result;
1274 }
1275 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1276 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1277 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1278 ++NotLogic;
1279 switch(N.getOperand(0).getOpcode()) {
1280 case ISD::OR:
1281 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1282 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1283 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 break;
1285 case ISD::AND:
1286 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1288 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1289 break;
1290 default:
1291 Tmp1 = SelectExpr(N.getOperand(0));
1292 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1293 break;
1294 }
1295 return Result;
1296 }
1297 Tmp1 = SelectExpr(N.getOperand(0));
1298 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1299 default: assert(0 && "unhandled result code");
1300 case 0: // No immediate
1301 Tmp2 = SelectExpr(N.getOperand(1));
1302 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1303 break;
1304 case 1: // Low immediate
1305 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1306 break;
1307 case 2: // Shifted immediate
1308 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1309 break;
1310 }
1311 return Result;
1312 }
1313
1314 case ISD::SUB:
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1317 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1318 else {
1319 Tmp1 = SelectExpr(N.getOperand(0));
1320 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1321 }
1322 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001323
Nate Begemand3e6b942005-04-05 08:51:15 +00001324 case ISD::MUL:
1325 Tmp1 = SelectExpr(N.getOperand(0));
1326 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1327 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1328 else {
1329 Tmp2 = SelectExpr(N.getOperand(1));
1330 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1331 }
1332 return Result;
1333
1334 case ISD::SDIV:
1335 case ISD::UDIV:
1336 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1337 Tmp1 = MakeReg(MVT::i64);
1338 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001339 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001340 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1341 return Result;
1342 }
1343 Tmp1 = SelectExpr(N.getOperand(0));
1344 Tmp2 = SelectExpr(N.getOperand(1));
1345 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1346 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1347 return Result;
1348
Nate Begemand3e6b942005-04-05 08:51:15 +00001349 case ISD::FP_TO_UINT:
1350 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001351 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001352 Tmp2 = MakeReg(MVT::f64);
1353 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1354 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1355 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1356 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1357 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001358 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001359
Nate Begemand3e6b942005-04-05 08:51:15 +00001360 case ISD::SETCC:
1361 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1362 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001363
Nate Begemand3e6b942005-04-05 08:51:15 +00001364 unsigned TrueValue = MakeReg(MVT::i32);
1365 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1366 unsigned FalseValue = MakeReg(MVT::i32);
1367 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1368
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001369 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001370 // value and the MBB to hold the PHI instruction for this SetCC.
1371 MachineBasicBlock *thisMBB = BB;
1372 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1373 ilist<MachineBasicBlock>::iterator It = BB;
1374 ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001375
Nate Begemand3e6b942005-04-05 08:51:15 +00001376 // thisMBB:
1377 // ...
1378 // cmpTY cr0, r1, r2
1379 // %TrueValue = li 1
1380 // bCC sinkMBB
1381 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1382 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1383 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1384 MachineFunction *F = BB->getParent();
1385 F->getBasicBlockList().insert(It, copy0MBB);
1386 F->getBasicBlockList().insert(It, sinkMBB);
1387 // Update machine-CFG edges
1388 BB->addSuccessor(copy0MBB);
1389 BB->addSuccessor(sinkMBB);
1390
1391 // copy0MBB:
1392 // %FalseValue = li 0
1393 // fallthrough
1394 BB = copy0MBB;
1395 // Update machine-CFG edges
1396 BB->addSuccessor(sinkMBB);
1397
1398 // sinkMBB:
1399 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1400 // ...
1401 BB = sinkMBB;
1402 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1403 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1404 return Result;
1405 }
1406 assert(0 && "Is this legal?");
1407 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001408
Nate Begemand3e6b942005-04-05 08:51:15 +00001409 case ISD::SELECT: {
1410 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1411 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1412 Opc = SelectSetCR0(N.getOperand(0));
1413
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001414 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001415 // value and the MBB to hold the PHI instruction for this SetCC.
1416 MachineBasicBlock *thisMBB = BB;
1417 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1418 ilist<MachineBasicBlock>::iterator It = BB;
1419 ++It;
1420
1421 // thisMBB:
1422 // ...
1423 // TrueVal = ...
1424 // cmpTY cr0, r1, r2
1425 // bCC copy1MBB
1426 // fallthrough --> copy0MBB
1427 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1428 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1429 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1430 MachineFunction *F = BB->getParent();
1431 F->getBasicBlockList().insert(It, copy0MBB);
1432 F->getBasicBlockList().insert(It, sinkMBB);
1433 // Update machine-CFG edges
1434 BB->addSuccessor(copy0MBB);
1435 BB->addSuccessor(sinkMBB);
1436
1437 // copy0MBB:
1438 // %FalseValue = ...
1439 // # fallthrough to sinkMBB
1440 BB = copy0MBB;
1441 // Update machine-CFG edges
1442 BB->addSuccessor(sinkMBB);
1443
1444 // sinkMBB:
1445 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1446 // ...
1447 BB = sinkMBB;
1448 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1449 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1450
1451 // FIXME: Select i64?
1452 return Result;
1453 }
1454
1455 case ISD::Constant:
1456 switch (N.getValueType()) {
1457 default: assert(0 && "Cannot use constants of this type!");
1458 case MVT::i1:
1459 BuildMI(BB, PPC::LI, 1, Result)
1460 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1461 break;
1462 case MVT::i32:
1463 {
1464 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1465 if (v < 32768 && v >= -32768) {
1466 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1467 } else {
1468 Tmp1 = MakeReg(MVT::i32);
1469 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1470 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1471 }
1472 }
1473 }
1474 return Result;
1475 }
1476
1477 return 0;
1478}
1479
1480void ISel::Select(SDOperand N) {
1481 unsigned Tmp1, Tmp2, Opc;
1482 unsigned opcode = N.getOpcode();
1483
1484 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1485 return; // Already selected.
1486
1487 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001488
Nate Begemand3e6b942005-04-05 08:51:15 +00001489 switch (Node->getOpcode()) {
1490 default:
1491 Node->dump(); std::cerr << "\n";
1492 assert(0 && "Node not handled yet!");
1493 case ISD::EntryToken: return; // Noop
1494 case ISD::TokenFactor:
1495 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1496 Select(Node->getOperand(i));
1497 return;
1498 case ISD::ADJCALLSTACKDOWN:
1499 case ISD::ADJCALLSTACKUP:
1500 Select(N.getOperand(0));
1501 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1502 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1503 PPC::ADJCALLSTACKUP;
1504 BuildMI(BB, Opc, 1).addImm(Tmp1);
1505 return;
1506 case ISD::BR: {
1507 MachineBasicBlock *Dest =
1508 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1509 Select(N.getOperand(0));
1510 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1511 return;
1512 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001513 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001514 SelectBranchCC(N);
1515 return;
1516 case ISD::CopyToReg:
1517 Select(N.getOperand(0));
1518 Tmp1 = SelectExpr(N.getOperand(1));
1519 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001520
Nate Begemand3e6b942005-04-05 08:51:15 +00001521 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001522 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001523 N.getOperand(1).getValueType() == MVT::f32)
1524 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1525 else
1526 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1527 }
1528 return;
1529 case ISD::ImplicitDef:
1530 Select(N.getOperand(0));
1531 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1532 return;
1533 case ISD::RET:
1534 switch (N.getNumOperands()) {
1535 default:
1536 assert(0 && "Unknown return instruction!");
1537 case 3:
1538 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1539 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001540 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001541 Select(N.getOperand(0));
1542 Tmp1 = SelectExpr(N.getOperand(1));
1543 Tmp2 = SelectExpr(N.getOperand(2));
1544 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1545 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1546 break;
1547 case 2:
1548 Select(N.getOperand(0));
1549 Tmp1 = SelectExpr(N.getOperand(1));
1550 switch (N.getOperand(1).getValueType()) {
1551 default:
1552 assert(0 && "Unknown return type!");
1553 case MVT::f64:
1554 case MVT::f32:
1555 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1556 break;
1557 case MVT::i32:
1558 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1559 break;
1560 }
1561 case 1:
1562 Select(N.getOperand(0));
1563 break;
1564 }
1565 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1566 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001567 case ISD::TRUNCSTORE:
1568 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001569 {
1570 SDOperand Chain = N.getOperand(0);
1571 SDOperand Value = N.getOperand(1);
1572 SDOperand Address = N.getOperand(2);
1573 Select(Chain);
1574
1575 Tmp1 = SelectExpr(Value); //value
1576
1577 if (opcode == ISD::STORE) {
1578 switch(Value.getValueType()) {
1579 default: assert(0 && "unknown Type in store");
1580 case MVT::i64: Opc = PPC::STD; break;
1581 case MVT::f64: Opc = PPC::STFD; break;
1582 case MVT::f32: Opc = PPC::STFS; break;
1583 }
1584 } else { //ISD::TRUNCSTORE
1585 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1586 default: assert(0 && "unknown Type in store");
1587 case MVT::i1: //FIXME: DAG does not promote this load
1588 case MVT::i8: Opc= PPC::STB; break;
1589 case MVT::i16: Opc = PPC::STH; break;
1590 case MVT::i32: Opc = PPC::STW; break;
1591 }
1592 }
1593
1594 if(Address.getOpcode() == ISD::FrameIndex)
1595 {
1596 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1597 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1598 }
1599 else
1600 {
1601 int offset;
1602 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001603 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001604 Opc = IndexedOpForOp(Opc);
1605 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1606 } else {
1607 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1608 }
1609 }
1610 return;
1611 }
1612 case ISD::EXTLOAD:
1613 case ISD::SEXTLOAD:
1614 case ISD::ZEXTLOAD:
1615 case ISD::LOAD:
1616 case ISD::CopyFromReg:
1617 case ISD::CALL:
1618 case ISD::DYNAMIC_STACKALLOC:
1619 ExprMap.erase(N);
1620 SelectExpr(N);
1621 return;
1622 }
1623 assert(0 && "Should not be reached!");
1624}
1625
1626
1627/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1628/// into a machine code representation using pattern matching and a machine
1629/// description file.
1630///
1631FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001632 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001633}
1634