blob: a654adced928e48f73e69a42cfc181afbd46e445 [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>
94 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
95 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,
238 SDOperand Callee, ArgListTy &Args,
239 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000240 // args_to_use will accumulate outgoing args for the ISD::CALL case in
241 // SelectExpr to use to put the arguments in the appropriate registers.
242 std::vector<SDOperand> args_to_use;
243
244 // Count how many bytes are to be pushed on the stack, including the linkage
245 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000246 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000247
248 if (Args.empty()) {
249 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
250 DAG.getConstant(NumBytes, getPointerTy()));
251 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000252 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000253
254 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000255 // plus 64 bytes of argument space in case any called code gets funky on us.
256 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000257
258 // Adjust the stack pointer for the new arguments...
259 // These operations are automatically eliminated by the prolog/epilog pass
260 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
261 DAG.getConstant(NumBytes, getPointerTy()));
262
263 // Set up a copy of the stack pointer for use loading and storing any
264 // arguments that may not fit in the registers available for argument
265 // passing.
266 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
267 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000268
Nate Begemand3e6b942005-04-05 08:51:15 +0000269 // Figure out which arguments are going to go in registers, and which in
270 // memory. Also, if this is a vararg function, floating point operations
271 // must be stored to our stack, and loaded into integer regs as well, if
272 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000273 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000274 unsigned GPR_remaining = 8;
275 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000276
Nate Begemand3e6b942005-04-05 08:51:15 +0000277 std::vector<SDOperand> MemOps;
278 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
279 // PtrOff will be used to store the current argument to the stack if a
280 // register cannot be found for it.
281 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
282 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
283 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000284
Nate Begemand3e6b942005-04-05 08:51:15 +0000285 switch (ArgVT) {
286 default: assert(0 && "Unexpected ValueType for argument!");
287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000290 case MVT::i32:
291 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000292 // sign extend, otherwise use a zero extend.
293 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000294 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000295 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000296 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000297 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000298 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000299 if (GPR_remaining > 0) {
300 args_to_use.push_back(Args[i].first);
301 --GPR_remaining;
302 } else {
303 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000304 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000305 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000306 ArgOffset += 8;
307 break;
308 case MVT::f32:
309 case MVT::f64:
310 if (FPR_remaining > 0) {
311 args_to_use.push_back(Args[i].first);
312 --FPR_remaining;
313 if (isVarArg) {
314 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000315 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000316 MemOps.push_back(Store);
317 // Float varargs are always shadowed in available integer registers
318 if (GPR_remaining > 0) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000319 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000320 MemOps.push_back(Load);
321 args_to_use.push_back(Load);
322 --GPR_remaining;
323 }
324 } else {
325 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000326 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000327 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000328 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000329 --GPR_remaining;
330 }
331 }
332 } else {
333 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000334 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000335 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000336 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000337 break;
338 }
339 }
340 if (!MemOps.empty())
341 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
342 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000343
Nate Begemand3e6b942005-04-05 08:51:15 +0000344 std::vector<MVT::ValueType> RetVals;
345 MVT::ValueType RetTyVT = getValueType(RetTy);
346 if (RetTyVT != MVT::isVoid)
347 RetVals.push_back(RetTyVT);
348 RetVals.push_back(MVT::Other);
349
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000350 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000351 Chain, Callee, args_to_use), 0);
352 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
353 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
354 DAG.getConstant(NumBytes, getPointerTy()));
355 return std::make_pair(TheCall, Chain);
356}
357
358std::pair<SDOperand, SDOperand>
359PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
360 //vastart just returns the address of the VarArgsFrameIndex slot.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000361 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
Nate Begemand3e6b942005-04-05 08:51:15 +0000362}
363
364std::pair<SDOperand,SDOperand> PPC64TargetLowering::
365LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
366 const Type *ArgTy, SelectionDAG &DAG) {
367 MVT::ValueType ArgVT = getValueType(ArgTy);
368 SDOperand Result;
369 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000370 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000371 } else {
Nate Begemand3e6b942005-04-05 08:51:15 +0000372 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000373 DAG.getConstant(8, VAList.getValueType()));
Nate Begemand3e6b942005-04-05 08:51:15 +0000374 }
375 return std::make_pair(Result, Chain);
376}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377
Nate Begemand3e6b942005-04-05 08:51:15 +0000378
379std::pair<SDOperand, SDOperand> PPC64TargetLowering::
380LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
381 SelectionDAG &DAG) {
382 assert(0 && "LowerFrameReturnAddress unimplemented");
383 abort();
384}
385
386namespace {
387Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
388Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
389//===--------------------------------------------------------------------===//
390/// ISel - PPC32 specific code to select PPC32 machine instructions for
391/// SelectionDAG operations.
392//===--------------------------------------------------------------------===//
393class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000394
Nate Begemand3e6b942005-04-05 08:51:15 +0000395 /// Comment Here.
396 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000397
Nate Begemand3e6b942005-04-05 08:51:15 +0000398 /// ExprMap - As shared expressions are codegen'd, we keep track of which
399 /// vreg the value is produced in, so we only emit one copy of each compiled
400 /// tree.
401 std::map<SDOperand, unsigned> ExprMap;
402
403 unsigned GlobalBaseReg;
404 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000405
Nate Begemand3e6b942005-04-05 08:51:15 +0000406public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000407 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000408 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000409
Nate Begemand3e6b942005-04-05 08:51:15 +0000410 /// runOnFunction - Override this function in order to reset our per-function
411 /// variables.
412 virtual bool runOnFunction(Function &Fn) {
413 // Make sure we re-emit a set of the global base reg if necessary
414 GlobalBaseInitialized = false;
415 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416 }
417
Nate Begemand3e6b942005-04-05 08:51:15 +0000418 /// InstructionSelectBasicBlock - This callback is invoked by
419 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
420 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
421 DEBUG(BB->dump());
422 // Codegen the basic block.
423 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000424
Nate Begemand3e6b942005-04-05 08:51:15 +0000425 // Clear state used for selection.
426 ExprMap.clear();
427 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000428
Nate Begemand3e6b942005-04-05 08:51:15 +0000429 unsigned getGlobalBaseReg();
430 unsigned getConstDouble(double floatVal, unsigned Result);
431 unsigned SelectSetCR0(SDOperand CC);
432 unsigned SelectExpr(SDOperand N);
433 unsigned SelectExprFP(SDOperand N, unsigned Result);
434 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000435
Nate Begemand3e6b942005-04-05 08:51:15 +0000436 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
437 void SelectBranchCC(SDOperand N);
438};
439
440/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
441/// returns zero when the input is not exactly a power of two.
442static unsigned ExactLog2(unsigned Val) {
443 if (Val == 0 || (Val & (Val-1))) return 0;
444 unsigned Count = 0;
445 while (Val != 1) {
446 Val >>= 1;
447 ++Count;
448 }
449 return Count;
450}
451
452/// getImmediateForOpcode - This method returns a value indicating whether
453/// the ConstantSDNode N can be used as an immediate to Opcode. The return
454/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000455/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000456/// of 1 indicates that the constant may be used in normal immediate form. A
457/// return value of 2 indicates that the constant may be used in shifted
458/// immediate form. A return value of 3 indicates that log base 2 of the
459/// constant may be used.
460///
461static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
462 unsigned& Imm, bool U = false) {
463 if (N.getOpcode() != ISD::Constant) return 0;
464
465 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000466
Nate Begemand3e6b942005-04-05 08:51:15 +0000467 switch(Opcode) {
468 default: return 0;
469 case ISD::ADD:
470 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
471 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
472 break;
473 case ISD::AND:
474 case ISD::XOR:
475 case ISD::OR:
476 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
477 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
478 break;
479 case ISD::MUL:
480 case ISD::SUB:
481 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
482 break;
483 case ISD::SETCC:
484 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
485 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
486 break;
487 case ISD::SDIV:
488 if ((Imm = ExactLog2(v))) { return 3; }
489 break;
490 }
491 return 0;
492}
493
494/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
495/// to Condition. If the Condition is unordered or unsigned, the bool argument
496/// U is set to true, otherwise it is set to false.
497static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
498 U = false;
499 switch (Condition) {
500 default: assert(0 && "Unknown condition!"); abort();
501 case ISD::SETEQ: return PPC::BEQ;
502 case ISD::SETNE: return PPC::BNE;
503 case ISD::SETULT: U = true;
504 case ISD::SETLT: return PPC::BLT;
505 case ISD::SETULE: U = true;
506 case ISD::SETLE: return PPC::BLE;
507 case ISD::SETUGT: U = true;
508 case ISD::SETGT: return PPC::BGT;
509 case ISD::SETUGE: U = true;
510 case ISD::SETGE: return PPC::BGE;
511 }
512 return 0;
513}
514
515/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
516/// and store immediate instructions.
517static unsigned IndexedOpForOp(unsigned Opcode) {
518 switch(Opcode) {
519 default: assert(0 && "Unknown opcode!"); abort();
520 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
521 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
522 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
523 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
524 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
525 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
526 case PPC::LFD: return PPC::LFDX;
527 }
528 return 0;
529}
530}
531
532/// getGlobalBaseReg - Output the instructions required to put the
533/// base address to use for accessing globals into a register.
534///
535unsigned ISel::getGlobalBaseReg() {
536 if (!GlobalBaseInitialized) {
537 // Insert the set of GlobalBaseReg into the first MBB of the function
538 MachineBasicBlock &FirstMBB = BB->getParent()->front();
539 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
540 GlobalBaseReg = MakeReg(MVT::i64);
541 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
542 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
543 GlobalBaseInitialized = true;
544 }
545 return GlobalBaseReg;
546}
547
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000549/// Constant Pool. Optionally takes a register in which to load the value.
550unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000551 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000552 if (0 == Result) Result = MakeReg(MVT::f64);
553 MachineConstantPool *CP = BB->getParent()->getConstantPool();
554 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
555 unsigned CPI = CP->getConstantPoolIndex(CFP);
556 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
557 .addConstantPoolIndex(CPI);
558 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
559 return Result;
560}
561
562unsigned ISel::SelectSetCR0(SDOperand CC) {
563 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000564 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000565 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000566
Nate Begemand3e6b942005-04-05 08:51:15 +0000567 // If the first operand to the select is a SETCC node, then we can fold it
568 // into the branch that selects which value to return.
569 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
570 if (SetCC && CC.getOpcode() == ISD::SETCC) {
571 bool U;
572 Opc = getBCCForSetCC(SetCC->getCondition(), U);
573 Tmp1 = SelectExpr(SetCC->getOperand(0));
574
575 // Pass the optional argument U to getImmediateForOpcode for SETCC,
576 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000577 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000578 Tmp2, U)) {
579 if (U)
580 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
581 else
582 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
583 } else {
584 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
585 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
586 Tmp2 = SelectExpr(SetCC->getOperand(1));
587 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
588 }
589 } else {
590 Tmp1 = SelectExpr(CC);
591 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
592 Opc = PPC::BNE;
593 }
594 return Opc;
595}
596
597/// Check to see if the load is a constant offset from a base register
598bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
599{
600 unsigned imm = 0, opcode = N.getOpcode();
601 if (N.getOpcode() == ISD::ADD) {
602 Reg = SelectExpr(N.getOperand(0));
603 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
604 offset = imm;
605 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000606 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000607 offset = SelectExpr(N.getOperand(1));
608 return true;
609 }
610 Reg = SelectExpr(N);
611 offset = 0;
612 return false;
613}
614
615void ISel::SelectBranchCC(SDOperand N)
616{
617 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000618 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000619 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
620
621 // Get the MBB we will fall through to so that we can hand it off to the
622 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
623 //ilist<MachineBasicBlock>::iterator It = BB;
624 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000625
Nate Begemand3e6b942005-04-05 08:51:15 +0000626 Select(N.getOperand(0)); //chain
627 unsigned Opc = SelectSetCR0(N.getOperand(1));
628 // FIXME: Use this once we have something approximating two-way branches
629 // We cannot currently use this in case the ISel hands us something like
630 // BRcc MBBx
631 // BR MBBy
632 // since the fallthrough basic block for the conditional branch does not start
633 // with the unconditional branch (it is skipped over).
634 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
635 // .addMBB(Dest).addMBB(Fallthrough);
636 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
637 return;
638}
639
640unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
641{
642 unsigned Tmp1, Tmp2, Tmp3;
643 unsigned Opc = 0;
644 SDNode *Node = N.Val;
645 MVT::ValueType DestType = N.getValueType();
646 unsigned opcode = N.getOpcode();
647
648 switch (opcode) {
649 default:
650 Node->dump();
651 assert(0 && "Node not handled!\n");
652
653 case ISD::SELECT: {
654 // Attempt to generate FSEL. We can do this whenever we have an FP result,
655 // and an FP comparison in the SetCC node.
656 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
657 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
658 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
659 SetCC->getCondition() != ISD::SETEQ &&
660 SetCC->getCondition() != ISD::SETNE) {
661 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
662 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
663 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
664 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000665
Nate Begemand3e6b942005-04-05 08:51:15 +0000666 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
667 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
668 switch(SetCC->getCondition()) {
669 default: assert(0 && "Invalid FSEL condition"); abort();
670 case ISD::SETULT:
671 case ISD::SETLT:
672 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
673 return Result;
674 case ISD::SETUGE:
675 case ISD::SETGE:
676 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
677 return Result;
678 case ISD::SETUGT:
679 case ISD::SETGT: {
680 Tmp2 = MakeReg(VT);
681 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
682 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
683 return Result;
684 }
685 case ISD::SETULE:
686 case ISD::SETLE: {
687 Tmp2 = MakeReg(VT);
688 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
689 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
690 return Result;
691 }
692 }
693 } else {
694 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
695 Tmp2 = SelectExpr(SetCC->getOperand(1));
696 Tmp3 = MakeReg(VT);
697 switch(SetCC->getCondition()) {
698 default: assert(0 && "Invalid FSEL condition"); abort();
699 case ISD::SETULT:
700 case ISD::SETLT:
701 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
702 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
703 return Result;
704 case ISD::SETUGE:
705 case ISD::SETGE:
706 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
707 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
708 return Result;
709 case ISD::SETUGT:
710 case ISD::SETGT:
711 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
712 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
713 return Result;
714 case ISD::SETULE:
715 case ISD::SETLE:
716 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
717 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
718 return Result;
719 }
720 }
721 assert(0 && "Should never get here");
722 return 0;
723 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000724
Nate Begemand3e6b942005-04-05 08:51:15 +0000725 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
726 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
727 Opc = SelectSetCR0(N.getOperand(0));
728
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000729 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000730 // value and the MBB to hold the PHI instruction for this SetCC.
731 MachineBasicBlock *thisMBB = BB;
732 const BasicBlock *LLVM_BB = BB->getBasicBlock();
733 ilist<MachineBasicBlock>::iterator It = BB;
734 ++It;
735
736 // thisMBB:
737 // ...
738 // TrueVal = ...
739 // cmpTY cr0, r1, r2
740 // bCC copy1MBB
741 // fallthrough --> copy0MBB
742 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
743 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
744 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
745 MachineFunction *F = BB->getParent();
746 F->getBasicBlockList().insert(It, copy0MBB);
747 F->getBasicBlockList().insert(It, sinkMBB);
748 // Update machine-CFG edges
749 BB->addSuccessor(copy0MBB);
750 BB->addSuccessor(sinkMBB);
751
752 // copy0MBB:
753 // %FalseValue = ...
754 // # fallthrough to sinkMBB
755 BB = copy0MBB;
756 // Update machine-CFG edges
757 BB->addSuccessor(sinkMBB);
758
759 // sinkMBB:
760 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
761 // ...
762 BB = sinkMBB;
763 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
764 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
765 return Result;
766 }
767
768 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000769 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000770 ISD::ADD == N.getOperand(0).getOpcode() &&
771 N.getOperand(0).Val->hasOneUse() &&
772 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
773 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
774 ++FusedFP; // Statistic
775 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
776 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
777 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
778 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
779 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000780 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000781 ISD::SUB == N.getOperand(0).getOpcode() &&
782 N.getOperand(0).Val->hasOneUse() &&
783 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
784 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
785 ++FusedFP; // Statistic
786 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
787 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
788 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
789 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
790 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
791 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
792 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
793 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
794 } else {
795 Tmp1 = SelectExpr(N.getOperand(0));
796 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
797 }
798 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000799
Nate Begemand3e6b942005-04-05 08:51:15 +0000800 case ISD::FABS:
801 Tmp1 = SelectExpr(N.getOperand(0));
802 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
803 return Result;
804
805 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000806 assert (DestType == MVT::f32 &&
807 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000808 "only f64 to f32 conversion supported here");
809 Tmp1 = SelectExpr(N.getOperand(0));
810 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
811 return Result;
812
813 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000814 assert (DestType == MVT::f64 &&
815 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000816 "only f32 to f64 conversion supported here");
817 Tmp1 = SelectExpr(N.getOperand(0));
818 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
819 return Result;
820
821 case ISD::CopyFromReg:
822 if (Result == 1)
823 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
824 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
825 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
826 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000827
Nate Begemand3e6b942005-04-05 08:51:15 +0000828 case ISD::ConstantFP: {
829 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
830 Result = getConstDouble(CN->getValue(), Result);
831 return Result;
832 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000833
Nate Begemand3e6b942005-04-05 08:51:15 +0000834 case ISD::ADD:
835 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
836 N.getOperand(0).Val->hasOneUse()) {
837 ++FusedFP; // Statistic
838 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
839 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
840 Tmp3 = SelectExpr(N.getOperand(1));
841 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
842 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
843 return Result;
844 }
845 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
846 Tmp1 = SelectExpr(N.getOperand(0));
847 Tmp2 = SelectExpr(N.getOperand(1));
848 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
849 return Result;
850
851 case ISD::SUB:
852 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
853 N.getOperand(0).Val->hasOneUse()) {
854 ++FusedFP; // Statistic
855 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
856 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
857 Tmp3 = SelectExpr(N.getOperand(1));
858 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
859 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
860 return Result;
861 }
862 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
863 Tmp1 = SelectExpr(N.getOperand(0));
864 Tmp2 = SelectExpr(N.getOperand(1));
865 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
866 return Result;
867
868 case ISD::MUL:
869 case ISD::SDIV:
870 switch( opcode ) {
871 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
872 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
873 };
874 Tmp1 = SelectExpr(N.getOperand(0));
875 Tmp2 = SelectExpr(N.getOperand(1));
876 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
877 return Result;
878
879 case ISD::UINT_TO_FP:
880 case ISD::SINT_TO_FP: {
881 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
882 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
883 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
884 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
885 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000886
Nate Begemand3e6b942005-04-05 08:51:15 +0000887 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
888 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000889
Nate Begemand3e6b942005-04-05 08:51:15 +0000890 // FIXME: pull this FP constant generation stuff out into something like
891 // the simple ISel's getReg.
892 if (IsUnsigned) {
893 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
894 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
895 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
896 } else {
897 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
898 unsigned CPI = CP->getConstantPoolIndex(CFP);
899 // Load constant fp value
900 unsigned Tmp4 = MakeReg(MVT::i32);
901 unsigned TmpL = MakeReg(MVT::i32);
902 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
903 .addConstantPoolIndex(CPI);
904 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
905 // Store the hi & low halves of the fp value, currently in int regs
906 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
907 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
908 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
909 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
910 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
911 // Generate the return value with a subtract
912 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
913 }
914 return Result;
915 }
916 }
917 assert(0 && "Should never get here");
918 return 0;
919}
920
921unsigned ISel::SelectExpr(SDOperand N) {
922 unsigned Result;
923 unsigned Tmp1, Tmp2, Tmp3;
924 unsigned Opc = 0;
925 unsigned opcode = N.getOpcode();
926
927 SDNode *Node = N.Val;
928 MVT::ValueType DestType = N.getValueType();
929
930 unsigned &Reg = ExprMap[N];
931 if (Reg) return Reg;
932
933 switch (N.getOpcode()) {
934 default:
935 Reg = Result = (N.getValueType() != MVT::Other) ?
936 MakeReg(N.getValueType()) : 1;
937 break;
938 case ISD::CALL:
939 // If this is a call instruction, make sure to prepare ALL of the result
940 // values as well as the chain.
941 if (Node->getNumValues() == 1)
942 Reg = Result = 1; // Void call, just a chain.
943 else {
944 Result = MakeReg(Node->getValueType(0));
945 ExprMap[N.getValue(0)] = Result;
946 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
947 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
948 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
949 }
950 break;
951 }
952
953 if (ISD::CopyFromReg == opcode)
954 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000955
Nate Begemand3e6b942005-04-05 08:51:15 +0000956 if (DestType == MVT::f64 || DestType == MVT::f32)
957 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
958 return SelectExprFP(N, Result);
959
960 switch (opcode) {
961 default:
962 Node->dump();
963 assert(0 && "Node not handled!\n");
964 case ISD::UNDEF:
965 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
966 return Result;
967 case ISD::DYNAMIC_STACKALLOC:
968 // Generate both result values. FIXME: Need a better commment here?
969 if (Result != 1)
970 ExprMap[N.getValue(1)] = 1;
971 else
972 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
973
974 // FIXME: We are currently ignoring the requested alignment for handling
975 // greater than the stack alignment. This will need to be revisited at some
976 // point. Align = N.getOperand(2);
977 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
978 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
979 std::cerr << "Cannot allocate stack object with greater alignment than"
980 << " the stack alignment yet!";
981 abort();
982 }
983 Select(N.getOperand(0));
984 Tmp1 = SelectExpr(N.getOperand(1));
985 // Subtract size from stack pointer, thereby allocating some space.
986 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
987 // Put a pointer to the space into the result register by copying the SP
988 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
989 return Result;
990
991 case ISD::ConstantPool:
992 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
993 Tmp2 = MakeReg(MVT::i64);
994 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
995 .addConstantPoolIndex(Tmp1);
996 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
997 return Result;
998
999 case ISD::FrameIndex:
1000 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1001 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1002 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003
Nate Begemand3e6b942005-04-05 08:51:15 +00001004 case ISD::GlobalAddress: {
1005 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1006 Tmp1 = MakeReg(MVT::i64);
1007 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1008 .addGlobalAddress(GV);
1009 if (GV->hasWeakLinkage() || GV->isExternal()) {
Nate Begemana9532d52005-04-08 23:45:01 +00001010 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001011 } else {
1012 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1013 }
1014 return Result;
1015 }
1016
1017 case ISD::LOAD:
1018 case ISD::EXTLOAD:
1019 case ISD::ZEXTLOAD:
1020 case ISD::SEXTLOAD: {
1021 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1022 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1023 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001024
Nate Begemand3e6b942005-04-05 08:51:15 +00001025 // Make sure we generate both values.
1026 if (Result != 1)
1027 ExprMap[N.getValue(1)] = 1; // Generate the token
1028 else
1029 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1030
1031 SDOperand Chain = N.getOperand(0);
1032 SDOperand Address = N.getOperand(1);
1033 Select(Chain);
1034
1035 switch (TypeBeingLoaded) {
1036 default: Node->dump(); assert(0 && "Cannot load this type!");
1037 case MVT::i1: Opc = PPC::LBZ; break;
1038 case MVT::i8: Opc = PPC::LBZ; break;
1039 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1040 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1041 case MVT::i64: Opc = PPC::LD; break;
1042 case MVT::f32: Opc = PPC::LFS; break;
1043 case MVT::f64: Opc = PPC::LFD; break;
1044 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001045
Nate Begemand3e6b942005-04-05 08:51:15 +00001046 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1047 Tmp1 = MakeReg(MVT::i64);
1048 int CPI = CP->getIndex();
1049 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1050 .addConstantPoolIndex(CPI);
1051 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1052 }
1053 else if(Address.getOpcode() == ISD::FrameIndex) {
1054 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1055 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1056 } else {
1057 int offset;
1058 bool idx = SelectAddr(Address, Tmp1, offset);
1059 if (idx) {
1060 Opc = IndexedOpForOp(Opc);
1061 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1062 } else {
1063 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1064 }
1065 }
1066 return Result;
1067 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001068
Nate Begemand3e6b942005-04-05 08:51:15 +00001069 case ISD::CALL: {
1070 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001071 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001072 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1073 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1074 };
1075 static const unsigned FPR[] = {
1076 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1077 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1078 };
1079
1080 // Lower the chain for this call.
1081 Select(N.getOperand(0));
1082 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1083
1084 MachineInstr *CallMI;
1085 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001086 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001087 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001088 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001089 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001090 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001091 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001092 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001093 true);
1094 } else {
1095 Tmp1 = SelectExpr(N.getOperand(1));
1096 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1097 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1098 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1099 .addReg(PPC::R12);
1100 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001101
Nate Begemand3e6b942005-04-05 08:51:15 +00001102 // Load the register args to virtual regs
1103 std::vector<unsigned> ArgVR;
1104 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1105 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1106
1107 // Copy the virtual registers into the appropriate argument register
1108 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1109 switch(N.getOperand(i+2).getValueType()) {
1110 default: Node->dump(); assert(0 && "Unknown value type for call");
1111 case MVT::i1:
1112 case MVT::i8:
1113 case MVT::i16:
1114 case MVT::i32:
1115 case MVT::i64:
1116 assert(GPR_idx < 8 && "Too many int args");
1117 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1118 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1119 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1120 }
1121 ++GPR_idx;
1122 break;
1123 case MVT::f64:
1124 case MVT::f32:
1125 assert(FPR_idx < 13 && "Too many fp args");
1126 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1127 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1128 ++FPR_idx;
1129 break;
1130 }
1131 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001132
Nate Begemand3e6b942005-04-05 08:51:15 +00001133 // Put the call instruction in the correct place in the MachineBasicBlock
1134 BB->push_back(CallMI);
1135
1136 switch (Node->getValueType(0)) {
1137 default: assert(0 && "Unknown value type for call result!");
1138 case MVT::Other: return 1;
1139 case MVT::i1:
1140 case MVT::i8:
1141 case MVT::i16:
1142 case MVT::i32:
1143 case MVT::i64:
1144 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1145 break;
1146 case MVT::f32:
1147 case MVT::f64:
1148 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1149 break;
1150 }
1151 return Result+N.ResNo;
1152 }
1153
1154 case ISD::SIGN_EXTEND:
1155 case ISD::SIGN_EXTEND_INREG:
1156 Tmp1 = SelectExpr(N.getOperand(0));
1157 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1158 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1159 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001160 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001161 break;
1162 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001163 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001164 break;
1165 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001166 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001167 break;
1168 case MVT::i1:
1169 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1170 break;
1171 }
1172 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001173
Nate Begemand3e6b942005-04-05 08:51:15 +00001174 case ISD::CopyFromReg:
1175 if (Result == 1)
1176 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1177 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1178 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1179 return Result;
1180
1181 case ISD::SHL:
1182 Tmp1 = SelectExpr(N.getOperand(0));
1183 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001184 Tmp2 = CN->getValue() & 0x3F;
1185 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1186 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001187 } else {
1188 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001189 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001190 }
1191 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001192
Nate Begemand3e6b942005-04-05 08:51:15 +00001193 case ISD::SRL:
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001196 Tmp2 = CN->getValue() & 0x3F;
1197 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1198 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001199 } else {
1200 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001201 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001202 }
1203 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001204
Nate Begemand3e6b942005-04-05 08:51:15 +00001205 case ISD::SRA:
1206 Tmp1 = SelectExpr(N.getOperand(0));
1207 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001208 Tmp2 = CN->getValue() & 0x3F;
1209 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001210 } else {
1211 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001212 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001213 }
1214 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001215
Nate Begemand3e6b942005-04-05 08:51:15 +00001216 case ISD::ADD:
1217 Tmp1 = SelectExpr(N.getOperand(0));
1218 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1219 default: assert(0 && "unhandled result code");
1220 case 0: // No immediate
1221 Tmp2 = SelectExpr(N.getOperand(1));
1222 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1223 break;
1224 case 1: // Low immediate
1225 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1226 break;
1227 case 2: // Shifted immediate
1228 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1229 break;
1230 }
1231 return Result;
1232
1233 case ISD::AND:
1234 case ISD::OR:
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1237 default: assert(0 && "unhandled result code");
1238 case 0: // No immediate
1239 Tmp2 = SelectExpr(N.getOperand(1));
1240 switch (opcode) {
1241 case ISD::AND: Opc = PPC::AND; break;
1242 case ISD::OR: Opc = PPC::OR; break;
1243 }
1244 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1245 break;
1246 case 1: // Low immediate
1247 switch (opcode) {
1248 case ISD::AND: Opc = PPC::ANDIo; break;
1249 case ISD::OR: Opc = PPC::ORI; break;
1250 }
1251 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1252 break;
1253 case 2: // Shifted immediate
1254 switch (opcode) {
1255 case ISD::AND: Opc = PPC::ANDISo; break;
1256 case ISD::OR: Opc = PPC::ORIS; break;
1257 }
1258 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1259 break;
1260 }
1261 return Result;
1262
1263 case ISD::XOR: {
1264 // Check for EQV: xor, (xor a, -1), b
1265 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1266 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1267 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1268 ++NotLogic;
1269 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1270 Tmp2 = SelectExpr(N.getOperand(1));
1271 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1272 return Result;
1273 }
1274 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1275 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1276 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1277 ++NotLogic;
1278 switch(N.getOperand(0).getOpcode()) {
1279 case ISD::OR:
1280 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1281 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1282 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1283 break;
1284 case ISD::AND:
1285 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1286 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1287 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1288 break;
1289 default:
1290 Tmp1 = SelectExpr(N.getOperand(0));
1291 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1292 break;
1293 }
1294 return Result;
1295 }
1296 Tmp1 = SelectExpr(N.getOperand(0));
1297 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1298 default: assert(0 && "unhandled result code");
1299 case 0: // No immediate
1300 Tmp2 = SelectExpr(N.getOperand(1));
1301 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1302 break;
1303 case 1: // Low immediate
1304 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1305 break;
1306 case 2: // Shifted immediate
1307 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1308 break;
1309 }
1310 return Result;
1311 }
1312
1313 case ISD::SUB:
1314 Tmp2 = SelectExpr(N.getOperand(1));
1315 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1316 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1317 else {
1318 Tmp1 = SelectExpr(N.getOperand(0));
1319 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1320 }
1321 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001322
Nate Begemand3e6b942005-04-05 08:51:15 +00001323 case ISD::MUL:
1324 Tmp1 = SelectExpr(N.getOperand(0));
1325 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1326 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1327 else {
1328 Tmp2 = SelectExpr(N.getOperand(1));
1329 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1330 }
1331 return Result;
1332
1333 case ISD::SDIV:
1334 case ISD::UDIV:
1335 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1336 Tmp1 = MakeReg(MVT::i64);
1337 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001338 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001339 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1340 return Result;
1341 }
1342 Tmp1 = SelectExpr(N.getOperand(0));
1343 Tmp2 = SelectExpr(N.getOperand(1));
1344 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1345 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1346 return Result;
1347
Nate Begemand3e6b942005-04-05 08:51:15 +00001348 case ISD::FP_TO_UINT:
1349 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001350 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001351 Tmp2 = MakeReg(MVT::f64);
1352 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1353 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1354 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1355 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1356 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001357 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001358
Nate Begemand3e6b942005-04-05 08:51:15 +00001359 case ISD::SETCC:
1360 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1361 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001362
Nate Begemand3e6b942005-04-05 08:51:15 +00001363 unsigned TrueValue = MakeReg(MVT::i32);
1364 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1365 unsigned FalseValue = MakeReg(MVT::i32);
1366 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1367
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001368 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001369 // value and the MBB to hold the PHI instruction for this SetCC.
1370 MachineBasicBlock *thisMBB = BB;
1371 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1372 ilist<MachineBasicBlock>::iterator It = BB;
1373 ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001374
Nate Begemand3e6b942005-04-05 08:51:15 +00001375 // thisMBB:
1376 // ...
1377 // cmpTY cr0, r1, r2
1378 // %TrueValue = li 1
1379 // bCC sinkMBB
1380 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1381 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1382 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1383 MachineFunction *F = BB->getParent();
1384 F->getBasicBlockList().insert(It, copy0MBB);
1385 F->getBasicBlockList().insert(It, sinkMBB);
1386 // Update machine-CFG edges
1387 BB->addSuccessor(copy0MBB);
1388 BB->addSuccessor(sinkMBB);
1389
1390 // copy0MBB:
1391 // %FalseValue = li 0
1392 // fallthrough
1393 BB = copy0MBB;
1394 // Update machine-CFG edges
1395 BB->addSuccessor(sinkMBB);
1396
1397 // sinkMBB:
1398 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1399 // ...
1400 BB = sinkMBB;
1401 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1402 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1403 return Result;
1404 }
1405 assert(0 && "Is this legal?");
1406 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001407
Nate Begemand3e6b942005-04-05 08:51:15 +00001408 case ISD::SELECT: {
1409 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1410 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1411 Opc = SelectSetCR0(N.getOperand(0));
1412
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001413 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001414 // value and the MBB to hold the PHI instruction for this SetCC.
1415 MachineBasicBlock *thisMBB = BB;
1416 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1417 ilist<MachineBasicBlock>::iterator It = BB;
1418 ++It;
1419
1420 // thisMBB:
1421 // ...
1422 // TrueVal = ...
1423 // cmpTY cr0, r1, r2
1424 // bCC copy1MBB
1425 // fallthrough --> copy0MBB
1426 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1427 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1428 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1429 MachineFunction *F = BB->getParent();
1430 F->getBasicBlockList().insert(It, copy0MBB);
1431 F->getBasicBlockList().insert(It, sinkMBB);
1432 // Update machine-CFG edges
1433 BB->addSuccessor(copy0MBB);
1434 BB->addSuccessor(sinkMBB);
1435
1436 // copy0MBB:
1437 // %FalseValue = ...
1438 // # fallthrough to sinkMBB
1439 BB = copy0MBB;
1440 // Update machine-CFG edges
1441 BB->addSuccessor(sinkMBB);
1442
1443 // sinkMBB:
1444 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1445 // ...
1446 BB = sinkMBB;
1447 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1448 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1449
1450 // FIXME: Select i64?
1451 return Result;
1452 }
1453
1454 case ISD::Constant:
1455 switch (N.getValueType()) {
1456 default: assert(0 && "Cannot use constants of this type!");
1457 case MVT::i1:
1458 BuildMI(BB, PPC::LI, 1, Result)
1459 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1460 break;
1461 case MVT::i32:
1462 {
1463 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1464 if (v < 32768 && v >= -32768) {
1465 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1466 } else {
1467 Tmp1 = MakeReg(MVT::i32);
1468 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1469 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1470 }
1471 }
1472 }
1473 return Result;
1474 }
1475
1476 return 0;
1477}
1478
1479void ISel::Select(SDOperand N) {
1480 unsigned Tmp1, Tmp2, Opc;
1481 unsigned opcode = N.getOpcode();
1482
1483 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1484 return; // Already selected.
1485
1486 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001487
Nate Begemand3e6b942005-04-05 08:51:15 +00001488 switch (Node->getOpcode()) {
1489 default:
1490 Node->dump(); std::cerr << "\n";
1491 assert(0 && "Node not handled yet!");
1492 case ISD::EntryToken: return; // Noop
1493 case ISD::TokenFactor:
1494 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1495 Select(Node->getOperand(i));
1496 return;
1497 case ISD::ADJCALLSTACKDOWN:
1498 case ISD::ADJCALLSTACKUP:
1499 Select(N.getOperand(0));
1500 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1501 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1502 PPC::ADJCALLSTACKUP;
1503 BuildMI(BB, Opc, 1).addImm(Tmp1);
1504 return;
1505 case ISD::BR: {
1506 MachineBasicBlock *Dest =
1507 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1508 Select(N.getOperand(0));
1509 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1510 return;
1511 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001512 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001513 SelectBranchCC(N);
1514 return;
1515 case ISD::CopyToReg:
1516 Select(N.getOperand(0));
1517 Tmp1 = SelectExpr(N.getOperand(1));
1518 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001519
Nate Begemand3e6b942005-04-05 08:51:15 +00001520 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001521 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001522 N.getOperand(1).getValueType() == MVT::f32)
1523 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1524 else
1525 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1526 }
1527 return;
1528 case ISD::ImplicitDef:
1529 Select(N.getOperand(0));
1530 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1531 return;
1532 case ISD::RET:
1533 switch (N.getNumOperands()) {
1534 default:
1535 assert(0 && "Unknown return instruction!");
1536 case 3:
1537 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1538 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001539 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001540 Select(N.getOperand(0));
1541 Tmp1 = SelectExpr(N.getOperand(1));
1542 Tmp2 = SelectExpr(N.getOperand(2));
1543 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1544 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1545 break;
1546 case 2:
1547 Select(N.getOperand(0));
1548 Tmp1 = SelectExpr(N.getOperand(1));
1549 switch (N.getOperand(1).getValueType()) {
1550 default:
1551 assert(0 && "Unknown return type!");
1552 case MVT::f64:
1553 case MVT::f32:
1554 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1555 break;
1556 case MVT::i32:
1557 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1558 break;
1559 }
1560 case 1:
1561 Select(N.getOperand(0));
1562 break;
1563 }
1564 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1565 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001566 case ISD::TRUNCSTORE:
1567 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001568 {
1569 SDOperand Chain = N.getOperand(0);
1570 SDOperand Value = N.getOperand(1);
1571 SDOperand Address = N.getOperand(2);
1572 Select(Chain);
1573
1574 Tmp1 = SelectExpr(Value); //value
1575
1576 if (opcode == ISD::STORE) {
1577 switch(Value.getValueType()) {
1578 default: assert(0 && "unknown Type in store");
1579 case MVT::i64: Opc = PPC::STD; break;
1580 case MVT::f64: Opc = PPC::STFD; break;
1581 case MVT::f32: Opc = PPC::STFS; break;
1582 }
1583 } else { //ISD::TRUNCSTORE
1584 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1585 default: assert(0 && "unknown Type in store");
1586 case MVT::i1: //FIXME: DAG does not promote this load
1587 case MVT::i8: Opc= PPC::STB; break;
1588 case MVT::i16: Opc = PPC::STH; break;
1589 case MVT::i32: Opc = PPC::STW; break;
1590 }
1591 }
1592
1593 if(Address.getOpcode() == ISD::FrameIndex)
1594 {
1595 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1596 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1597 }
1598 else
1599 {
1600 int offset;
1601 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001602 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001603 Opc = IndexedOpForOp(Opc);
1604 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1605 } else {
1606 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1607 }
1608 }
1609 return;
1610 }
1611 case ISD::EXTLOAD:
1612 case ISD::SEXTLOAD:
1613 case ISD::ZEXTLOAD:
1614 case ISD::LOAD:
1615 case ISD::CopyFromReg:
1616 case ISD::CALL:
1617 case ISD::DYNAMIC_STACKALLOC:
1618 ExprMap.erase(N);
1619 SelectExpr(N);
1620 return;
1621 }
1622 assert(0 && "Should not be reached!");
1623}
1624
1625
1626/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1627/// into a machine code representation using pattern matching and a machine
1628/// description file.
1629///
1630FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001631 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001632}
1633