blob: 8614a24743ce0326f3522e3bb9f55c8ff4298eee [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) {
44 // Set up the register classes.
45 addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
46 addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
47 addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000048
Nate Begemand3e6b942005-04-05 08:51:15 +000049 // PowerPC has no intrinsics for these particular operations
Chris Lattner644db4e2005-04-09 03:22:30 +000050 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Nate Begemand3e6b942005-04-05 08:51:15 +000051 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
52 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
53 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
54
Chris Lattner08cae7f2005-04-30 04:26:56 +000055 // We don't support sin/cos/sqrt
56 setOperationAction(ISD::FSIN , MVT::f64, Expand);
57 setOperationAction(ISD::FCOS , MVT::f64, Expand);
58 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
59 setOperationAction(ISD::FSIN , MVT::f32, Expand);
60 setOperationAction(ISD::FCOS , MVT::f32, Expand);
61 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
62
Nate Begemand3e6b942005-04-05 08:51:15 +000063 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
64 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
65 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
66
Nate Begemane88aa5b2005-04-09 03:05:51 +000067 // PowerPC has no SREM/UREM instructions
68 setOperationAction(ISD::SREM, MVT::i64, Expand);
69 setOperationAction(ISD::UREM, MVT::i64, Expand);
70
Nate Begemand3e6b942005-04-05 08:51:15 +000071 setShiftAmountFlavor(Extend); // shl X, 32 == 0
72 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000073 addLegalFPImmediate(-0.0); //
Nate Begemand3e6b942005-04-05 08:51:15 +000074
75 computeRegisterProperties();
76 }
77
78 /// LowerArguments - This hook must be implemented to indicate how we should
79 /// lower the arguments for the specified function, into the specified DAG.
80 virtual std::vector<SDOperand>
81 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000082
Nate Begemand3e6b942005-04-05 08:51:15 +000083 /// LowerCallTo - This hook lowers an abstract call to a function into an
84 /// actual call.
85 virtual std::pair<SDOperand, SDOperand>
86 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
87 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000088
Nate Begemand3e6b942005-04-05 08:51:15 +000089 virtual std::pair<SDOperand, SDOperand>
90 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000091
Nate Begemand3e6b942005-04-05 08:51:15 +000092 virtual std::pair<SDOperand,SDOperand>
93 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
94 const Type *ArgTy, SelectionDAG &DAG);
95
96 virtual std::pair<SDOperand, SDOperand>
97 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
98 SelectionDAG &DAG);
99 };
100}
101
102
103std::vector<SDOperand>
104PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
105 //
106 // add beautiful description of PPC stack frame format, or at least some docs
107 //
108 MachineFunction &MF = DAG.getMachineFunction();
109 MachineFrameInfo *MFI = MF.getFrameInfo();
110 MachineBasicBlock& BB = MF.front();
111 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000112
113 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemand3e6b942005-04-05 08:51:15 +0000114 // fixed size array of physical args, for the sake of simplicity let the STL
115 // handle tracking them for us.
116 std::vector<unsigned> argVR, argPR, argOp;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000117 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000118 unsigned GPR_remaining = 8;
119 unsigned FPR_remaining = 13;
120 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000121 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +0000122 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
123 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
124 };
125 static const unsigned FPR[] = {
126 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
127 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
128 };
129
130 // Add DAG nodes to load the arguments... On entry to a function on PPC,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000131 // the arguments start at offset 48, although they are likely to be passed
Nate Begemand3e6b942005-04-05 08:51:15 +0000132 // in registers.
133 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
134 SDOperand newroot, argt;
Nate Begemand3e6b942005-04-05 08:51:15 +0000135 bool needsLoad = false;
136 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000137
Nate Begemand3e6b942005-04-05 08:51:15 +0000138 switch (ObjectVT) {
139 default: assert(0 && "Unhandled argument type!");
140 case MVT::i1:
141 case MVT::i8:
142 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000143 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000144 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000145 if (GPR_remaining > 0) {
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
147 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
148 DAG.getRoot());
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000149 if (ObjectVT != MVT::i64)
Nate Begemand3e6b942005-04-05 08:51:15 +0000150 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
151 } else {
152 needsLoad = true;
153 }
154 break;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000155 case MVT::f32:
156 case MVT::f64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000157 if (FPR_remaining > 0) {
158 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000159 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemand3e6b942005-04-05 08:51:15 +0000160 DAG.getRoot());
161 --FPR_remaining;
162 ++FPR_idx;
163 } else {
164 needsLoad = true;
165 }
166 break;
167 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000168
Nate Begemand3e6b942005-04-05 08:51:15 +0000169 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000170 // that we ran out of physical registers of the appropriate type
Nate Begemand3e6b942005-04-05 08:51:15 +0000171 if (needsLoad) {
172 unsigned SubregOffset = 0;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000173 switch (ObjectVT) {
174 default: assert(0 && "Unhandled argument type!");
175 case MVT::i1:
176 case MVT::i8: SubregOffset = 7; break;
177 case MVT::i16: SubregOffset = 6; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000178 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000179 case MVT::f32: SubregOffset = 4; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000180 case MVT::i64:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000181 case MVT::f64: SubregOffset = 0; break;
182 }
183 int FI = MFI->CreateFixedObject(8, ArgOffset);
184 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000185 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000186 DAG.getConstant(SubregOffset, MVT::i64));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000187 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000188 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000189
Nate Begemand3e6b942005-04-05 08:51:15 +0000190 // Every 4 bytes of argument space consumes one of the GPRs available for
191 // argument passing.
192 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000193 --GPR_remaining;
194 ++GPR_idx;
Nate Begemand3e6b942005-04-05 08:51:15 +0000195 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000196 ArgOffset += 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000197
Nate Begemand3e6b942005-04-05 08:51:15 +0000198 DAG.setRoot(newroot.getValue(1));
199 ArgValues.push_back(argt);
200 }
201
202 // If the function takes variable number of arguments, make a frame index for
203 // the start of the first vararg value... for expansion of llvm.va_start.
204 if (F.isVarArg()) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000205 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
206 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000207 // If this function is vararg, store any remaining integer argument regs
208 // to their spots on the stack so that they may be loaded by deferencing the
209 // result of va_next.
210 std::vector<SDOperand> MemOps;
211 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
212 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000213 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000214 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000215 Val, FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000216 MemOps.push_back(Store);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000217 // Increment the address by eight for the next argument to store
218 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
Nate Begemand3e6b942005-04-05 08:51:15 +0000219 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
220 }
221 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
222 }
223
224 return ArgValues;
225}
226
227std::pair<SDOperand, SDOperand>
228PPC64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000229 const Type *RetTy, bool isVarArg,
230 SDOperand Callee, ArgListTy &Args,
231 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000232 // args_to_use will accumulate outgoing args for the ISD::CALL case in
233 // SelectExpr to use to put the arguments in the appropriate registers.
234 std::vector<SDOperand> args_to_use;
235
236 // Count how many bytes are to be pushed on the stack, including the linkage
237 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000238 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000239
240 if (Args.empty()) {
241 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
242 DAG.getConstant(NumBytes, getPointerTy()));
243 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000244 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000245
246 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000247 // plus 64 bytes of argument space in case any called code gets funky on us.
248 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000249
250 // Adjust the stack pointer for the new arguments...
251 // These operations are automatically eliminated by the prolog/epilog pass
252 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
253 DAG.getConstant(NumBytes, getPointerTy()));
254
255 // Set up a copy of the stack pointer for use loading and storing any
256 // arguments that may not fit in the registers available for argument
257 // passing.
258 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
259 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000260
Nate Begemand3e6b942005-04-05 08:51:15 +0000261 // Figure out which arguments are going to go in registers, and which in
262 // memory. Also, if this is a vararg function, floating point operations
263 // must be stored to our stack, and loaded into integer regs as well, if
264 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000265 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000266 unsigned GPR_remaining = 8;
267 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000268
Nate Begemand3e6b942005-04-05 08:51:15 +0000269 std::vector<SDOperand> MemOps;
270 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
271 // PtrOff will be used to store the current argument to the stack if a
272 // register cannot be found for it.
273 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
274 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
275 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000276
Nate Begemand3e6b942005-04-05 08:51:15 +0000277 switch (ArgVT) {
278 default: assert(0 && "Unexpected ValueType for argument!");
279 case MVT::i1:
280 case MVT::i8:
281 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000282 case MVT::i32:
283 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000284 // sign extend, otherwise use a zero extend.
285 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000286 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000287 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000288 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000289 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000290 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000291 if (GPR_remaining > 0) {
292 args_to_use.push_back(Args[i].first);
293 --GPR_remaining;
294 } else {
295 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000296 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000297 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000298 ArgOffset += 8;
299 break;
300 case MVT::f32:
301 case MVT::f64:
302 if (FPR_remaining > 0) {
303 args_to_use.push_back(Args[i].first);
304 --FPR_remaining;
305 if (isVarArg) {
306 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000307 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000308 MemOps.push_back(Store);
309 // Float varargs are always shadowed in available integer registers
310 if (GPR_remaining > 0) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000311 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000312 MemOps.push_back(Load);
313 args_to_use.push_back(Load);
314 --GPR_remaining;
315 }
316 } else {
317 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000318 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000319 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000320 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000321 --GPR_remaining;
322 }
323 }
324 } else {
325 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000326 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000327 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000328 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000329 break;
330 }
331 }
332 if (!MemOps.empty())
333 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
334 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000335
Nate Begemand3e6b942005-04-05 08:51:15 +0000336 std::vector<MVT::ValueType> RetVals;
337 MVT::ValueType RetTyVT = getValueType(RetTy);
338 if (RetTyVT != MVT::isVoid)
339 RetVals.push_back(RetTyVT);
340 RetVals.push_back(MVT::Other);
341
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000342 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000343 Chain, Callee, args_to_use), 0);
344 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
345 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
346 DAG.getConstant(NumBytes, getPointerTy()));
347 return std::make_pair(TheCall, Chain);
348}
349
350std::pair<SDOperand, SDOperand>
351PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
352 //vastart just returns the address of the VarArgsFrameIndex slot.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000353 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
Nate Begemand3e6b942005-04-05 08:51:15 +0000354}
355
356std::pair<SDOperand,SDOperand> PPC64TargetLowering::
357LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
358 const Type *ArgTy, SelectionDAG &DAG) {
359 MVT::ValueType ArgVT = getValueType(ArgTy);
360 SDOperand Result;
361 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000362 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000363 } else {
Nate Begemand3e6b942005-04-05 08:51:15 +0000364 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000365 DAG.getConstant(8, VAList.getValueType()));
Nate Begemand3e6b942005-04-05 08:51:15 +0000366 }
367 return std::make_pair(Result, Chain);
368}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000369
Nate Begemand3e6b942005-04-05 08:51:15 +0000370
371std::pair<SDOperand, SDOperand> PPC64TargetLowering::
372LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
373 SelectionDAG &DAG) {
374 assert(0 && "LowerFrameReturnAddress unimplemented");
375 abort();
376}
377
378namespace {
379Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
380Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
381//===--------------------------------------------------------------------===//
382/// ISel - PPC32 specific code to select PPC32 machine instructions for
383/// SelectionDAG operations.
384//===--------------------------------------------------------------------===//
385class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000386
Nate Begemand3e6b942005-04-05 08:51:15 +0000387 /// Comment Here.
388 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000389
Nate Begemand3e6b942005-04-05 08:51:15 +0000390 /// ExprMap - As shared expressions are codegen'd, we keep track of which
391 /// vreg the value is produced in, so we only emit one copy of each compiled
392 /// tree.
393 std::map<SDOperand, unsigned> ExprMap;
394
395 unsigned GlobalBaseReg;
396 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000397
Nate Begemand3e6b942005-04-05 08:51:15 +0000398public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000399 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000400 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000401
Nate Begemand3e6b942005-04-05 08:51:15 +0000402 /// runOnFunction - Override this function in order to reset our per-function
403 /// variables.
404 virtual bool runOnFunction(Function &Fn) {
405 // Make sure we re-emit a set of the global base reg if necessary
406 GlobalBaseInitialized = false;
407 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000408 }
409
Nate Begemand3e6b942005-04-05 08:51:15 +0000410 /// InstructionSelectBasicBlock - This callback is invoked by
411 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
412 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
413 DEBUG(BB->dump());
414 // Codegen the basic block.
415 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416
Nate Begemand3e6b942005-04-05 08:51:15 +0000417 // Clear state used for selection.
418 ExprMap.clear();
419 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000420
Nate Begemand3e6b942005-04-05 08:51:15 +0000421 unsigned getGlobalBaseReg();
422 unsigned getConstDouble(double floatVal, unsigned Result);
423 unsigned SelectSetCR0(SDOperand CC);
424 unsigned SelectExpr(SDOperand N);
425 unsigned SelectExprFP(SDOperand N, unsigned Result);
426 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000427
Nate Begemand3e6b942005-04-05 08:51:15 +0000428 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
429 void SelectBranchCC(SDOperand N);
430};
431
432/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
433/// returns zero when the input is not exactly a power of two.
434static unsigned ExactLog2(unsigned Val) {
435 if (Val == 0 || (Val & (Val-1))) return 0;
436 unsigned Count = 0;
437 while (Val != 1) {
438 Val >>= 1;
439 ++Count;
440 }
441 return Count;
442}
443
444/// getImmediateForOpcode - This method returns a value indicating whether
445/// the ConstantSDNode N can be used as an immediate to Opcode. The return
446/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000447/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000448/// of 1 indicates that the constant may be used in normal immediate form. A
449/// return value of 2 indicates that the constant may be used in shifted
450/// immediate form. A return value of 3 indicates that log base 2 of the
451/// constant may be used.
452///
453static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
454 unsigned& Imm, bool U = false) {
455 if (N.getOpcode() != ISD::Constant) return 0;
456
457 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458
Nate Begemand3e6b942005-04-05 08:51:15 +0000459 switch(Opcode) {
460 default: return 0;
461 case ISD::ADD:
462 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
463 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
464 break;
465 case ISD::AND:
466 case ISD::XOR:
467 case ISD::OR:
468 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
469 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
470 break;
471 case ISD::MUL:
472 case ISD::SUB:
473 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
474 break;
475 case ISD::SETCC:
476 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
477 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
478 break;
479 case ISD::SDIV:
480 if ((Imm = ExactLog2(v))) { return 3; }
481 break;
482 }
483 return 0;
484}
485
486/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
487/// to Condition. If the Condition is unordered or unsigned, the bool argument
488/// U is set to true, otherwise it is set to false.
489static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
490 U = false;
491 switch (Condition) {
492 default: assert(0 && "Unknown condition!"); abort();
493 case ISD::SETEQ: return PPC::BEQ;
494 case ISD::SETNE: return PPC::BNE;
495 case ISD::SETULT: U = true;
496 case ISD::SETLT: return PPC::BLT;
497 case ISD::SETULE: U = true;
498 case ISD::SETLE: return PPC::BLE;
499 case ISD::SETUGT: U = true;
500 case ISD::SETGT: return PPC::BGT;
501 case ISD::SETUGE: U = true;
502 case ISD::SETGE: return PPC::BGE;
503 }
504 return 0;
505}
506
507/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
508/// and store immediate instructions.
509static unsigned IndexedOpForOp(unsigned Opcode) {
510 switch(Opcode) {
511 default: assert(0 && "Unknown opcode!"); abort();
512 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
513 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
514 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
515 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
516 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
517 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
518 case PPC::LFD: return PPC::LFDX;
519 }
520 return 0;
521}
522}
523
524/// getGlobalBaseReg - Output the instructions required to put the
525/// base address to use for accessing globals into a register.
526///
527unsigned ISel::getGlobalBaseReg() {
528 if (!GlobalBaseInitialized) {
529 // Insert the set of GlobalBaseReg into the first MBB of the function
530 MachineBasicBlock &FirstMBB = BB->getParent()->front();
531 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
532 GlobalBaseReg = MakeReg(MVT::i64);
533 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
534 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
535 GlobalBaseInitialized = true;
536 }
537 return GlobalBaseReg;
538}
539
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000540/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000541/// Constant Pool. Optionally takes a register in which to load the value.
542unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000543 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000544 if (0 == Result) Result = MakeReg(MVT::f64);
545 MachineConstantPool *CP = BB->getParent()->getConstantPool();
546 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
547 unsigned CPI = CP->getConstantPoolIndex(CFP);
548 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
549 .addConstantPoolIndex(CPI);
550 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
551 return Result;
552}
553
554unsigned ISel::SelectSetCR0(SDOperand CC) {
555 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000556 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000557 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000558
Nate Begemand3e6b942005-04-05 08:51:15 +0000559 // If the first operand to the select is a SETCC node, then we can fold it
560 // into the branch that selects which value to return.
561 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
562 if (SetCC && CC.getOpcode() == ISD::SETCC) {
563 bool U;
564 Opc = getBCCForSetCC(SetCC->getCondition(), U);
565 Tmp1 = SelectExpr(SetCC->getOperand(0));
566
567 // Pass the optional argument U to getImmediateForOpcode for SETCC,
568 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000569 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000570 Tmp2, U)) {
571 if (U)
572 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
573 else
574 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
575 } else {
576 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
577 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
578 Tmp2 = SelectExpr(SetCC->getOperand(1));
579 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
580 }
581 } else {
582 Tmp1 = SelectExpr(CC);
583 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
584 Opc = PPC::BNE;
585 }
586 return Opc;
587}
588
589/// Check to see if the load is a constant offset from a base register
590bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
591{
592 unsigned imm = 0, opcode = N.getOpcode();
593 if (N.getOpcode() == ISD::ADD) {
594 Reg = SelectExpr(N.getOperand(0));
595 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
596 offset = imm;
597 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000598 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000599 offset = SelectExpr(N.getOperand(1));
600 return true;
601 }
602 Reg = SelectExpr(N);
603 offset = 0;
604 return false;
605}
606
607void ISel::SelectBranchCC(SDOperand N)
608{
609 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000610 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000611 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
612
613 // Get the MBB we will fall through to so that we can hand it off to the
614 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
615 //ilist<MachineBasicBlock>::iterator It = BB;
616 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000617
Nate Begemand3e6b942005-04-05 08:51:15 +0000618 Select(N.getOperand(0)); //chain
619 unsigned Opc = SelectSetCR0(N.getOperand(1));
620 // FIXME: Use this once we have something approximating two-way branches
621 // We cannot currently use this in case the ISel hands us something like
622 // BRcc MBBx
623 // BR MBBy
624 // since the fallthrough basic block for the conditional branch does not start
625 // with the unconditional branch (it is skipped over).
626 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
627 // .addMBB(Dest).addMBB(Fallthrough);
628 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
629 return;
630}
631
632unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
633{
634 unsigned Tmp1, Tmp2, Tmp3;
635 unsigned Opc = 0;
636 SDNode *Node = N.Val;
637 MVT::ValueType DestType = N.getValueType();
638 unsigned opcode = N.getOpcode();
639
640 switch (opcode) {
641 default:
642 Node->dump();
643 assert(0 && "Node not handled!\n");
644
645 case ISD::SELECT: {
646 // Attempt to generate FSEL. We can do this whenever we have an FP result,
647 // and an FP comparison in the SetCC node.
648 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
649 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
650 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
651 SetCC->getCondition() != ISD::SETEQ &&
652 SetCC->getCondition() != ISD::SETNE) {
653 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
654 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
655 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
656 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000657
Nate Begemand3e6b942005-04-05 08:51:15 +0000658 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
659 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
660 switch(SetCC->getCondition()) {
661 default: assert(0 && "Invalid FSEL condition"); abort();
662 case ISD::SETULT:
663 case ISD::SETLT:
664 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
665 return Result;
666 case ISD::SETUGE:
667 case ISD::SETGE:
668 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
669 return Result;
670 case ISD::SETUGT:
671 case ISD::SETGT: {
672 Tmp2 = MakeReg(VT);
673 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
674 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
675 return Result;
676 }
677 case ISD::SETULE:
678 case ISD::SETLE: {
679 Tmp2 = MakeReg(VT);
680 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
681 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
682 return Result;
683 }
684 }
685 } else {
686 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
687 Tmp2 = SelectExpr(SetCC->getOperand(1));
688 Tmp3 = MakeReg(VT);
689 switch(SetCC->getCondition()) {
690 default: assert(0 && "Invalid FSEL condition"); abort();
691 case ISD::SETULT:
692 case ISD::SETLT:
693 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
694 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
695 return Result;
696 case ISD::SETUGE:
697 case ISD::SETGE:
698 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
699 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
700 return Result;
701 case ISD::SETUGT:
702 case ISD::SETGT:
703 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
704 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
705 return Result;
706 case ISD::SETULE:
707 case ISD::SETLE:
708 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
709 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
710 return Result;
711 }
712 }
713 assert(0 && "Should never get here");
714 return 0;
715 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000716
Nate Begemand3e6b942005-04-05 08:51:15 +0000717 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
718 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
719 Opc = SelectSetCR0(N.getOperand(0));
720
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000721 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000722 // value and the MBB to hold the PHI instruction for this SetCC.
723 MachineBasicBlock *thisMBB = BB;
724 const BasicBlock *LLVM_BB = BB->getBasicBlock();
725 ilist<MachineBasicBlock>::iterator It = BB;
726 ++It;
727
728 // thisMBB:
729 // ...
730 // TrueVal = ...
731 // cmpTY cr0, r1, r2
732 // bCC copy1MBB
733 // fallthrough --> copy0MBB
734 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
735 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
736 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
737 MachineFunction *F = BB->getParent();
738 F->getBasicBlockList().insert(It, copy0MBB);
739 F->getBasicBlockList().insert(It, sinkMBB);
740 // Update machine-CFG edges
741 BB->addSuccessor(copy0MBB);
742 BB->addSuccessor(sinkMBB);
743
744 // copy0MBB:
745 // %FalseValue = ...
746 // # fallthrough to sinkMBB
747 BB = copy0MBB;
748 // Update machine-CFG edges
749 BB->addSuccessor(sinkMBB);
750
751 // sinkMBB:
752 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
753 // ...
754 BB = sinkMBB;
755 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
756 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
757 return Result;
758 }
759
760 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000761 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000762 ISD::ADD == N.getOperand(0).getOpcode() &&
763 N.getOperand(0).Val->hasOneUse() &&
764 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
765 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
766 ++FusedFP; // Statistic
767 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
768 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
769 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
770 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
771 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000772 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000773 ISD::SUB == N.getOperand(0).getOpcode() &&
774 N.getOperand(0).Val->hasOneUse() &&
775 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
776 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
777 ++FusedFP; // Statistic
778 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
779 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
780 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
781 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
782 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
783 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
784 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
785 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
786 } else {
787 Tmp1 = SelectExpr(N.getOperand(0));
788 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
789 }
790 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000791
Nate Begemand3e6b942005-04-05 08:51:15 +0000792 case ISD::FABS:
793 Tmp1 = SelectExpr(N.getOperand(0));
794 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
795 return Result;
796
797 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000798 assert (DestType == MVT::f32 &&
799 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000800 "only f64 to f32 conversion supported here");
801 Tmp1 = SelectExpr(N.getOperand(0));
802 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
803 return Result;
804
805 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000806 assert (DestType == MVT::f64 &&
807 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000808 "only f32 to f64 conversion supported here");
809 Tmp1 = SelectExpr(N.getOperand(0));
810 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
811 return Result;
812
813 case ISD::CopyFromReg:
814 if (Result == 1)
815 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
816 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
817 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
818 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000819
Nate Begemand3e6b942005-04-05 08:51:15 +0000820 case ISD::ConstantFP: {
821 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
822 Result = getConstDouble(CN->getValue(), Result);
823 return Result;
824 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000825
Nate Begemand3e6b942005-04-05 08:51:15 +0000826 case ISD::ADD:
827 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
828 N.getOperand(0).Val->hasOneUse()) {
829 ++FusedFP; // Statistic
830 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
831 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
832 Tmp3 = SelectExpr(N.getOperand(1));
833 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
834 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
835 return Result;
836 }
837 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
838 Tmp1 = SelectExpr(N.getOperand(0));
839 Tmp2 = SelectExpr(N.getOperand(1));
840 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
841 return Result;
842
843 case ISD::SUB:
844 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
845 N.getOperand(0).Val->hasOneUse()) {
846 ++FusedFP; // Statistic
847 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
848 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
849 Tmp3 = SelectExpr(N.getOperand(1));
850 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
851 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
852 return Result;
853 }
854 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
855 Tmp1 = SelectExpr(N.getOperand(0));
856 Tmp2 = SelectExpr(N.getOperand(1));
857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
858 return Result;
859
860 case ISD::MUL:
861 case ISD::SDIV:
862 switch( opcode ) {
863 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
864 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
865 };
866 Tmp1 = SelectExpr(N.getOperand(0));
867 Tmp2 = SelectExpr(N.getOperand(1));
868 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
869 return Result;
870
871 case ISD::UINT_TO_FP:
872 case ISD::SINT_TO_FP: {
873 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
874 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
875 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
876 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
877 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000878
Nate Begemand3e6b942005-04-05 08:51:15 +0000879 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
880 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000881
Nate Begemand3e6b942005-04-05 08:51:15 +0000882 // FIXME: pull this FP constant generation stuff out into something like
883 // the simple ISel's getReg.
884 if (IsUnsigned) {
885 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
886 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
887 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
888 } else {
889 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
890 unsigned CPI = CP->getConstantPoolIndex(CFP);
891 // Load constant fp value
892 unsigned Tmp4 = MakeReg(MVT::i32);
893 unsigned TmpL = MakeReg(MVT::i32);
894 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
895 .addConstantPoolIndex(CPI);
896 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
897 // Store the hi & low halves of the fp value, currently in int regs
898 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
899 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
900 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
901 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
902 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
903 // Generate the return value with a subtract
904 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
905 }
906 return Result;
907 }
908 }
909 assert(0 && "Should never get here");
910 return 0;
911}
912
913unsigned ISel::SelectExpr(SDOperand N) {
914 unsigned Result;
915 unsigned Tmp1, Tmp2, Tmp3;
916 unsigned Opc = 0;
917 unsigned opcode = N.getOpcode();
918
919 SDNode *Node = N.Val;
920 MVT::ValueType DestType = N.getValueType();
921
922 unsigned &Reg = ExprMap[N];
923 if (Reg) return Reg;
924
925 switch (N.getOpcode()) {
926 default:
927 Reg = Result = (N.getValueType() != MVT::Other) ?
928 MakeReg(N.getValueType()) : 1;
929 break;
930 case ISD::CALL:
931 // If this is a call instruction, make sure to prepare ALL of the result
932 // values as well as the chain.
933 if (Node->getNumValues() == 1)
934 Reg = Result = 1; // Void call, just a chain.
935 else {
936 Result = MakeReg(Node->getValueType(0));
937 ExprMap[N.getValue(0)] = Result;
938 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
939 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
940 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
941 }
942 break;
943 }
944
945 if (ISD::CopyFromReg == opcode)
946 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000947
Nate Begemand3e6b942005-04-05 08:51:15 +0000948 if (DestType == MVT::f64 || DestType == MVT::f32)
949 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
950 return SelectExprFP(N, Result);
951
952 switch (opcode) {
953 default:
954 Node->dump();
955 assert(0 && "Node not handled!\n");
956 case ISD::UNDEF:
957 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
958 return Result;
959 case ISD::DYNAMIC_STACKALLOC:
960 // Generate both result values. FIXME: Need a better commment here?
961 if (Result != 1)
962 ExprMap[N.getValue(1)] = 1;
963 else
964 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
965
966 // FIXME: We are currently ignoring the requested alignment for handling
967 // greater than the stack alignment. This will need to be revisited at some
968 // point. Align = N.getOperand(2);
969 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
970 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
971 std::cerr << "Cannot allocate stack object with greater alignment than"
972 << " the stack alignment yet!";
973 abort();
974 }
975 Select(N.getOperand(0));
976 Tmp1 = SelectExpr(N.getOperand(1));
977 // Subtract size from stack pointer, thereby allocating some space.
978 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
979 // Put a pointer to the space into the result register by copying the SP
980 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
981 return Result;
982
983 case ISD::ConstantPool:
984 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
985 Tmp2 = MakeReg(MVT::i64);
986 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
987 .addConstantPoolIndex(Tmp1);
988 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
989 return Result;
990
991 case ISD::FrameIndex:
992 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
993 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
994 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000995
Nate Begemand3e6b942005-04-05 08:51:15 +0000996 case ISD::GlobalAddress: {
997 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
998 Tmp1 = MakeReg(MVT::i64);
999 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1000 .addGlobalAddress(GV);
1001 if (GV->hasWeakLinkage() || GV->isExternal()) {
Nate Begemana9532d52005-04-08 23:45:01 +00001002 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001003 } else {
1004 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1005 }
1006 return Result;
1007 }
1008
1009 case ISD::LOAD:
1010 case ISD::EXTLOAD:
1011 case ISD::ZEXTLOAD:
1012 case ISD::SEXTLOAD: {
1013 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1014 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1015 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001016
Nate Begemand3e6b942005-04-05 08:51:15 +00001017 // Make sure we generate both values.
1018 if (Result != 1)
1019 ExprMap[N.getValue(1)] = 1; // Generate the token
1020 else
1021 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1022
1023 SDOperand Chain = N.getOperand(0);
1024 SDOperand Address = N.getOperand(1);
1025 Select(Chain);
1026
1027 switch (TypeBeingLoaded) {
1028 default: Node->dump(); assert(0 && "Cannot load this type!");
1029 case MVT::i1: Opc = PPC::LBZ; break;
1030 case MVT::i8: Opc = PPC::LBZ; break;
1031 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1032 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1033 case MVT::i64: Opc = PPC::LD; break;
1034 case MVT::f32: Opc = PPC::LFS; break;
1035 case MVT::f64: Opc = PPC::LFD; break;
1036 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001037
Nate Begemand3e6b942005-04-05 08:51:15 +00001038 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1039 Tmp1 = MakeReg(MVT::i64);
1040 int CPI = CP->getIndex();
1041 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1042 .addConstantPoolIndex(CPI);
1043 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1044 }
1045 else if(Address.getOpcode() == ISD::FrameIndex) {
1046 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1047 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1048 } else {
1049 int offset;
1050 bool idx = SelectAddr(Address, Tmp1, offset);
1051 if (idx) {
1052 Opc = IndexedOpForOp(Opc);
1053 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1054 } else {
1055 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1056 }
1057 }
1058 return Result;
1059 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001060
Nate Begemand3e6b942005-04-05 08:51:15 +00001061 case ISD::CALL: {
1062 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001063 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001064 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1065 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1066 };
1067 static const unsigned FPR[] = {
1068 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1069 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1070 };
1071
1072 // Lower the chain for this call.
1073 Select(N.getOperand(0));
1074 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1075
1076 MachineInstr *CallMI;
1077 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001078 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001079 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001080 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001081 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001082 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001083 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001084 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001085 true);
1086 } else {
1087 Tmp1 = SelectExpr(N.getOperand(1));
1088 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1089 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1090 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1091 .addReg(PPC::R12);
1092 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001093
Nate Begemand3e6b942005-04-05 08:51:15 +00001094 // Load the register args to virtual regs
1095 std::vector<unsigned> ArgVR;
1096 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1097 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1098
1099 // Copy the virtual registers into the appropriate argument register
1100 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1101 switch(N.getOperand(i+2).getValueType()) {
1102 default: Node->dump(); assert(0 && "Unknown value type for call");
1103 case MVT::i1:
1104 case MVT::i8:
1105 case MVT::i16:
1106 case MVT::i32:
1107 case MVT::i64:
1108 assert(GPR_idx < 8 && "Too many int args");
1109 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1110 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1111 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1112 }
1113 ++GPR_idx;
1114 break;
1115 case MVT::f64:
1116 case MVT::f32:
1117 assert(FPR_idx < 13 && "Too many fp args");
1118 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1119 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1120 ++FPR_idx;
1121 break;
1122 }
1123 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001124
Nate Begemand3e6b942005-04-05 08:51:15 +00001125 // Put the call instruction in the correct place in the MachineBasicBlock
1126 BB->push_back(CallMI);
1127
1128 switch (Node->getValueType(0)) {
1129 default: assert(0 && "Unknown value type for call result!");
1130 case MVT::Other: return 1;
1131 case MVT::i1:
1132 case MVT::i8:
1133 case MVT::i16:
1134 case MVT::i32:
1135 case MVT::i64:
1136 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1137 break;
1138 case MVT::f32:
1139 case MVT::f64:
1140 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1141 break;
1142 }
1143 return Result+N.ResNo;
1144 }
1145
1146 case ISD::SIGN_EXTEND:
1147 case ISD::SIGN_EXTEND_INREG:
1148 Tmp1 = SelectExpr(N.getOperand(0));
1149 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1150 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1151 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001152 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001153 break;
1154 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001155 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001156 break;
1157 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001158 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001159 break;
1160 case MVT::i1:
1161 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1162 break;
1163 }
1164 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001165
Nate Begemand3e6b942005-04-05 08:51:15 +00001166 case ISD::CopyFromReg:
1167 if (Result == 1)
1168 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1169 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1170 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1171 return Result;
1172
1173 case ISD::SHL:
1174 Tmp1 = SelectExpr(N.getOperand(0));
1175 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001176 Tmp2 = CN->getValue() & 0x3F;
1177 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1178 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001179 } else {
1180 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001181 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001182 }
1183 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001184
Nate Begemand3e6b942005-04-05 08:51:15 +00001185 case ISD::SRL:
1186 Tmp1 = SelectExpr(N.getOperand(0));
1187 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001188 Tmp2 = CN->getValue() & 0x3F;
1189 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1190 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001191 } else {
1192 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001193 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001194 }
1195 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001196
Nate Begemand3e6b942005-04-05 08:51:15 +00001197 case ISD::SRA:
1198 Tmp1 = SelectExpr(N.getOperand(0));
1199 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001200 Tmp2 = CN->getValue() & 0x3F;
1201 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001202 } else {
1203 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001204 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001205 }
1206 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001207
Nate Begemand3e6b942005-04-05 08:51:15 +00001208 case ISD::ADD:
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1211 default: assert(0 && "unhandled result code");
1212 case 0: // No immediate
1213 Tmp2 = SelectExpr(N.getOperand(1));
1214 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1215 break;
1216 case 1: // Low immediate
1217 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1218 break;
1219 case 2: // Shifted immediate
1220 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1221 break;
1222 }
1223 return Result;
1224
1225 case ISD::AND:
1226 case ISD::OR:
1227 Tmp1 = SelectExpr(N.getOperand(0));
1228 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1229 default: assert(0 && "unhandled result code");
1230 case 0: // No immediate
1231 Tmp2 = SelectExpr(N.getOperand(1));
1232 switch (opcode) {
1233 case ISD::AND: Opc = PPC::AND; break;
1234 case ISD::OR: Opc = PPC::OR; break;
1235 }
1236 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1237 break;
1238 case 1: // Low immediate
1239 switch (opcode) {
1240 case ISD::AND: Opc = PPC::ANDIo; break;
1241 case ISD::OR: Opc = PPC::ORI; break;
1242 }
1243 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1244 break;
1245 case 2: // Shifted immediate
1246 switch (opcode) {
1247 case ISD::AND: Opc = PPC::ANDISo; break;
1248 case ISD::OR: Opc = PPC::ORIS; break;
1249 }
1250 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1251 break;
1252 }
1253 return Result;
1254
1255 case ISD::XOR: {
1256 // Check for EQV: xor, (xor a, -1), b
1257 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1258 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1259 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1260 ++NotLogic;
1261 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1262 Tmp2 = SelectExpr(N.getOperand(1));
1263 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1264 return Result;
1265 }
1266 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1267 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1268 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1269 ++NotLogic;
1270 switch(N.getOperand(0).getOpcode()) {
1271 case ISD::OR:
1272 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1273 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1274 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1275 break;
1276 case ISD::AND:
1277 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1278 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1279 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1280 break;
1281 default:
1282 Tmp1 = SelectExpr(N.getOperand(0));
1283 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1284 break;
1285 }
1286 return Result;
1287 }
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1290 default: assert(0 && "unhandled result code");
1291 case 0: // No immediate
1292 Tmp2 = SelectExpr(N.getOperand(1));
1293 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1294 break;
1295 case 1: // Low immediate
1296 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1297 break;
1298 case 2: // Shifted immediate
1299 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1300 break;
1301 }
1302 return Result;
1303 }
1304
1305 case ISD::SUB:
1306 Tmp2 = SelectExpr(N.getOperand(1));
1307 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1308 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1309 else {
1310 Tmp1 = SelectExpr(N.getOperand(0));
1311 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1312 }
1313 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001314
Nate Begemand3e6b942005-04-05 08:51:15 +00001315 case ISD::MUL:
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1318 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1319 else {
1320 Tmp2 = SelectExpr(N.getOperand(1));
1321 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1322 }
1323 return Result;
1324
1325 case ISD::SDIV:
1326 case ISD::UDIV:
1327 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1328 Tmp1 = MakeReg(MVT::i64);
1329 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001330 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001331 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1332 return Result;
1333 }
1334 Tmp1 = SelectExpr(N.getOperand(0));
1335 Tmp2 = SelectExpr(N.getOperand(1));
1336 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1337 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1338 return Result;
1339
Nate Begemand3e6b942005-04-05 08:51:15 +00001340 case ISD::FP_TO_UINT:
1341 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001342 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001343 Tmp2 = MakeReg(MVT::f64);
1344 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1345 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1346 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1347 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1348 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001349 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001350
Nate Begemand3e6b942005-04-05 08:51:15 +00001351 case ISD::SETCC:
1352 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1353 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001354
Nate Begemand3e6b942005-04-05 08:51:15 +00001355 unsigned TrueValue = MakeReg(MVT::i32);
1356 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1357 unsigned FalseValue = MakeReg(MVT::i32);
1358 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1359
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001360 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001361 // value and the MBB to hold the PHI instruction for this SetCC.
1362 MachineBasicBlock *thisMBB = BB;
1363 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1364 ilist<MachineBasicBlock>::iterator It = BB;
1365 ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001366
Nate Begemand3e6b942005-04-05 08:51:15 +00001367 // thisMBB:
1368 // ...
1369 // cmpTY cr0, r1, r2
1370 // %TrueValue = li 1
1371 // bCC sinkMBB
1372 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1373 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1374 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1375 MachineFunction *F = BB->getParent();
1376 F->getBasicBlockList().insert(It, copy0MBB);
1377 F->getBasicBlockList().insert(It, sinkMBB);
1378 // Update machine-CFG edges
1379 BB->addSuccessor(copy0MBB);
1380 BB->addSuccessor(sinkMBB);
1381
1382 // copy0MBB:
1383 // %FalseValue = li 0
1384 // fallthrough
1385 BB = copy0MBB;
1386 // Update machine-CFG edges
1387 BB->addSuccessor(sinkMBB);
1388
1389 // sinkMBB:
1390 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1391 // ...
1392 BB = sinkMBB;
1393 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1394 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1395 return Result;
1396 }
1397 assert(0 && "Is this legal?");
1398 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001399
Nate Begemand3e6b942005-04-05 08:51:15 +00001400 case ISD::SELECT: {
1401 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1402 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1403 Opc = SelectSetCR0(N.getOperand(0));
1404
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001405 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001406 // value and the MBB to hold the PHI instruction for this SetCC.
1407 MachineBasicBlock *thisMBB = BB;
1408 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1409 ilist<MachineBasicBlock>::iterator It = BB;
1410 ++It;
1411
1412 // thisMBB:
1413 // ...
1414 // TrueVal = ...
1415 // cmpTY cr0, r1, r2
1416 // bCC copy1MBB
1417 // fallthrough --> copy0MBB
1418 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1419 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1420 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1421 MachineFunction *F = BB->getParent();
1422 F->getBasicBlockList().insert(It, copy0MBB);
1423 F->getBasicBlockList().insert(It, sinkMBB);
1424 // Update machine-CFG edges
1425 BB->addSuccessor(copy0MBB);
1426 BB->addSuccessor(sinkMBB);
1427
1428 // copy0MBB:
1429 // %FalseValue = ...
1430 // # fallthrough to sinkMBB
1431 BB = copy0MBB;
1432 // Update machine-CFG edges
1433 BB->addSuccessor(sinkMBB);
1434
1435 // sinkMBB:
1436 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1437 // ...
1438 BB = sinkMBB;
1439 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1440 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1441
1442 // FIXME: Select i64?
1443 return Result;
1444 }
1445
1446 case ISD::Constant:
1447 switch (N.getValueType()) {
1448 default: assert(0 && "Cannot use constants of this type!");
1449 case MVT::i1:
1450 BuildMI(BB, PPC::LI, 1, Result)
1451 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1452 break;
1453 case MVT::i32:
1454 {
1455 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1456 if (v < 32768 && v >= -32768) {
1457 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1458 } else {
1459 Tmp1 = MakeReg(MVT::i32);
1460 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1461 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1462 }
1463 }
1464 }
1465 return Result;
1466 }
1467
1468 return 0;
1469}
1470
1471void ISel::Select(SDOperand N) {
1472 unsigned Tmp1, Tmp2, Opc;
1473 unsigned opcode = N.getOpcode();
1474
1475 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1476 return; // Already selected.
1477
1478 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001479
Nate Begemand3e6b942005-04-05 08:51:15 +00001480 switch (Node->getOpcode()) {
1481 default:
1482 Node->dump(); std::cerr << "\n";
1483 assert(0 && "Node not handled yet!");
1484 case ISD::EntryToken: return; // Noop
1485 case ISD::TokenFactor:
1486 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1487 Select(Node->getOperand(i));
1488 return;
1489 case ISD::ADJCALLSTACKDOWN:
1490 case ISD::ADJCALLSTACKUP:
1491 Select(N.getOperand(0));
1492 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1493 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1494 PPC::ADJCALLSTACKUP;
1495 BuildMI(BB, Opc, 1).addImm(Tmp1);
1496 return;
1497 case ISD::BR: {
1498 MachineBasicBlock *Dest =
1499 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1500 Select(N.getOperand(0));
1501 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1502 return;
1503 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001504 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001505 SelectBranchCC(N);
1506 return;
1507 case ISD::CopyToReg:
1508 Select(N.getOperand(0));
1509 Tmp1 = SelectExpr(N.getOperand(1));
1510 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001511
Nate Begemand3e6b942005-04-05 08:51:15 +00001512 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001513 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001514 N.getOperand(1).getValueType() == MVT::f32)
1515 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1516 else
1517 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1518 }
1519 return;
1520 case ISD::ImplicitDef:
1521 Select(N.getOperand(0));
1522 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1523 return;
1524 case ISD::RET:
1525 switch (N.getNumOperands()) {
1526 default:
1527 assert(0 && "Unknown return instruction!");
1528 case 3:
1529 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1530 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001531 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001532 Select(N.getOperand(0));
1533 Tmp1 = SelectExpr(N.getOperand(1));
1534 Tmp2 = SelectExpr(N.getOperand(2));
1535 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1536 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1537 break;
1538 case 2:
1539 Select(N.getOperand(0));
1540 Tmp1 = SelectExpr(N.getOperand(1));
1541 switch (N.getOperand(1).getValueType()) {
1542 default:
1543 assert(0 && "Unknown return type!");
1544 case MVT::f64:
1545 case MVT::f32:
1546 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1547 break;
1548 case MVT::i32:
1549 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1550 break;
1551 }
1552 case 1:
1553 Select(N.getOperand(0));
1554 break;
1555 }
1556 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1557 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001558 case ISD::TRUNCSTORE:
1559 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001560 {
1561 SDOperand Chain = N.getOperand(0);
1562 SDOperand Value = N.getOperand(1);
1563 SDOperand Address = N.getOperand(2);
1564 Select(Chain);
1565
1566 Tmp1 = SelectExpr(Value); //value
1567
1568 if (opcode == ISD::STORE) {
1569 switch(Value.getValueType()) {
1570 default: assert(0 && "unknown Type in store");
1571 case MVT::i64: Opc = PPC::STD; break;
1572 case MVT::f64: Opc = PPC::STFD; break;
1573 case MVT::f32: Opc = PPC::STFS; break;
1574 }
1575 } else { //ISD::TRUNCSTORE
1576 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1577 default: assert(0 && "unknown Type in store");
1578 case MVT::i1: //FIXME: DAG does not promote this load
1579 case MVT::i8: Opc= PPC::STB; break;
1580 case MVT::i16: Opc = PPC::STH; break;
1581 case MVT::i32: Opc = PPC::STW; break;
1582 }
1583 }
1584
1585 if(Address.getOpcode() == ISD::FrameIndex)
1586 {
1587 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1588 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1589 }
1590 else
1591 {
1592 int offset;
1593 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001594 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001595 Opc = IndexedOpForOp(Opc);
1596 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1597 } else {
1598 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1599 }
1600 }
1601 return;
1602 }
1603 case ISD::EXTLOAD:
1604 case ISD::SEXTLOAD:
1605 case ISD::ZEXTLOAD:
1606 case ISD::LOAD:
1607 case ISD::CopyFromReg:
1608 case ISD::CALL:
1609 case ISD::DYNAMIC_STACKALLOC:
1610 ExprMap.erase(N);
1611 SelectExpr(N);
1612 return;
1613 }
1614 assert(0 && "Should not be reached!");
1615}
1616
1617
1618/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1619/// into a machine code representation using pattern matching and a machine
1620/// description file.
1621///
1622FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001623 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001624}
1625