blob: eb4410b32ac829d0090cf5be29b81ab7da5b31b1 [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//===----------------------------------------------------------------------===//
Chris Lattner0561b3f2005-08-02 19:26:06 +000037// PPC64TargetLowering - PPC64 Implementation of the TargetLowering interface
Nate Begemand3e6b942005-04-05 08:51:15 +000038namespace {
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 Lattner32f3cf62005-05-13 16:20:22 +000058 // We don't support sin/cos/sqrt/fmod
Chris Lattner08cae7f2005-04-30 04:26:56 +000059 setOperationAction(ISD::FSIN , MVT::f64, Expand);
60 setOperationAction(ISD::FCOS , MVT::f64, Expand);
61 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000062 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner08cae7f2005-04-30 04:26:56 +000063 setOperationAction(ISD::FSIN , MVT::f32, Expand);
64 setOperationAction(ISD::FCOS , MVT::f32, Expand);
65 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000066 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner08cae7f2005-04-30 04:26:56 +000067
Nate Begemand3e6b942005-04-05 08:51:15 +000068 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
69 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
70 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
71
Nate Begemane88aa5b2005-04-09 03:05:51 +000072 // PowerPC has no SREM/UREM instructions
73 setOperationAction(ISD::SREM, MVT::i64, Expand);
74 setOperationAction(ISD::UREM, MVT::i64, Expand);
75
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000076 // PowerPC has these, but they are not implemented
77 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
78 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000079 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000080
Nate Begemand3e6b942005-04-05 08:51:15 +000081 setShiftAmountFlavor(Extend); // shl X, 32 == 0
82 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083 addLegalFPImmediate(-0.0); //
Nate Begemand3e6b942005-04-05 08:51:15 +000084
85 computeRegisterProperties();
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemand3e6b942005-04-05 08:51:15 +000093 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
95 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +000097 bool isTailCall, SDOperand Callee, ArgListTy &Args,
98 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Nate Begemand3e6b942005-04-05 08:51:15 +0000100 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000101 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000102
Nate Begemand3e6b942005-04-05 08:51:15 +0000103 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000104 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemand3e6b942005-04-05 08:51:15 +0000105 const Type *ArgTy, SelectionDAG &DAG);
106
107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
109 SelectionDAG &DAG);
110 };
111}
112
113
114std::vector<SDOperand>
115PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 //
117 // add beautiful description of PPC stack frame format, or at least some docs
118 //
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000123
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemand3e6b942005-04-05 08:51:15 +0000125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000128 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +0000133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 };
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
139 };
140
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000142 // the arguments start at offset 48, although they are likely to be passed
Nate Begemand3e6b942005-04-05 08:51:15 +0000143 // in registers.
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
Nate Begemand3e6b942005-04-05 08:51:15 +0000146 bool needsLoad = false;
147 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000148
Nate Begemand3e6b942005-04-05 08:51:15 +0000149 switch (ObjectVT) {
150 default: assert(0 && "Unhandled argument type!");
151 case MVT::i1:
152 case MVT::i8:
153 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000154 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000155 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000156 if (GPR_remaining > 0) {
157 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
158 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
159 DAG.getRoot());
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000160 if (ObjectVT != MVT::i64)
Nate Begemand3e6b942005-04-05 08:51:15 +0000161 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
162 } else {
163 needsLoad = true;
164 }
165 break;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000166 case MVT::f32:
167 case MVT::f64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000168 if (FPR_remaining > 0) {
169 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000170 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemand3e6b942005-04-05 08:51:15 +0000171 DAG.getRoot());
172 --FPR_remaining;
173 ++FPR_idx;
174 } else {
175 needsLoad = true;
176 }
177 break;
178 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000179
Nate Begemand3e6b942005-04-05 08:51:15 +0000180 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000181 // that we ran out of physical registers of the appropriate type
Nate Begemand3e6b942005-04-05 08:51:15 +0000182 if (needsLoad) {
183 unsigned SubregOffset = 0;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000184 switch (ObjectVT) {
185 default: assert(0 && "Unhandled argument type!");
186 case MVT::i1:
187 case MVT::i8: SubregOffset = 7; break;
188 case MVT::i16: SubregOffset = 6; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000189 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000190 case MVT::f32: SubregOffset = 4; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000191 case MVT::i64:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000192 case MVT::f64: SubregOffset = 0; break;
193 }
194 int FI = MFI->CreateFixedObject(8, ArgOffset);
195 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000196 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000197 DAG.getConstant(SubregOffset, MVT::i64));
Chris Lattner022ed322005-05-15 19:54:37 +0000198 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
199 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000200 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201
Nate Begemand3e6b942005-04-05 08:51:15 +0000202 // Every 4 bytes of argument space consumes one of the GPRs available for
203 // argument passing.
204 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000205 --GPR_remaining;
206 ++GPR_idx;
Nate Begemand3e6b942005-04-05 08:51:15 +0000207 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000208 ArgOffset += 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000209
Nate Begemand3e6b942005-04-05 08:51:15 +0000210 DAG.setRoot(newroot.getValue(1));
211 ArgValues.push_back(argt);
212 }
213
214 // If the function takes variable number of arguments, make a frame index for
215 // the start of the first vararg value... for expansion of llvm.va_start.
216 if (F.isVarArg()) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000217 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
218 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000219 // If this function is vararg, store any remaining integer argument regs
220 // to their spots on the stack so that they may be loaded by deferencing the
221 // result of va_next.
222 std::vector<SDOperand> MemOps;
223 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
224 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000225 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000226 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000227 Val, FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000228 MemOps.push_back(Store);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000229 // Increment the address by eight for the next argument to store
230 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
Nate Begemand3e6b942005-04-05 08:51:15 +0000231 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
232 }
233 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
234 }
235
236 return ArgValues;
237}
238
239std::pair<SDOperand, SDOperand>
240PPC64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000241 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000242 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000243 SDOperand Callee, ArgListTy &Args,
244 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000245 // args_to_use will accumulate outgoing args for the ISD::CALL case in
246 // SelectExpr to use to put the arguments in the appropriate registers.
247 std::vector<SDOperand> args_to_use;
248
249 // Count how many bytes are to be pushed on the stack, including the linkage
250 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000251 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000252
253 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000254 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000255 DAG.getConstant(NumBytes, getPointerTy()));
256 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000257 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000258
259 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000260 // plus 64 bytes of argument space in case any called code gets funky on us.
Chris Lattner0561b3f2005-08-02 19:26:06 +0000261 // (Required by ABI to support var arg)
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000262 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000263
264 // Adjust the stack pointer for the new arguments...
265 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000266 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000267 DAG.getConstant(NumBytes, getPointerTy()));
268
269 // Set up a copy of the stack pointer for use loading and storing any
270 // arguments that may not fit in the registers available for argument
271 // passing.
272 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
273 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000274
Nate Begemand3e6b942005-04-05 08:51:15 +0000275 // Figure out which arguments are going to go in registers, and which in
276 // memory. Also, if this is a vararg function, floating point operations
277 // must be stored to our stack, and loaded into integer regs as well, if
278 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000279 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000280 unsigned GPR_remaining = 8;
281 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000282
Nate Begemand3e6b942005-04-05 08:51:15 +0000283 std::vector<SDOperand> MemOps;
284 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
285 // PtrOff will be used to store the current argument to the stack if a
286 // register cannot be found for it.
287 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
288 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
289 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000290
Nate Begemand3e6b942005-04-05 08:51:15 +0000291 switch (ArgVT) {
292 default: assert(0 && "Unexpected ValueType for argument!");
293 case MVT::i1:
294 case MVT::i8:
295 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000296 case MVT::i32:
297 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000298 // sign extend, otherwise use a zero extend.
299 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000300 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000301 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000302 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000303 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000304 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000305 if (GPR_remaining > 0) {
306 args_to_use.push_back(Args[i].first);
307 --GPR_remaining;
308 } else {
309 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000310 Args[i].first, PtrOff,
311 DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000312 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000313 ArgOffset += 8;
314 break;
315 case MVT::f32:
316 case MVT::f64:
317 if (FPR_remaining > 0) {
318 args_to_use.push_back(Args[i].first);
319 --FPR_remaining;
320 if (isVarArg) {
321 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000322 Args[i].first, PtrOff,
323 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000324 MemOps.push_back(Store);
325 // Float varargs are always shadowed in available integer registers
326 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000327 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff,
328 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000329 MemOps.push_back(Load);
330 args_to_use.push_back(Load);
331 --GPR_remaining;
332 }
333 } else {
334 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000335 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000336 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000337 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000338 --GPR_remaining;
339 }
340 }
341 } else {
342 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000343 Args[i].first, PtrOff,
344 DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000345 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000346 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000347 break;
348 }
349 }
350 if (!MemOps.empty())
351 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
352 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000353
Nate Begemand3e6b942005-04-05 08:51:15 +0000354 std::vector<MVT::ValueType> RetVals;
355 MVT::ValueType RetTyVT = getValueType(RetTy);
356 if (RetTyVT != MVT::isVoid)
357 RetVals.push_back(RetTyVT);
358 RetVals.push_back(MVT::Other);
359
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000360 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000361 Chain, Callee, args_to_use), 0);
362 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000363 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000364 DAG.getConstant(NumBytes, getPointerTy()));
365 return std::make_pair(TheCall, Chain);
366}
367
368std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000369PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
370 // vastart just stores the address of the VarArgsFrameIndex slot.
371 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
372 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
373 return std::make_pair(Result, Result);
Nate Begemand3e6b942005-04-05 08:51:15 +0000374}
375
376std::pair<SDOperand,SDOperand> PPC64TargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000377LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemand3e6b942005-04-05 08:51:15 +0000378 const Type *ArgTy, SelectionDAG &DAG) {
379 MVT::ValueType ArgVT = getValueType(ArgTy);
380 SDOperand Result;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000381 SDOperand Val = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
382 Result = DAG.getLoad(ArgVT, Val.getValue(1), Val, DAG.getSrcValue(NULL));
383 Val = DAG.getNode(ISD::ADD, VAList.getValueType(), Val,
384 DAG.getConstant(8, VAList.getValueType()));
385 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
386 Val, VAList, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000387 return std::make_pair(Result, Chain);
388}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000389
Nate Begemand3e6b942005-04-05 08:51:15 +0000390std::pair<SDOperand, SDOperand> PPC64TargetLowering::
391LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
392 SelectionDAG &DAG) {
393 assert(0 && "LowerFrameReturnAddress unimplemented");
394 abort();
395}
396
397namespace {
398Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
399Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
400//===--------------------------------------------------------------------===//
Chris Lattner0561b3f2005-08-02 19:26:06 +0000401/// ISel - PPC64 specific code to select PPC64 machine instructions for
Nate Begemand3e6b942005-04-05 08:51:15 +0000402/// SelectionDAG operations.
403//===--------------------------------------------------------------------===//
404class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000405
Nate Begemand3e6b942005-04-05 08:51:15 +0000406 /// Comment Here.
407 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000408
Nate Begemand3e6b942005-04-05 08:51:15 +0000409 /// ExprMap - As shared expressions are codegen'd, we keep track of which
410 /// vreg the value is produced in, so we only emit one copy of each compiled
411 /// tree.
412 std::map<SDOperand, unsigned> ExprMap;
413
414 unsigned GlobalBaseReg;
415 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416
Nate Begemand3e6b942005-04-05 08:51:15 +0000417public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000418 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000419 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000420
Nate Begemand3e6b942005-04-05 08:51:15 +0000421 /// runOnFunction - Override this function in order to reset our per-function
422 /// variables.
423 virtual bool runOnFunction(Function &Fn) {
424 // Make sure we re-emit a set of the global base reg if necessary
425 GlobalBaseInitialized = false;
426 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000427 }
428
Nate Begemand3e6b942005-04-05 08:51:15 +0000429 /// InstructionSelectBasicBlock - This callback is invoked by
430 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
431 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
432 DEBUG(BB->dump());
433 // Codegen the basic block.
434 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000435
Nate Begemand3e6b942005-04-05 08:51:15 +0000436 // Clear state used for selection.
437 ExprMap.clear();
438 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000439
Nate Begemand3e6b942005-04-05 08:51:15 +0000440 unsigned getGlobalBaseReg();
441 unsigned getConstDouble(double floatVal, unsigned Result);
442 unsigned SelectSetCR0(SDOperand CC);
443 unsigned SelectExpr(SDOperand N);
444 unsigned SelectExprFP(SDOperand N, unsigned Result);
445 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000446
Nate Begemand3e6b942005-04-05 08:51:15 +0000447 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
448 void SelectBranchCC(SDOperand N);
449};
450
Nate Begemand3e6b942005-04-05 08:51:15 +0000451/// getImmediateForOpcode - This method returns a value indicating whether
452/// the ConstantSDNode N can be used as an immediate to Opcode. The return
453/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000454/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000455/// of 1 indicates that the constant may be used in normal immediate form. A
456/// return value of 2 indicates that the constant may be used in shifted
457/// immediate form. A return value of 3 indicates that log base 2 of the
458/// constant may be used.
459///
460static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
461 unsigned& Imm, bool U = false) {
462 if (N.getOpcode() != ISD::Constant) return 0;
463
464 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000465
Nate Begemand3e6b942005-04-05 08:51:15 +0000466 switch(Opcode) {
467 default: return 0;
468 case ISD::ADD:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000469 if (isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemand3e6b942005-04-05 08:51:15 +0000470 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
471 break;
472 case ISD::AND:
473 case ISD::XOR:
474 case ISD::OR:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000475 if (isUInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemand3e6b942005-04-05 08:51:15 +0000476 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
477 break;
478 case ISD::MUL:
479 case ISD::SUB:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000480 if (isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemand3e6b942005-04-05 08:51:15 +0000481 break;
482 case ISD::SETCC:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000483 if (U && isUInt16(v)) { Imm = v & 0xFFFF; return 1; }
484 if (!U && isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemand3e6b942005-04-05 08:51:15 +0000485 break;
486 case ISD::SDIV:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000487 if (isPowerOf2_32(v)) { Imm = Log2_32(v); return 3; }
Nate Begemand3e6b942005-04-05 08:51:15 +0000488 break;
489 }
490 return 0;
491}
492
493/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
494/// to Condition. If the Condition is unordered or unsigned, the bool argument
495/// U is set to true, otherwise it is set to false.
496static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
497 U = false;
498 switch (Condition) {
499 default: assert(0 && "Unknown condition!"); abort();
500 case ISD::SETEQ: return PPC::BEQ;
501 case ISD::SETNE: return PPC::BNE;
502 case ISD::SETULT: U = true;
503 case ISD::SETLT: return PPC::BLT;
504 case ISD::SETULE: U = true;
505 case ISD::SETLE: return PPC::BLE;
506 case ISD::SETUGT: U = true;
507 case ISD::SETGT: return PPC::BGT;
508 case ISD::SETUGE: U = true;
509 case ISD::SETGE: return PPC::BGE;
510 }
511 return 0;
512}
513
514/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
515/// and store immediate instructions.
516static unsigned IndexedOpForOp(unsigned Opcode) {
517 switch(Opcode) {
518 default: assert(0 && "Unknown opcode!"); abort();
519 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
520 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
521 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
522 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
523 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
524 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
525 case PPC::LFD: return PPC::LFDX;
526 }
527 return 0;
528}
529}
530
531/// getGlobalBaseReg - Output the instructions required to put the
532/// base address to use for accessing globals into a register.
533///
534unsigned ISel::getGlobalBaseReg() {
535 if (!GlobalBaseInitialized) {
536 // Insert the set of GlobalBaseReg into the first MBB of the function
537 MachineBasicBlock &FirstMBB = BB->getParent()->front();
538 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
539 GlobalBaseReg = MakeReg(MVT::i64);
540 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
541 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
542 GlobalBaseInitialized = true;
543 }
544 return GlobalBaseReg;
545}
546
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000547/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000548/// Constant Pool. Optionally takes a register in which to load the value.
549unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000550 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000551 if (0 == Result) Result = MakeReg(MVT::f64);
552 MachineConstantPool *CP = BB->getParent()->getConstantPool();
553 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
554 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000555 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
Nate Begemand3e6b942005-04-05 08:51:15 +0000556 .addConstantPoolIndex(CPI);
557 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
558 return Result;
559}
560
561unsigned ISel::SelectSetCR0(SDOperand CC) {
562 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000563 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000564 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000565
Nate Begemand3e6b942005-04-05 08:51:15 +0000566 // If the first operand to the select is a SETCC node, then we can fold it
567 // into the branch that selects which value to return.
Chris Lattner88ac32c2005-08-09 20:21:10 +0000568 if (CC.getOpcode() == ISD::SETCC) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000569 bool U;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000570 Opc = getBCCForSetCC(cast<CondCodeSDNode>(CC.getOperand(2))->get(), U);
571 Tmp1 = SelectExpr(CC.getOperand(0));
Nate Begemand3e6b942005-04-05 08:51:15 +0000572
573 // Pass the optional argument U to getImmediateForOpcode for SETCC,
574 // so that it knows whether the SETCC immediate range is signed or not.
Chris Lattner88ac32c2005-08-09 20:21:10 +0000575 if (1 == getImmediateForOpcode(CC.getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000576 Tmp2, U)) {
577 if (U)
578 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
579 else
580 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
581 } else {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000582 bool IsInteger = MVT::isInteger(CC.getOperand(0).getValueType());
Nate Begemand3e6b942005-04-05 08:51:15 +0000583 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Chris Lattner88ac32c2005-08-09 20:21:10 +0000584 Tmp2 = SelectExpr(CC.getOperand(1));
Nate Begemand3e6b942005-04-05 08:51:15 +0000585 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
586 }
587 } else {
588 Tmp1 = SelectExpr(CC);
589 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
590 Opc = PPC::BNE;
591 }
592 return Opc;
593}
594
595/// Check to see if the load is a constant offset from a base register
596bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
597{
598 unsigned imm = 0, opcode = N.getOpcode();
599 if (N.getOpcode() == ISD::ADD) {
600 Reg = SelectExpr(N.getOperand(0));
601 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
602 offset = imm;
603 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000604 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000605 offset = SelectExpr(N.getOperand(1));
606 return true;
607 }
608 Reg = SelectExpr(N);
609 offset = 0;
610 return false;
611}
612
613void ISel::SelectBranchCC(SDOperand N)
614{
615 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000616 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000617 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
618
619 // Get the MBB we will fall through to so that we can hand it off to the
620 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
621 //ilist<MachineBasicBlock>::iterator It = BB;
622 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000623
Nate Begemand3e6b942005-04-05 08:51:15 +0000624 Select(N.getOperand(0)); //chain
625 unsigned Opc = SelectSetCR0(N.getOperand(1));
626 // FIXME: Use this once we have something approximating two-way branches
627 // We cannot currently use this in case the ISel hands us something like
628 // BRcc MBBx
629 // BR MBBy
630 // since the fallthrough basic block for the conditional branch does not start
631 // with the unconditional branch (it is skipped over).
632 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
633 // .addMBB(Dest).addMBB(Fallthrough);
634 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
635 return;
636}
637
638unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
639{
640 unsigned Tmp1, Tmp2, Tmp3;
641 unsigned Opc = 0;
642 SDNode *Node = N.Val;
643 MVT::ValueType DestType = N.getValueType();
644 unsigned opcode = N.getOpcode();
645
646 switch (opcode) {
647 default:
648 Node->dump();
649 assert(0 && "Node not handled!\n");
650
651 case ISD::SELECT: {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000652 SDNode *Cond = N.getOperand(0).Val;
Nate Begemand3e6b942005-04-05 08:51:15 +0000653 // Attempt to generate FSEL. We can do this whenever we have an FP result,
654 // and an FP comparison in the SetCC node.
Chris Lattner88ac32c2005-08-09 20:21:10 +0000655 if (Cond->getOpcode() == ISD::SETCC &&
656 !MVT::isInteger(N.getOperand(1).getValueType()) &&
657 cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETEQ &&
658 cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETNE) {
659 MVT::ValueType VT = Cond->getOperand(0).getValueType();
660 ISD::CondCode CC = cast<CondCodeSDNode>(Cond->getOperand(2))->get();
661 Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against
Nate Begemand3e6b942005-04-05 08:51:15 +0000662 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
663 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000664
Chris Lattner88ac32c2005-08-09 20:21:10 +0000665 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Cond->getOperand(1));
Nate Begemand3e6b942005-04-05 08:51:15 +0000666 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000667 switch(CC) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000668 default: assert(0 && "Invalid FSEL condition"); abort();
669 case ISD::SETULT:
670 case ISD::SETLT:
671 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
672 return Result;
673 case ISD::SETUGE:
674 case ISD::SETGE:
675 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
676 return Result;
677 case ISD::SETUGT:
678 case ISD::SETGT: {
679 Tmp2 = MakeReg(VT);
680 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
681 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
682 return Result;
683 }
684 case ISD::SETULE:
685 case ISD::SETLE: {
686 Tmp2 = MakeReg(VT);
687 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
688 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
689 return Result;
690 }
691 }
692 } else {
693 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000694 Tmp2 = SelectExpr(Cond->getOperand(1));
Nate Begemand3e6b942005-04-05 08:51:15 +0000695 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +0000696 switch(CC) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000697 default: assert(0 && "Invalid FSEL condition"); abort();
698 case ISD::SETULT:
699 case ISD::SETLT:
700 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
701 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
702 return Result;
703 case ISD::SETUGE:
704 case ISD::SETGE:
705 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
706 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
707 return Result;
708 case ISD::SETUGT:
709 case ISD::SETGT:
710 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
711 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
712 return Result;
713 case ISD::SETULE:
714 case ISD::SETLE:
715 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
716 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
717 return Result;
718 }
719 }
720 assert(0 && "Should never get here");
721 return 0;
722 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000723
Nate Begemand3e6b942005-04-05 08:51:15 +0000724 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
725 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
726 Opc = SelectSetCR0(N.getOperand(0));
727
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000728 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000729 // value and the MBB to hold the PHI instruction for this SetCC.
730 MachineBasicBlock *thisMBB = BB;
731 const BasicBlock *LLVM_BB = BB->getBasicBlock();
732 ilist<MachineBasicBlock>::iterator It = BB;
733 ++It;
734
735 // thisMBB:
736 // ...
737 // TrueVal = ...
738 // cmpTY cr0, r1, r2
739 // bCC copy1MBB
740 // fallthrough --> copy0MBB
741 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
742 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
743 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
744 MachineFunction *F = BB->getParent();
745 F->getBasicBlockList().insert(It, copy0MBB);
746 F->getBasicBlockList().insert(It, sinkMBB);
747 // Update machine-CFG edges
748 BB->addSuccessor(copy0MBB);
749 BB->addSuccessor(sinkMBB);
750
751 // copy0MBB:
752 // %FalseValue = ...
753 // # fallthrough to sinkMBB
754 BB = copy0MBB;
755 // Update machine-CFG edges
756 BB->addSuccessor(sinkMBB);
757
758 // sinkMBB:
759 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
760 // ...
761 BB = sinkMBB;
762 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
763 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
764 return Result;
765 }
766
767 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000768 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000769 ISD::ADD == N.getOperand(0).getOpcode() &&
770 N.getOperand(0).Val->hasOneUse() &&
771 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
772 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
773 ++FusedFP; // Statistic
774 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
775 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
776 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
777 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
778 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000779 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000780 ISD::SUB == N.getOperand(0).getOpcode() &&
781 N.getOperand(0).Val->hasOneUse() &&
782 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
783 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
784 ++FusedFP; // Statistic
785 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
786 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
787 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
788 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
789 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
790 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
791 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
792 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
793 } else {
794 Tmp1 = SelectExpr(N.getOperand(0));
795 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
796 }
797 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000798
Nate Begemand3e6b942005-04-05 08:51:15 +0000799 case ISD::FABS:
800 Tmp1 = SelectExpr(N.getOperand(0));
801 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
802 return Result;
803
804 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000805 assert (DestType == MVT::f32 &&
806 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000807 "only f64 to f32 conversion supported here");
808 Tmp1 = SelectExpr(N.getOperand(0));
809 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
810 return Result;
811
812 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000813 assert (DestType == MVT::f64 &&
814 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000815 "only f32 to f64 conversion supported here");
816 Tmp1 = SelectExpr(N.getOperand(0));
817 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
818 return Result;
819
820 case ISD::CopyFromReg:
821 if (Result == 1)
822 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
823 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
824 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
825 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000826
Nate Begemand3e6b942005-04-05 08:51:15 +0000827 case ISD::ConstantFP: {
828 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
829 Result = getConstDouble(CN->getValue(), Result);
830 return Result;
831 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000832
Nate Begemand3e6b942005-04-05 08:51:15 +0000833 case ISD::ADD:
834 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
835 N.getOperand(0).Val->hasOneUse()) {
836 ++FusedFP; // Statistic
837 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
838 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
839 Tmp3 = SelectExpr(N.getOperand(1));
840 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
841 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
842 return Result;
843 }
844 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
845 Tmp1 = SelectExpr(N.getOperand(0));
846 Tmp2 = SelectExpr(N.getOperand(1));
847 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
848 return Result;
849
850 case ISD::SUB:
851 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
852 N.getOperand(0).Val->hasOneUse()) {
853 ++FusedFP; // Statistic
854 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
855 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
856 Tmp3 = SelectExpr(N.getOperand(1));
857 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
858 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
859 return Result;
860 }
861 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
862 Tmp1 = SelectExpr(N.getOperand(0));
863 Tmp2 = SelectExpr(N.getOperand(1));
864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
865 return Result;
866
867 case ISD::MUL:
868 case ISD::SDIV:
869 switch( opcode ) {
870 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
871 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
872 };
873 Tmp1 = SelectExpr(N.getOperand(0));
874 Tmp2 = SelectExpr(N.getOperand(1));
875 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
876 return Result;
877
878 case ISD::UINT_TO_FP:
879 case ISD::SINT_TO_FP: {
880 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
881 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
882 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
883 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
884 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000885
Nate Begemand3e6b942005-04-05 08:51:15 +0000886 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
887 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000888
Nate Begemand3e6b942005-04-05 08:51:15 +0000889 // FIXME: pull this FP constant generation stuff out into something like
890 // the simple ISel's getReg.
891 if (IsUnsigned) {
892 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
893 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
894 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
895 } else {
896 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
897 unsigned CPI = CP->getConstantPoolIndex(CFP);
898 // Load constant fp value
899 unsigned Tmp4 = MakeReg(MVT::i32);
900 unsigned TmpL = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +0000901 BuildMI(BB, PPC::ADDIS, 2, Tmp4).addReg(getGlobalBaseReg())
Nate Begemand3e6b942005-04-05 08:51:15 +0000902 .addConstantPoolIndex(CPI);
903 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
904 // Store the hi & low halves of the fp value, currently in int regs
905 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
906 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
907 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
908 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
909 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
910 // Generate the return value with a subtract
911 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
912 }
913 return Result;
914 }
915 }
916 assert(0 && "Should never get here");
917 return 0;
918}
919
920unsigned ISel::SelectExpr(SDOperand N) {
921 unsigned Result;
922 unsigned Tmp1, Tmp2, Tmp3;
923 unsigned Opc = 0;
924 unsigned opcode = N.getOpcode();
925
926 SDNode *Node = N.Val;
927 MVT::ValueType DestType = N.getValueType();
928
929 unsigned &Reg = ExprMap[N];
930 if (Reg) return Reg;
931
932 switch (N.getOpcode()) {
933 default:
934 Reg = Result = (N.getValueType() != MVT::Other) ?
935 MakeReg(N.getValueType()) : 1;
936 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000937 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +0000938 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);
Nate Begeman2497e632005-07-21 20:44:43 +0000994 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
Nate Begemand3e6b942005-04-05 08:51:15 +0000995 .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);
Nate Begeman2497e632005-07-21 20:44:43 +00001007 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
Nate Begemand3e6b942005-04-05 08:51:15 +00001008 .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) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001022 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begemand3e6b942005-04-05 08:51:15 +00001023 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();
Nate Begeman2497e632005-07-21 20:44:43 +00001049 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
Nate Begemand3e6b942005-04-05 08:51:15 +00001050 .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
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001069 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +00001070 case ISD::CALL: {
1071 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001072 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001073 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1074 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1075 };
1076 static const unsigned FPR[] = {
1077 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1078 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1079 };
1080
1081 // Lower the chain for this call.
1082 Select(N.getOperand(0));
1083 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1084
1085 MachineInstr *CallMI;
1086 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001087 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001088 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001089 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001090 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001091 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001092 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001093 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001094 true);
1095 } else {
1096 Tmp1 = SelectExpr(N.getOperand(1));
1097 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1098 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1099 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1100 .addReg(PPC::R12);
1101 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001102
Nate Begemand3e6b942005-04-05 08:51:15 +00001103 // Load the register args to virtual regs
1104 std::vector<unsigned> ArgVR;
1105 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1106 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1107
1108 // Copy the virtual registers into the appropriate argument register
1109 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1110 switch(N.getOperand(i+2).getValueType()) {
1111 default: Node->dump(); assert(0 && "Unknown value type for call");
1112 case MVT::i1:
1113 case MVT::i8:
1114 case MVT::i16:
1115 case MVT::i32:
1116 case MVT::i64:
1117 assert(GPR_idx < 8 && "Too many int args");
1118 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1119 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1120 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1121 }
1122 ++GPR_idx;
1123 break;
1124 case MVT::f64:
1125 case MVT::f32:
1126 assert(FPR_idx < 13 && "Too many fp args");
1127 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1128 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1129 ++FPR_idx;
1130 break;
1131 }
1132 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001133
Nate Begemand3e6b942005-04-05 08:51:15 +00001134 // Put the call instruction in the correct place in the MachineBasicBlock
1135 BB->push_back(CallMI);
1136
1137 switch (Node->getValueType(0)) {
1138 default: assert(0 && "Unknown value type for call result!");
1139 case MVT::Other: return 1;
1140 case MVT::i1:
1141 case MVT::i8:
1142 case MVT::i16:
1143 case MVT::i32:
1144 case MVT::i64:
1145 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1146 break;
1147 case MVT::f32:
1148 case MVT::f64:
1149 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1150 break;
1151 }
1152 return Result+N.ResNo;
1153 }
1154
1155 case ISD::SIGN_EXTEND:
1156 case ISD::SIGN_EXTEND_INREG:
1157 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001158 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001159 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1160 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001161 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001162 break;
1163 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001164 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001165 break;
1166 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001167 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001168 break;
1169 case MVT::i1:
1170 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1171 break;
1172 }
1173 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001174
Nate Begemand3e6b942005-04-05 08:51:15 +00001175 case ISD::CopyFromReg:
1176 if (Result == 1)
1177 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1178 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1179 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1180 return Result;
1181
1182 case ISD::SHL:
1183 Tmp1 = SelectExpr(N.getOperand(0));
1184 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001185 Tmp2 = CN->getValue() & 0x3F;
1186 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1187 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001188 } else {
1189 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001190 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001191 }
1192 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001193
Nate Begemand3e6b942005-04-05 08:51:15 +00001194 case ISD::SRL:
1195 Tmp1 = SelectExpr(N.getOperand(0));
1196 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001197 Tmp2 = CN->getValue() & 0x3F;
1198 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1199 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001200 } else {
1201 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001202 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001203 }
1204 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001205
Nate Begemand3e6b942005-04-05 08:51:15 +00001206 case ISD::SRA:
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001209 Tmp2 = CN->getValue() & 0x3F;
1210 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001211 } else {
1212 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001213 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001214 }
1215 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001216
Nate Begemand3e6b942005-04-05 08:51:15 +00001217 case ISD::ADD:
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1220 default: assert(0 && "unhandled result code");
1221 case 0: // No immediate
1222 Tmp2 = SelectExpr(N.getOperand(1));
1223 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1224 break;
1225 case 1: // Low immediate
1226 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1227 break;
1228 case 2: // Shifted immediate
1229 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1230 break;
1231 }
1232 return Result;
1233
1234 case ISD::AND:
1235 case ISD::OR:
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1238 default: assert(0 && "unhandled result code");
1239 case 0: // No immediate
1240 Tmp2 = SelectExpr(N.getOperand(1));
1241 switch (opcode) {
1242 case ISD::AND: Opc = PPC::AND; break;
1243 case ISD::OR: Opc = PPC::OR; break;
1244 }
1245 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 break;
1247 case 1: // Low immediate
1248 switch (opcode) {
1249 case ISD::AND: Opc = PPC::ANDIo; break;
1250 case ISD::OR: Opc = PPC::ORI; break;
1251 }
1252 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1253 break;
1254 case 2: // Shifted immediate
1255 switch (opcode) {
1256 case ISD::AND: Opc = PPC::ANDISo; break;
1257 case ISD::OR: Opc = PPC::ORIS; break;
1258 }
1259 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1260 break;
1261 }
1262 return Result;
1263
1264 case ISD::XOR: {
1265 // Check for EQV: xor, (xor a, -1), b
1266 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1267 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1268 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1269 ++NotLogic;
1270 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1271 Tmp2 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1273 return Result;
1274 }
1275 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1276 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1277 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1278 ++NotLogic;
1279 switch(N.getOperand(0).getOpcode()) {
1280 case ISD::OR:
1281 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1282 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1283 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 break;
1285 case ISD::AND:
1286 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1288 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1289 break;
1290 default:
1291 Tmp1 = SelectExpr(N.getOperand(0));
1292 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1293 break;
1294 }
1295 return Result;
1296 }
1297 Tmp1 = SelectExpr(N.getOperand(0));
1298 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1299 default: assert(0 && "unhandled result code");
1300 case 0: // No immediate
1301 Tmp2 = SelectExpr(N.getOperand(1));
1302 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1303 break;
1304 case 1: // Low immediate
1305 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1306 break;
1307 case 2: // Shifted immediate
1308 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1309 break;
1310 }
1311 return Result;
1312 }
1313
1314 case ISD::SUB:
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1317 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1318 else {
1319 Tmp1 = SelectExpr(N.getOperand(0));
1320 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1321 }
1322 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001323
Nate Begemand3e6b942005-04-05 08:51:15 +00001324 case ISD::MUL:
1325 Tmp1 = SelectExpr(N.getOperand(0));
1326 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1327 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1328 else {
1329 Tmp2 = SelectExpr(N.getOperand(1));
1330 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1331 }
1332 return Result;
1333
1334 case ISD::SDIV:
1335 case ISD::UDIV:
1336 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1337 Tmp1 = MakeReg(MVT::i64);
1338 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001339 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001340 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1341 return Result;
1342 }
1343 Tmp1 = SelectExpr(N.getOperand(0));
1344 Tmp2 = SelectExpr(N.getOperand(1));
1345 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1346 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1347 return Result;
1348
Nate Begemand3e6b942005-04-05 08:51:15 +00001349 case ISD::FP_TO_UINT:
1350 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001351 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001352 Tmp2 = MakeReg(MVT::f64);
1353 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1354 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1355 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1356 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1357 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001358 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001359
Chris Lattner88ac32c2005-08-09 20:21:10 +00001360 case ISD::SETCC: {
1361 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001362
Chris Lattner88ac32c2005-08-09 20:21:10 +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);
Nate Begemand3e6b942005-04-05 08:51:15 +00001367
Chris Lattner88ac32c2005-08-09 20:21:10 +00001368 // Create an iterator with which to insert the MBB for copying the false
1369 // 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
Chris Lattner88ac32c2005-08-09 20:21:10 +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);
Nate Begemand3e6b942005-04-05 08:51:15 +00001389
Chris Lattner88ac32c2005-08-09 20:21:10 +00001390 // copy0MBB:
1391 // %FalseValue = li 0
1392 // fallthrough
1393 BB = copy0MBB;
1394 // Update machine-CFG edges
1395 BB->addSuccessor(sinkMBB);
Nate Begemand3e6b942005-04-05 08:51:15 +00001396
Chris Lattner88ac32c2005-08-09 20:21:10 +00001397 // 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 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001405
Nate Begemand3e6b942005-04-05 08:51:15 +00001406 case ISD::SELECT: {
1407 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1408 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1409 Opc = SelectSetCR0(N.getOperand(0));
1410
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001411 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001412 // value and the MBB to hold the PHI instruction for this SetCC.
1413 MachineBasicBlock *thisMBB = BB;
1414 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1415 ilist<MachineBasicBlock>::iterator It = BB;
1416 ++It;
1417
1418 // thisMBB:
1419 // ...
1420 // TrueVal = ...
1421 // cmpTY cr0, r1, r2
1422 // bCC copy1MBB
1423 // fallthrough --> copy0MBB
1424 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1425 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1426 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1427 MachineFunction *F = BB->getParent();
1428 F->getBasicBlockList().insert(It, copy0MBB);
1429 F->getBasicBlockList().insert(It, sinkMBB);
1430 // Update machine-CFG edges
1431 BB->addSuccessor(copy0MBB);
1432 BB->addSuccessor(sinkMBB);
1433
1434 // copy0MBB:
1435 // %FalseValue = ...
1436 // # fallthrough to sinkMBB
1437 BB = copy0MBB;
1438 // Update machine-CFG edges
1439 BB->addSuccessor(sinkMBB);
1440
1441 // sinkMBB:
1442 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1443 // ...
1444 BB = sinkMBB;
1445 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1446 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1447
1448 // FIXME: Select i64?
1449 return Result;
1450 }
1451
1452 case ISD::Constant:
1453 switch (N.getValueType()) {
1454 default: assert(0 && "Cannot use constants of this type!");
1455 case MVT::i1:
1456 BuildMI(BB, PPC::LI, 1, Result)
1457 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1458 break;
1459 case MVT::i32:
1460 {
1461 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1462 if (v < 32768 && v >= -32768) {
1463 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1464 } else {
1465 Tmp1 = MakeReg(MVT::i32);
1466 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1467 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1468 }
1469 }
1470 }
1471 return Result;
1472 }
1473
1474 return 0;
1475}
1476
1477void ISel::Select(SDOperand N) {
1478 unsigned Tmp1, Tmp2, Opc;
1479 unsigned opcode = N.getOpcode();
1480
1481 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1482 return; // Already selected.
1483
1484 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001485
Nate Begemand3e6b942005-04-05 08:51:15 +00001486 switch (Node->getOpcode()) {
1487 default:
1488 Node->dump(); std::cerr << "\n";
1489 assert(0 && "Node not handled yet!");
1490 case ISD::EntryToken: return; // Noop
1491 case ISD::TokenFactor:
1492 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1493 Select(Node->getOperand(i));
1494 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001495 case ISD::CALLSEQ_START:
1496 case ISD::CALLSEQ_END:
Nate Begemand3e6b942005-04-05 08:51:15 +00001497 Select(N.getOperand(0));
1498 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001499 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemand3e6b942005-04-05 08:51:15 +00001500 PPC::ADJCALLSTACKUP;
1501 BuildMI(BB, Opc, 1).addImm(Tmp1);
1502 return;
1503 case ISD::BR: {
1504 MachineBasicBlock *Dest =
1505 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1506 Select(N.getOperand(0));
1507 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1508 return;
1509 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001510 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001511 SelectBranchCC(N);
1512 return;
1513 case ISD::CopyToReg:
1514 Select(N.getOperand(0));
1515 Tmp1 = SelectExpr(N.getOperand(1));
1516 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001517
Nate Begemand3e6b942005-04-05 08:51:15 +00001518 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001519 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001520 N.getOperand(1).getValueType() == MVT::f32)
1521 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1522 else
1523 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1524 }
1525 return;
1526 case ISD::ImplicitDef:
1527 Select(N.getOperand(0));
1528 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1529 return;
1530 case ISD::RET:
1531 switch (N.getNumOperands()) {
1532 default:
1533 assert(0 && "Unknown return instruction!");
1534 case 3:
1535 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1536 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001537 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001538 Select(N.getOperand(0));
1539 Tmp1 = SelectExpr(N.getOperand(1));
1540 Tmp2 = SelectExpr(N.getOperand(2));
1541 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1542 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1543 break;
1544 case 2:
1545 Select(N.getOperand(0));
1546 Tmp1 = SelectExpr(N.getOperand(1));
1547 switch (N.getOperand(1).getValueType()) {
1548 default:
1549 assert(0 && "Unknown return type!");
1550 case MVT::f64:
1551 case MVT::f32:
1552 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1553 break;
1554 case MVT::i32:
1555 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1556 break;
1557 }
1558 case 1:
1559 Select(N.getOperand(0));
1560 break;
1561 }
1562 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1563 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001564 case ISD::TRUNCSTORE:
1565 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001566 {
1567 SDOperand Chain = N.getOperand(0);
1568 SDOperand Value = N.getOperand(1);
1569 SDOperand Address = N.getOperand(2);
1570 Select(Chain);
1571
1572 Tmp1 = SelectExpr(Value); //value
1573
1574 if (opcode == ISD::STORE) {
1575 switch(Value.getValueType()) {
1576 default: assert(0 && "unknown Type in store");
1577 case MVT::i64: Opc = PPC::STD; break;
1578 case MVT::f64: Opc = PPC::STFD; break;
1579 case MVT::f32: Opc = PPC::STFS; break;
1580 }
1581 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001582 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001583 default: assert(0 && "unknown Type in store");
1584 case MVT::i1: //FIXME: DAG does not promote this load
1585 case MVT::i8: Opc= PPC::STB; break;
1586 case MVT::i16: Opc = PPC::STH; break;
1587 case MVT::i32: Opc = PPC::STW; break;
1588 }
1589 }
1590
1591 if(Address.getOpcode() == ISD::FrameIndex)
1592 {
1593 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1594 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1595 }
1596 else
1597 {
1598 int offset;
1599 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001600 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001601 Opc = IndexedOpForOp(Opc);
1602 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1603 } else {
1604 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1605 }
1606 }
1607 return;
1608 }
1609 case ISD::EXTLOAD:
1610 case ISD::SEXTLOAD:
1611 case ISD::ZEXTLOAD:
1612 case ISD::LOAD:
1613 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001614 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +00001615 case ISD::CALL:
1616 case ISD::DYNAMIC_STACKALLOC:
1617 ExprMap.erase(N);
1618 SelectExpr(N);
1619 return;
1620 }
1621 assert(0 && "Should not be reached!");
1622}
1623
1624
Chris Lattner0561b3f2005-08-02 19:26:06 +00001625/// createPPC64PatternInstructionSelector - This pass converts an LLVM function
Nate Begemand3e6b942005-04-05 08:51:15 +00001626/// into a machine code representation using pattern matching and a machine
1627/// description file.
1628///
1629FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001630 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001631}
1632