blob: 20fd7244447b037df5099629bcaccdcba0622cb1 [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 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
Andrew Lenharth558bc882005-06-18 18:34:52 +0000107 virtual std::pair<SDOperand,SDOperand>
108 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
109 SelectionDAG &DAG);
110
Nate Begemand3e6b942005-04-05 08:51:15 +0000111 virtual std::pair<SDOperand, SDOperand>
112 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
113 SelectionDAG &DAG);
114 };
115}
116
117
118std::vector<SDOperand>
119PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
120 //
121 // add beautiful description of PPC stack frame format, or at least some docs
122 //
123 MachineFunction &MF = DAG.getMachineFunction();
124 MachineFrameInfo *MFI = MF.getFrameInfo();
125 MachineBasicBlock& BB = MF.front();
126 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000127
128 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemand3e6b942005-04-05 08:51:15 +0000129 // fixed size array of physical args, for the sake of simplicity let the STL
130 // handle tracking them for us.
131 std::vector<unsigned> argVR, argPR, argOp;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000132 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000133 unsigned GPR_remaining = 8;
134 unsigned FPR_remaining = 13;
135 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000136 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +0000137 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
138 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
139 };
140 static const unsigned FPR[] = {
141 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
142 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
143 };
144
145 // Add DAG nodes to load the arguments... On entry to a function on PPC,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000146 // the arguments start at offset 48, although they are likely to be passed
Nate Begemand3e6b942005-04-05 08:51:15 +0000147 // in registers.
148 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
149 SDOperand newroot, argt;
Nate Begemand3e6b942005-04-05 08:51:15 +0000150 bool needsLoad = false;
151 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000152
Nate Begemand3e6b942005-04-05 08:51:15 +0000153 switch (ObjectVT) {
154 default: assert(0 && "Unhandled argument type!");
155 case MVT::i1:
156 case MVT::i8:
157 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000158 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000159 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000160 if (GPR_remaining > 0) {
161 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
162 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
163 DAG.getRoot());
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000164 if (ObjectVT != MVT::i64)
Nate Begemand3e6b942005-04-05 08:51:15 +0000165 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
166 } else {
167 needsLoad = true;
168 }
169 break;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000170 case MVT::f32:
171 case MVT::f64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000172 if (FPR_remaining > 0) {
173 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000174 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemand3e6b942005-04-05 08:51:15 +0000175 DAG.getRoot());
176 --FPR_remaining;
177 ++FPR_idx;
178 } else {
179 needsLoad = true;
180 }
181 break;
182 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000183
Nate Begemand3e6b942005-04-05 08:51:15 +0000184 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000185 // that we ran out of physical registers of the appropriate type
Nate Begemand3e6b942005-04-05 08:51:15 +0000186 if (needsLoad) {
187 unsigned SubregOffset = 0;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000188 switch (ObjectVT) {
189 default: assert(0 && "Unhandled argument type!");
190 case MVT::i1:
191 case MVT::i8: SubregOffset = 7; break;
192 case MVT::i16: SubregOffset = 6; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000193 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000194 case MVT::f32: SubregOffset = 4; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000195 case MVT::i64:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000196 case MVT::f64: SubregOffset = 0; break;
197 }
198 int FI = MFI->CreateFixedObject(8, ArgOffset);
199 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000200 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000201 DAG.getConstant(SubregOffset, MVT::i64));
Chris Lattner022ed322005-05-15 19:54:37 +0000202 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
203 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000204 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000205
Nate Begemand3e6b942005-04-05 08:51:15 +0000206 // Every 4 bytes of argument space consumes one of the GPRs available for
207 // argument passing.
208 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000209 --GPR_remaining;
210 ++GPR_idx;
Nate Begemand3e6b942005-04-05 08:51:15 +0000211 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000212 ArgOffset += 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000213
Nate Begemand3e6b942005-04-05 08:51:15 +0000214 DAG.setRoot(newroot.getValue(1));
215 ArgValues.push_back(argt);
216 }
217
218 // If the function takes variable number of arguments, make a frame index for
219 // the start of the first vararg value... for expansion of llvm.va_start.
220 if (F.isVarArg()) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000221 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
222 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000223 // If this function is vararg, store any remaining integer argument regs
224 // to their spots on the stack so that they may be loaded by deferencing the
225 // result of va_next.
226 std::vector<SDOperand> MemOps;
227 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
228 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000229 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000230 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000231 Val, FIN, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000232 MemOps.push_back(Store);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000233 // Increment the address by eight for the next argument to store
234 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
Nate Begemand3e6b942005-04-05 08:51:15 +0000235 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
236 }
237 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
238 }
239
240 return ArgValues;
241}
242
243std::pair<SDOperand, SDOperand>
244PPC64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000245 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000246 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000247 SDOperand Callee, ArgListTy &Args,
248 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000249 // args_to_use will accumulate outgoing args for the ISD::CALL case in
250 // SelectExpr to use to put the arguments in the appropriate registers.
251 std::vector<SDOperand> args_to_use;
252
253 // Count how many bytes are to be pushed on the stack, including the linkage
254 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000255 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000256
257 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000258 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000259 DAG.getConstant(NumBytes, getPointerTy()));
260 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000261 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000262
263 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000264 // plus 64 bytes of argument space in case any called code gets funky on us.
265 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000266
267 // Adjust the stack pointer for the new arguments...
268 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000269 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000270 DAG.getConstant(NumBytes, getPointerTy()));
271
272 // Set up a copy of the stack pointer for use loading and storing any
273 // arguments that may not fit in the registers available for argument
274 // passing.
275 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
276 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000277
Nate Begemand3e6b942005-04-05 08:51:15 +0000278 // Figure out which arguments are going to go in registers, and which in
279 // memory. Also, if this is a vararg function, floating point operations
280 // must be stored to our stack, and loaded into integer regs as well, if
281 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000282 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000283 unsigned GPR_remaining = 8;
284 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000285
Nate Begemand3e6b942005-04-05 08:51:15 +0000286 std::vector<SDOperand> MemOps;
287 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
288 // PtrOff will be used to store the current argument to the stack if a
289 // register cannot be found for it.
290 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
291 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
292 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000293
Nate Begemand3e6b942005-04-05 08:51:15 +0000294 switch (ArgVT) {
295 default: assert(0 && "Unexpected ValueType for argument!");
296 case MVT::i1:
297 case MVT::i8:
298 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000299 case MVT::i32:
300 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000301 // sign extend, otherwise use a zero extend.
302 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000303 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000304 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000305 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000306 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000307 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000308 if (GPR_remaining > 0) {
309 args_to_use.push_back(Args[i].first);
310 --GPR_remaining;
311 } else {
312 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000313 Args[i].first, PtrOff,
314 DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000315 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000316 ArgOffset += 8;
317 break;
318 case MVT::f32:
319 case MVT::f64:
320 if (FPR_remaining > 0) {
321 args_to_use.push_back(Args[i].first);
322 --FPR_remaining;
323 if (isVarArg) {
324 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000325 Args[i].first, PtrOff,
326 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000327 MemOps.push_back(Store);
328 // Float varargs are always shadowed in available integer registers
329 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000330 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff,
331 DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000332 MemOps.push_back(Load);
333 args_to_use.push_back(Load);
334 --GPR_remaining;
335 }
336 } else {
337 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000338 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000339 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000340 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000341 --GPR_remaining;
342 }
343 }
344 } else {
345 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000346 Args[i].first, PtrOff,
347 DAG.getSrcValue(NULL)));
Nate Begemand3e6b942005-04-05 08:51:15 +0000348 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000349 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000350 break;
351 }
352 }
353 if (!MemOps.empty())
354 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
355 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000356
Nate Begemand3e6b942005-04-05 08:51:15 +0000357 std::vector<MVT::ValueType> RetVals;
358 MVT::ValueType RetTyVT = getValueType(RetTy);
359 if (RetTyVT != MVT::isVoid)
360 RetVals.push_back(RetTyVT);
361 RetVals.push_back(MVT::Other);
362
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000363 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000364 Chain, Callee, args_to_use), 0);
365 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000366 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemand3e6b942005-04-05 08:51:15 +0000367 DAG.getConstant(NumBytes, getPointerTy()));
368 return std::make_pair(TheCall, Chain);
369}
370
371std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000372PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
373 // vastart just stores the address of the VarArgsFrameIndex slot.
374 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
375 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
376 return std::make_pair(Result, Result);
Nate Begemand3e6b942005-04-05 08:51:15 +0000377}
378
379std::pair<SDOperand,SDOperand> PPC64TargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000380LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemand3e6b942005-04-05 08:51:15 +0000381 const Type *ArgTy, SelectionDAG &DAG) {
382 MVT::ValueType ArgVT = getValueType(ArgTy);
383 SDOperand Result;
Andrew Lenharth558bc882005-06-18 18:34:52 +0000384 SDOperand Val = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
385 Result = DAG.getLoad(ArgVT, Val.getValue(1), Val, DAG.getSrcValue(NULL));
386 Val = DAG.getNode(ISD::ADD, VAList.getValueType(), Val,
387 DAG.getConstant(8, VAList.getValueType()));
388 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
389 Val, VAList, DAG.getSrcValue(NULL));
Nate Begemand3e6b942005-04-05 08:51:15 +0000390 return std::make_pair(Result, Chain);
391}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000392
Andrew Lenharth558bc882005-06-18 18:34:52 +0000393std::pair<SDOperand,SDOperand>
394PPC64TargetLowering::LowerVACopy(SDOperand Chain, SDOperand Src,
395 SDOperand Dest, SelectionDAG &DAG)
396{
397 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Chain,
398 Src, Dest, DAG.getSrcValue(NULL));
399 return std::make_pair(Result, Result);
400}
401
Nate Begemand3e6b942005-04-05 08:51:15 +0000402
403std::pair<SDOperand, SDOperand> PPC64TargetLowering::
404LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
405 SelectionDAG &DAG) {
406 assert(0 && "LowerFrameReturnAddress unimplemented");
407 abort();
408}
409
410namespace {
411Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
412Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
413//===--------------------------------------------------------------------===//
414/// ISel - PPC32 specific code to select PPC32 machine instructions for
415/// SelectionDAG operations.
416//===--------------------------------------------------------------------===//
417class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000418
Nate Begemand3e6b942005-04-05 08:51:15 +0000419 /// Comment Here.
420 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000421
Nate Begemand3e6b942005-04-05 08:51:15 +0000422 /// ExprMap - As shared expressions are codegen'd, we keep track of which
423 /// vreg the value is produced in, so we only emit one copy of each compiled
424 /// tree.
425 std::map<SDOperand, unsigned> ExprMap;
426
427 unsigned GlobalBaseReg;
428 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000429
Nate Begemand3e6b942005-04-05 08:51:15 +0000430public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000431 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000432 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000433
Nate Begemand3e6b942005-04-05 08:51:15 +0000434 /// runOnFunction - Override this function in order to reset our per-function
435 /// variables.
436 virtual bool runOnFunction(Function &Fn) {
437 // Make sure we re-emit a set of the global base reg if necessary
438 GlobalBaseInitialized = false;
439 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000440 }
441
Nate Begemand3e6b942005-04-05 08:51:15 +0000442 /// InstructionSelectBasicBlock - This callback is invoked by
443 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
444 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
445 DEBUG(BB->dump());
446 // Codegen the basic block.
447 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000448
Nate Begemand3e6b942005-04-05 08:51:15 +0000449 // Clear state used for selection.
450 ExprMap.clear();
451 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000452
Nate Begemand3e6b942005-04-05 08:51:15 +0000453 unsigned getGlobalBaseReg();
454 unsigned getConstDouble(double floatVal, unsigned Result);
455 unsigned SelectSetCR0(SDOperand CC);
456 unsigned SelectExpr(SDOperand N);
457 unsigned SelectExprFP(SDOperand N, unsigned Result);
458 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000459
Nate Begemand3e6b942005-04-05 08:51:15 +0000460 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
461 void SelectBranchCC(SDOperand N);
462};
463
464/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
465/// returns zero when the input is not exactly a power of two.
466static unsigned ExactLog2(unsigned Val) {
467 if (Val == 0 || (Val & (Val-1))) return 0;
468 unsigned Count = 0;
469 while (Val != 1) {
470 Val >>= 1;
471 ++Count;
472 }
473 return Count;
474}
475
476/// getImmediateForOpcode - This method returns a value indicating whether
477/// the ConstantSDNode N can be used as an immediate to Opcode. The return
478/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000479/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000480/// of 1 indicates that the constant may be used in normal immediate form. A
481/// return value of 2 indicates that the constant may be used in shifted
482/// immediate form. A return value of 3 indicates that log base 2 of the
483/// constant may be used.
484///
485static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
486 unsigned& Imm, bool U = false) {
487 if (N.getOpcode() != ISD::Constant) return 0;
488
489 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000490
Nate Begemand3e6b942005-04-05 08:51:15 +0000491 switch(Opcode) {
492 default: return 0;
493 case ISD::ADD:
494 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
495 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
496 break;
497 case ISD::AND:
498 case ISD::XOR:
499 case ISD::OR:
500 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
501 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
502 break;
503 case ISD::MUL:
504 case ISD::SUB:
505 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
506 break;
507 case ISD::SETCC:
508 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
509 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
510 break;
511 case ISD::SDIV:
512 if ((Imm = ExactLog2(v))) { return 3; }
513 break;
514 }
515 return 0;
516}
517
518/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
519/// to Condition. If the Condition is unordered or unsigned, the bool argument
520/// U is set to true, otherwise it is set to false.
521static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
522 U = false;
523 switch (Condition) {
524 default: assert(0 && "Unknown condition!"); abort();
525 case ISD::SETEQ: return PPC::BEQ;
526 case ISD::SETNE: return PPC::BNE;
527 case ISD::SETULT: U = true;
528 case ISD::SETLT: return PPC::BLT;
529 case ISD::SETULE: U = true;
530 case ISD::SETLE: return PPC::BLE;
531 case ISD::SETUGT: U = true;
532 case ISD::SETGT: return PPC::BGT;
533 case ISD::SETUGE: U = true;
534 case ISD::SETGE: return PPC::BGE;
535 }
536 return 0;
537}
538
539/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
540/// and store immediate instructions.
541static unsigned IndexedOpForOp(unsigned Opcode) {
542 switch(Opcode) {
543 default: assert(0 && "Unknown opcode!"); abort();
544 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
545 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
546 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
547 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
548 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
549 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
550 case PPC::LFD: return PPC::LFDX;
551 }
552 return 0;
553}
554}
555
556/// getGlobalBaseReg - Output the instructions required to put the
557/// base address to use for accessing globals into a register.
558///
559unsigned ISel::getGlobalBaseReg() {
560 if (!GlobalBaseInitialized) {
561 // Insert the set of GlobalBaseReg into the first MBB of the function
562 MachineBasicBlock &FirstMBB = BB->getParent()->front();
563 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
564 GlobalBaseReg = MakeReg(MVT::i64);
565 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
566 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
567 GlobalBaseInitialized = true;
568 }
569 return GlobalBaseReg;
570}
571
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000572/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000573/// Constant Pool. Optionally takes a register in which to load the value.
574unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000575 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000576 if (0 == Result) Result = MakeReg(MVT::f64);
577 MachineConstantPool *CP = BB->getParent()->getConstantPool();
578 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
579 unsigned CPI = CP->getConstantPoolIndex(CFP);
580 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
581 .addConstantPoolIndex(CPI);
582 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
583 return Result;
584}
585
586unsigned ISel::SelectSetCR0(SDOperand CC) {
587 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000588 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000589 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000590
Nate Begemand3e6b942005-04-05 08:51:15 +0000591 // If the first operand to the select is a SETCC node, then we can fold it
592 // into the branch that selects which value to return.
593 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
594 if (SetCC && CC.getOpcode() == ISD::SETCC) {
595 bool U;
596 Opc = getBCCForSetCC(SetCC->getCondition(), U);
597 Tmp1 = SelectExpr(SetCC->getOperand(0));
598
599 // Pass the optional argument U to getImmediateForOpcode for SETCC,
600 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000601 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000602 Tmp2, U)) {
603 if (U)
604 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
605 else
606 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
607 } else {
608 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
609 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
610 Tmp2 = SelectExpr(SetCC->getOperand(1));
611 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
612 }
613 } else {
614 Tmp1 = SelectExpr(CC);
615 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
616 Opc = PPC::BNE;
617 }
618 return Opc;
619}
620
621/// Check to see if the load is a constant offset from a base register
622bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
623{
624 unsigned imm = 0, opcode = N.getOpcode();
625 if (N.getOpcode() == ISD::ADD) {
626 Reg = SelectExpr(N.getOperand(0));
627 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
628 offset = imm;
629 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000630 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000631 offset = SelectExpr(N.getOperand(1));
632 return true;
633 }
634 Reg = SelectExpr(N);
635 offset = 0;
636 return false;
637}
638
639void ISel::SelectBranchCC(SDOperand N)
640{
641 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000642 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000643 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
644
645 // Get the MBB we will fall through to so that we can hand it off to the
646 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
647 //ilist<MachineBasicBlock>::iterator It = BB;
648 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000649
Nate Begemand3e6b942005-04-05 08:51:15 +0000650 Select(N.getOperand(0)); //chain
651 unsigned Opc = SelectSetCR0(N.getOperand(1));
652 // FIXME: Use this once we have something approximating two-way branches
653 // We cannot currently use this in case the ISel hands us something like
654 // BRcc MBBx
655 // BR MBBy
656 // since the fallthrough basic block for the conditional branch does not start
657 // with the unconditional branch (it is skipped over).
658 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
659 // .addMBB(Dest).addMBB(Fallthrough);
660 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
661 return;
662}
663
664unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
665{
666 unsigned Tmp1, Tmp2, Tmp3;
667 unsigned Opc = 0;
668 SDNode *Node = N.Val;
669 MVT::ValueType DestType = N.getValueType();
670 unsigned opcode = N.getOpcode();
671
672 switch (opcode) {
673 default:
674 Node->dump();
675 assert(0 && "Node not handled!\n");
676
677 case ISD::SELECT: {
678 // Attempt to generate FSEL. We can do this whenever we have an FP result,
679 // and an FP comparison in the SetCC node.
680 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
681 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
682 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
683 SetCC->getCondition() != ISD::SETEQ &&
684 SetCC->getCondition() != ISD::SETNE) {
685 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
686 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
687 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
688 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000689
Nate Begemand3e6b942005-04-05 08:51:15 +0000690 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
691 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
692 switch(SetCC->getCondition()) {
693 default: assert(0 && "Invalid FSEL condition"); abort();
694 case ISD::SETULT:
695 case ISD::SETLT:
696 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
697 return Result;
698 case ISD::SETUGE:
699 case ISD::SETGE:
700 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
701 return Result;
702 case ISD::SETUGT:
703 case ISD::SETGT: {
704 Tmp2 = MakeReg(VT);
705 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
706 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
707 return Result;
708 }
709 case ISD::SETULE:
710 case ISD::SETLE: {
711 Tmp2 = MakeReg(VT);
712 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
713 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
714 return Result;
715 }
716 }
717 } else {
718 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
719 Tmp2 = SelectExpr(SetCC->getOperand(1));
720 Tmp3 = MakeReg(VT);
721 switch(SetCC->getCondition()) {
722 default: assert(0 && "Invalid FSEL condition"); abort();
723 case ISD::SETULT:
724 case ISD::SETLT:
725 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
726 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
727 return Result;
728 case ISD::SETUGE:
729 case ISD::SETGE:
730 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
731 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
732 return Result;
733 case ISD::SETUGT:
734 case ISD::SETGT:
735 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
736 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
737 return Result;
738 case ISD::SETULE:
739 case ISD::SETLE:
740 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
741 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
742 return Result;
743 }
744 }
745 assert(0 && "Should never get here");
746 return 0;
747 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000748
Nate Begemand3e6b942005-04-05 08:51:15 +0000749 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
750 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
751 Opc = SelectSetCR0(N.getOperand(0));
752
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000753 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000754 // value and the MBB to hold the PHI instruction for this SetCC.
755 MachineBasicBlock *thisMBB = BB;
756 const BasicBlock *LLVM_BB = BB->getBasicBlock();
757 ilist<MachineBasicBlock>::iterator It = BB;
758 ++It;
759
760 // thisMBB:
761 // ...
762 // TrueVal = ...
763 // cmpTY cr0, r1, r2
764 // bCC copy1MBB
765 // fallthrough --> copy0MBB
766 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
767 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
768 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
769 MachineFunction *F = BB->getParent();
770 F->getBasicBlockList().insert(It, copy0MBB);
771 F->getBasicBlockList().insert(It, sinkMBB);
772 // Update machine-CFG edges
773 BB->addSuccessor(copy0MBB);
774 BB->addSuccessor(sinkMBB);
775
776 // copy0MBB:
777 // %FalseValue = ...
778 // # fallthrough to sinkMBB
779 BB = copy0MBB;
780 // Update machine-CFG edges
781 BB->addSuccessor(sinkMBB);
782
783 // sinkMBB:
784 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
785 // ...
786 BB = sinkMBB;
787 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
788 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
789 return Result;
790 }
791
792 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000793 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000794 ISD::ADD == N.getOperand(0).getOpcode() &&
795 N.getOperand(0).Val->hasOneUse() &&
796 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
797 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
798 ++FusedFP; // Statistic
799 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
800 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
801 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
802 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
803 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000804 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000805 ISD::SUB == N.getOperand(0).getOpcode() &&
806 N.getOperand(0).Val->hasOneUse() &&
807 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
808 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
809 ++FusedFP; // Statistic
810 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
811 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
812 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
813 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
814 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
815 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
816 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
817 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
818 } else {
819 Tmp1 = SelectExpr(N.getOperand(0));
820 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
821 }
822 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000823
Nate Begemand3e6b942005-04-05 08:51:15 +0000824 case ISD::FABS:
825 Tmp1 = SelectExpr(N.getOperand(0));
826 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
827 return Result;
828
829 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000830 assert (DestType == MVT::f32 &&
831 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000832 "only f64 to f32 conversion supported here");
833 Tmp1 = SelectExpr(N.getOperand(0));
834 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
835 return Result;
836
837 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000838 assert (DestType == MVT::f64 &&
839 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000840 "only f32 to f64 conversion supported here");
841 Tmp1 = SelectExpr(N.getOperand(0));
842 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
843 return Result;
844
845 case ISD::CopyFromReg:
846 if (Result == 1)
847 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
848 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
849 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
850 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000851
Nate Begemand3e6b942005-04-05 08:51:15 +0000852 case ISD::ConstantFP: {
853 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
854 Result = getConstDouble(CN->getValue(), Result);
855 return Result;
856 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000857
Nate Begemand3e6b942005-04-05 08:51:15 +0000858 case ISD::ADD:
859 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
860 N.getOperand(0).Val->hasOneUse()) {
861 ++FusedFP; // Statistic
862 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
863 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
864 Tmp3 = SelectExpr(N.getOperand(1));
865 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
866 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
867 return Result;
868 }
869 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
870 Tmp1 = SelectExpr(N.getOperand(0));
871 Tmp2 = SelectExpr(N.getOperand(1));
872 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
873 return Result;
874
875 case ISD::SUB:
876 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
877 N.getOperand(0).Val->hasOneUse()) {
878 ++FusedFP; // Statistic
879 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
880 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
881 Tmp3 = SelectExpr(N.getOperand(1));
882 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
883 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
884 return Result;
885 }
886 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
887 Tmp1 = SelectExpr(N.getOperand(0));
888 Tmp2 = SelectExpr(N.getOperand(1));
889 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
890 return Result;
891
892 case ISD::MUL:
893 case ISD::SDIV:
894 switch( opcode ) {
895 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
896 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
897 };
898 Tmp1 = SelectExpr(N.getOperand(0));
899 Tmp2 = SelectExpr(N.getOperand(1));
900 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
901 return Result;
902
903 case ISD::UINT_TO_FP:
904 case ISD::SINT_TO_FP: {
905 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
906 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
907 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
908 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
909 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910
Nate Begemand3e6b942005-04-05 08:51:15 +0000911 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
912 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000913
Nate Begemand3e6b942005-04-05 08:51:15 +0000914 // FIXME: pull this FP constant generation stuff out into something like
915 // the simple ISel's getReg.
916 if (IsUnsigned) {
917 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
918 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
919 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
920 } else {
921 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
922 unsigned CPI = CP->getConstantPoolIndex(CFP);
923 // Load constant fp value
924 unsigned Tmp4 = MakeReg(MVT::i32);
925 unsigned TmpL = MakeReg(MVT::i32);
926 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
927 .addConstantPoolIndex(CPI);
928 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
929 // Store the hi & low halves of the fp value, currently in int regs
930 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
931 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
932 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
933 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
934 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
935 // Generate the return value with a subtract
936 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
937 }
938 return Result;
939 }
940 }
941 assert(0 && "Should never get here");
942 return 0;
943}
944
945unsigned ISel::SelectExpr(SDOperand N) {
946 unsigned Result;
947 unsigned Tmp1, Tmp2, Tmp3;
948 unsigned Opc = 0;
949 unsigned opcode = N.getOpcode();
950
951 SDNode *Node = N.Val;
952 MVT::ValueType DestType = N.getValueType();
953
954 unsigned &Reg = ExprMap[N];
955 if (Reg) return Reg;
956
957 switch (N.getOpcode()) {
958 default:
959 Reg = Result = (N.getValueType() != MVT::Other) ?
960 MakeReg(N.getValueType()) : 1;
961 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000962 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +0000963 case ISD::CALL:
964 // If this is a call instruction, make sure to prepare ALL of the result
965 // values as well as the chain.
966 if (Node->getNumValues() == 1)
967 Reg = Result = 1; // Void call, just a chain.
968 else {
969 Result = MakeReg(Node->getValueType(0));
970 ExprMap[N.getValue(0)] = Result;
971 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
972 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
973 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
974 }
975 break;
976 }
977
978 if (ISD::CopyFromReg == opcode)
979 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000980
Nate Begemand3e6b942005-04-05 08:51:15 +0000981 if (DestType == MVT::f64 || DestType == MVT::f32)
982 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
983 return SelectExprFP(N, Result);
984
985 switch (opcode) {
986 default:
987 Node->dump();
988 assert(0 && "Node not handled!\n");
989 case ISD::UNDEF:
990 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
991 return Result;
992 case ISD::DYNAMIC_STACKALLOC:
993 // Generate both result values. FIXME: Need a better commment here?
994 if (Result != 1)
995 ExprMap[N.getValue(1)] = 1;
996 else
997 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
998
999 // FIXME: We are currently ignoring the requested alignment for handling
1000 // greater than the stack alignment. This will need to be revisited at some
1001 // point. Align = N.getOperand(2);
1002 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1003 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1004 std::cerr << "Cannot allocate stack object with greater alignment than"
1005 << " the stack alignment yet!";
1006 abort();
1007 }
1008 Select(N.getOperand(0));
1009 Tmp1 = SelectExpr(N.getOperand(1));
1010 // Subtract size from stack pointer, thereby allocating some space.
1011 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1012 // Put a pointer to the space into the result register by copying the SP
1013 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1014 return Result;
1015
1016 case ISD::ConstantPool:
1017 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1018 Tmp2 = MakeReg(MVT::i64);
1019 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1020 .addConstantPoolIndex(Tmp1);
1021 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1022 return Result;
1023
1024 case ISD::FrameIndex:
1025 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1026 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
1027 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001028
Nate Begemand3e6b942005-04-05 08:51:15 +00001029 case ISD::GlobalAddress: {
1030 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1031 Tmp1 = MakeReg(MVT::i64);
1032 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1033 .addGlobalAddress(GV);
1034 if (GV->hasWeakLinkage() || GV->isExternal()) {
Nate Begemana9532d52005-04-08 23:45:01 +00001035 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001036 } else {
1037 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1038 }
1039 return Result;
1040 }
1041
1042 case ISD::LOAD:
1043 case ISD::EXTLOAD:
1044 case ISD::ZEXTLOAD:
1045 case ISD::SEXTLOAD: {
1046 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1047 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1048 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001049
Nate Begemand3e6b942005-04-05 08:51:15 +00001050 // Make sure we generate both values.
1051 if (Result != 1)
1052 ExprMap[N.getValue(1)] = 1; // Generate the token
1053 else
1054 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1055
1056 SDOperand Chain = N.getOperand(0);
1057 SDOperand Address = N.getOperand(1);
1058 Select(Chain);
1059
1060 switch (TypeBeingLoaded) {
1061 default: Node->dump(); assert(0 && "Cannot load this type!");
1062 case MVT::i1: Opc = PPC::LBZ; break;
1063 case MVT::i8: Opc = PPC::LBZ; break;
1064 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1065 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1066 case MVT::i64: Opc = PPC::LD; break;
1067 case MVT::f32: Opc = PPC::LFS; break;
1068 case MVT::f64: Opc = PPC::LFD; break;
1069 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001070
Nate Begemand3e6b942005-04-05 08:51:15 +00001071 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1072 Tmp1 = MakeReg(MVT::i64);
1073 int CPI = CP->getIndex();
1074 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1075 .addConstantPoolIndex(CPI);
1076 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1077 }
1078 else if(Address.getOpcode() == ISD::FrameIndex) {
1079 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1080 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1081 } else {
1082 int offset;
1083 bool idx = SelectAddr(Address, Tmp1, offset);
1084 if (idx) {
1085 Opc = IndexedOpForOp(Opc);
1086 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1087 } else {
1088 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1089 }
1090 }
1091 return Result;
1092 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001093
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001094 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +00001095 case ISD::CALL: {
1096 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001097 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001098 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1099 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1100 };
1101 static const unsigned FPR[] = {
1102 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1103 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1104 };
1105
1106 // Lower the chain for this call.
1107 Select(N.getOperand(0));
1108 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1109
1110 MachineInstr *CallMI;
1111 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001112 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001113 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001114 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001115 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001116 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001117 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001118 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001119 true);
1120 } else {
1121 Tmp1 = SelectExpr(N.getOperand(1));
1122 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1123 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1124 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1125 .addReg(PPC::R12);
1126 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001127
Nate Begemand3e6b942005-04-05 08:51:15 +00001128 // Load the register args to virtual regs
1129 std::vector<unsigned> ArgVR;
1130 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1131 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1132
1133 // Copy the virtual registers into the appropriate argument register
1134 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1135 switch(N.getOperand(i+2).getValueType()) {
1136 default: Node->dump(); assert(0 && "Unknown value type for call");
1137 case MVT::i1:
1138 case MVT::i8:
1139 case MVT::i16:
1140 case MVT::i32:
1141 case MVT::i64:
1142 assert(GPR_idx < 8 && "Too many int args");
1143 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1144 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1145 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1146 }
1147 ++GPR_idx;
1148 break;
1149 case MVT::f64:
1150 case MVT::f32:
1151 assert(FPR_idx < 13 && "Too many fp args");
1152 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1153 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1154 ++FPR_idx;
1155 break;
1156 }
1157 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001158
Nate Begemand3e6b942005-04-05 08:51:15 +00001159 // Put the call instruction in the correct place in the MachineBasicBlock
1160 BB->push_back(CallMI);
1161
1162 switch (Node->getValueType(0)) {
1163 default: assert(0 && "Unknown value type for call result!");
1164 case MVT::Other: return 1;
1165 case MVT::i1:
1166 case MVT::i8:
1167 case MVT::i16:
1168 case MVT::i32:
1169 case MVT::i64:
1170 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1171 break;
1172 case MVT::f32:
1173 case MVT::f64:
1174 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1175 break;
1176 }
1177 return Result+N.ResNo;
1178 }
1179
1180 case ISD::SIGN_EXTEND:
1181 case ISD::SIGN_EXTEND_INREG:
1182 Tmp1 = SelectExpr(N.getOperand(0));
1183 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1184 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1185 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001186 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001187 break;
1188 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001189 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001190 break;
1191 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001192 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001193 break;
1194 case MVT::i1:
1195 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1196 break;
1197 }
1198 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001199
Nate Begemand3e6b942005-04-05 08:51:15 +00001200 case ISD::CopyFromReg:
1201 if (Result == 1)
1202 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1203 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1204 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1205 return Result;
1206
1207 case ISD::SHL:
1208 Tmp1 = SelectExpr(N.getOperand(0));
1209 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001210 Tmp2 = CN->getValue() & 0x3F;
1211 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1212 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001213 } else {
1214 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001215 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001216 }
1217 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001218
Nate Begemand3e6b942005-04-05 08:51:15 +00001219 case ISD::SRL:
1220 Tmp1 = SelectExpr(N.getOperand(0));
1221 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001222 Tmp2 = CN->getValue() & 0x3F;
1223 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1224 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001225 } else {
1226 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001227 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001228 }
1229 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001230
Nate Begemand3e6b942005-04-05 08:51:15 +00001231 case ISD::SRA:
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001234 Tmp2 = CN->getValue() & 0x3F;
1235 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001236 } else {
1237 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001238 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001239 }
1240 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001241
Nate Begemand3e6b942005-04-05 08:51:15 +00001242 case ISD::ADD:
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1245 default: assert(0 && "unhandled result code");
1246 case 0: // No immediate
1247 Tmp2 = SelectExpr(N.getOperand(1));
1248 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1249 break;
1250 case 1: // Low immediate
1251 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1252 break;
1253 case 2: // Shifted immediate
1254 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1255 break;
1256 }
1257 return Result;
1258
1259 case ISD::AND:
1260 case ISD::OR:
1261 Tmp1 = SelectExpr(N.getOperand(0));
1262 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1263 default: assert(0 && "unhandled result code");
1264 case 0: // No immediate
1265 Tmp2 = SelectExpr(N.getOperand(1));
1266 switch (opcode) {
1267 case ISD::AND: Opc = PPC::AND; break;
1268 case ISD::OR: Opc = PPC::OR; break;
1269 }
1270 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1271 break;
1272 case 1: // Low immediate
1273 switch (opcode) {
1274 case ISD::AND: Opc = PPC::ANDIo; break;
1275 case ISD::OR: Opc = PPC::ORI; break;
1276 }
1277 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1278 break;
1279 case 2: // Shifted immediate
1280 switch (opcode) {
1281 case ISD::AND: Opc = PPC::ANDISo; break;
1282 case ISD::OR: Opc = PPC::ORIS; break;
1283 }
1284 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1285 break;
1286 }
1287 return Result;
1288
1289 case ISD::XOR: {
1290 // Check for EQV: xor, (xor a, -1), b
1291 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1292 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1293 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1294 ++NotLogic;
1295 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1296 Tmp2 = SelectExpr(N.getOperand(1));
1297 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1298 return Result;
1299 }
1300 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1301 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1302 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1303 ++NotLogic;
1304 switch(N.getOperand(0).getOpcode()) {
1305 case ISD::OR:
1306 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1308 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 break;
1310 case ISD::AND:
1311 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1312 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1313 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1314 break;
1315 default:
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1318 break;
1319 }
1320 return Result;
1321 }
1322 Tmp1 = SelectExpr(N.getOperand(0));
1323 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1324 default: assert(0 && "unhandled result code");
1325 case 0: // No immediate
1326 Tmp2 = SelectExpr(N.getOperand(1));
1327 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1328 break;
1329 case 1: // Low immediate
1330 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1331 break;
1332 case 2: // Shifted immediate
1333 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1334 break;
1335 }
1336 return Result;
1337 }
1338
1339 case ISD::SUB:
1340 Tmp2 = SelectExpr(N.getOperand(1));
1341 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1342 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1343 else {
1344 Tmp1 = SelectExpr(N.getOperand(0));
1345 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1346 }
1347 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001348
Nate Begemand3e6b942005-04-05 08:51:15 +00001349 case ISD::MUL:
1350 Tmp1 = SelectExpr(N.getOperand(0));
1351 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1352 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1353 else {
1354 Tmp2 = SelectExpr(N.getOperand(1));
1355 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1356 }
1357 return Result;
1358
1359 case ISD::SDIV:
1360 case ISD::UDIV:
1361 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1362 Tmp1 = MakeReg(MVT::i64);
1363 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001364 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001365 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1366 return Result;
1367 }
1368 Tmp1 = SelectExpr(N.getOperand(0));
1369 Tmp2 = SelectExpr(N.getOperand(1));
1370 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1371 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1372 return Result;
1373
Nate Begemand3e6b942005-04-05 08:51:15 +00001374 case ISD::FP_TO_UINT:
1375 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001376 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001377 Tmp2 = MakeReg(MVT::f64);
1378 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1379 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1380 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1381 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1382 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001383 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001384
Nate Begemand3e6b942005-04-05 08:51:15 +00001385 case ISD::SETCC:
1386 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1387 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001388
Nate Begemand3e6b942005-04-05 08:51:15 +00001389 unsigned TrueValue = MakeReg(MVT::i32);
1390 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1391 unsigned FalseValue = MakeReg(MVT::i32);
1392 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1393
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001394 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001395 // value and the MBB to hold the PHI instruction for this SetCC.
1396 MachineBasicBlock *thisMBB = BB;
1397 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1398 ilist<MachineBasicBlock>::iterator It = BB;
1399 ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001400
Nate Begemand3e6b942005-04-05 08:51:15 +00001401 // thisMBB:
1402 // ...
1403 // cmpTY cr0, r1, r2
1404 // %TrueValue = li 1
1405 // bCC sinkMBB
1406 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1407 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1408 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1409 MachineFunction *F = BB->getParent();
1410 F->getBasicBlockList().insert(It, copy0MBB);
1411 F->getBasicBlockList().insert(It, sinkMBB);
1412 // Update machine-CFG edges
1413 BB->addSuccessor(copy0MBB);
1414 BB->addSuccessor(sinkMBB);
1415
1416 // copy0MBB:
1417 // %FalseValue = li 0
1418 // fallthrough
1419 BB = copy0MBB;
1420 // Update machine-CFG edges
1421 BB->addSuccessor(sinkMBB);
1422
1423 // sinkMBB:
1424 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1425 // ...
1426 BB = sinkMBB;
1427 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1428 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1429 return Result;
1430 }
1431 assert(0 && "Is this legal?");
1432 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001433
Nate Begemand3e6b942005-04-05 08:51:15 +00001434 case ISD::SELECT: {
1435 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1436 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1437 Opc = SelectSetCR0(N.getOperand(0));
1438
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001439 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001440 // value and the MBB to hold the PHI instruction for this SetCC.
1441 MachineBasicBlock *thisMBB = BB;
1442 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1443 ilist<MachineBasicBlock>::iterator It = BB;
1444 ++It;
1445
1446 // thisMBB:
1447 // ...
1448 // TrueVal = ...
1449 // cmpTY cr0, r1, r2
1450 // bCC copy1MBB
1451 // fallthrough --> copy0MBB
1452 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1453 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1454 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1455 MachineFunction *F = BB->getParent();
1456 F->getBasicBlockList().insert(It, copy0MBB);
1457 F->getBasicBlockList().insert(It, sinkMBB);
1458 // Update machine-CFG edges
1459 BB->addSuccessor(copy0MBB);
1460 BB->addSuccessor(sinkMBB);
1461
1462 // copy0MBB:
1463 // %FalseValue = ...
1464 // # fallthrough to sinkMBB
1465 BB = copy0MBB;
1466 // Update machine-CFG edges
1467 BB->addSuccessor(sinkMBB);
1468
1469 // sinkMBB:
1470 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1471 // ...
1472 BB = sinkMBB;
1473 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1474 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1475
1476 // FIXME: Select i64?
1477 return Result;
1478 }
1479
1480 case ISD::Constant:
1481 switch (N.getValueType()) {
1482 default: assert(0 && "Cannot use constants of this type!");
1483 case MVT::i1:
1484 BuildMI(BB, PPC::LI, 1, Result)
1485 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1486 break;
1487 case MVT::i32:
1488 {
1489 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1490 if (v < 32768 && v >= -32768) {
1491 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1492 } else {
1493 Tmp1 = MakeReg(MVT::i32);
1494 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1495 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1496 }
1497 }
1498 }
1499 return Result;
1500 }
1501
1502 return 0;
1503}
1504
1505void ISel::Select(SDOperand N) {
1506 unsigned Tmp1, Tmp2, Opc;
1507 unsigned opcode = N.getOpcode();
1508
1509 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1510 return; // Already selected.
1511
1512 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001513
Nate Begemand3e6b942005-04-05 08:51:15 +00001514 switch (Node->getOpcode()) {
1515 default:
1516 Node->dump(); std::cerr << "\n";
1517 assert(0 && "Node not handled yet!");
1518 case ISD::EntryToken: return; // Noop
1519 case ISD::TokenFactor:
1520 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1521 Select(Node->getOperand(i));
1522 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001523 case ISD::CALLSEQ_START:
1524 case ISD::CALLSEQ_END:
Nate Begemand3e6b942005-04-05 08:51:15 +00001525 Select(N.getOperand(0));
1526 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001527 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemand3e6b942005-04-05 08:51:15 +00001528 PPC::ADJCALLSTACKUP;
1529 BuildMI(BB, Opc, 1).addImm(Tmp1);
1530 return;
1531 case ISD::BR: {
1532 MachineBasicBlock *Dest =
1533 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1534 Select(N.getOperand(0));
1535 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1536 return;
1537 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001538 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001539 SelectBranchCC(N);
1540 return;
1541 case ISD::CopyToReg:
1542 Select(N.getOperand(0));
1543 Tmp1 = SelectExpr(N.getOperand(1));
1544 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001545
Nate Begemand3e6b942005-04-05 08:51:15 +00001546 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001547 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001548 N.getOperand(1).getValueType() == MVT::f32)
1549 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1550 else
1551 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1552 }
1553 return;
1554 case ISD::ImplicitDef:
1555 Select(N.getOperand(0));
1556 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1557 return;
1558 case ISD::RET:
1559 switch (N.getNumOperands()) {
1560 default:
1561 assert(0 && "Unknown return instruction!");
1562 case 3:
1563 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1564 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001565 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001566 Select(N.getOperand(0));
1567 Tmp1 = SelectExpr(N.getOperand(1));
1568 Tmp2 = SelectExpr(N.getOperand(2));
1569 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1570 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1571 break;
1572 case 2:
1573 Select(N.getOperand(0));
1574 Tmp1 = SelectExpr(N.getOperand(1));
1575 switch (N.getOperand(1).getValueType()) {
1576 default:
1577 assert(0 && "Unknown return type!");
1578 case MVT::f64:
1579 case MVT::f32:
1580 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1581 break;
1582 case MVT::i32:
1583 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1584 break;
1585 }
1586 case 1:
1587 Select(N.getOperand(0));
1588 break;
1589 }
1590 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1591 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001592 case ISD::TRUNCSTORE:
1593 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001594 {
1595 SDOperand Chain = N.getOperand(0);
1596 SDOperand Value = N.getOperand(1);
1597 SDOperand Address = N.getOperand(2);
1598 Select(Chain);
1599
1600 Tmp1 = SelectExpr(Value); //value
1601
1602 if (opcode == ISD::STORE) {
1603 switch(Value.getValueType()) {
1604 default: assert(0 && "unknown Type in store");
1605 case MVT::i64: Opc = PPC::STD; break;
1606 case MVT::f64: Opc = PPC::STFD; break;
1607 case MVT::f32: Opc = PPC::STFS; break;
1608 }
1609 } else { //ISD::TRUNCSTORE
1610 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1611 default: assert(0 && "unknown Type in store");
1612 case MVT::i1: //FIXME: DAG does not promote this load
1613 case MVT::i8: Opc= PPC::STB; break;
1614 case MVT::i16: Opc = PPC::STH; break;
1615 case MVT::i32: Opc = PPC::STW; break;
1616 }
1617 }
1618
1619 if(Address.getOpcode() == ISD::FrameIndex)
1620 {
1621 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1622 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1623 }
1624 else
1625 {
1626 int offset;
1627 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001628 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001629 Opc = IndexedOpForOp(Opc);
1630 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1631 } else {
1632 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1633 }
1634 }
1635 return;
1636 }
1637 case ISD::EXTLOAD:
1638 case ISD::SEXTLOAD:
1639 case ISD::ZEXTLOAD:
1640 case ISD::LOAD:
1641 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001642 case ISD::TAILCALL:
Nate Begemand3e6b942005-04-05 08:51:15 +00001643 case ISD::CALL:
1644 case ISD::DYNAMIC_STACKALLOC:
1645 ExprMap.erase(N);
1646 SelectExpr(N);
1647 return;
1648 }
1649 assert(0 && "Should not be reached!");
1650}
1651
1652
1653/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1654/// into a machine code representation using pattern matching and a machine
1655/// description file.
1656///
1657FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001658 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001659}
1660