blob: e5c2f40e3c274df374b6a85505e2069c314d3891 [file] [log] [blame]
Nate Begemana3829d52005-04-05 17:32:30 +00001//===-- PPC64ISelPattern.cpp - A pattern matching inst selector for PPC64 -===//
Nate Begemand3e6b942005-04-05 08:51:15 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemand3e6b942005-04-05 08:51:15 +00008//===----------------------------------------------------------------------===//
9//
Nate Begemana3829d52005-04-05 17:32:30 +000010// This file defines a pattern matching instruction selector for 64 bit PowerPC.
Nate Begemand3e6b942005-04-05 08:51:15 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC64RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetOptions.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
33#include <algorithm>
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
38namespace {
39 class PPC64TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
41 int ReturnAddrIndex; // FrameIndex for return slot.
42 public:
43 PPC64TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the register classes.
45 addRegisterClass(MVT::i64, PPC64::GPRCRegisterClass);
46 addRegisterClass(MVT::f32, PPC64::FPRCRegisterClass);
47 addRegisterClass(MVT::f64, PPC64::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000048
Nate Begemand3e6b942005-04-05 08:51:15 +000049 // PowerPC has no intrinsics for these particular operations
Chris Lattner644db4e2005-04-09 03:22:30 +000050 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Nate Begemand3e6b942005-04-05 08:51:15 +000051 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
52 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
53 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
54
55 // PPC 64 has i16 and i32 but no i8 (or i1) SEXTLOAD
56 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
57 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
58
Nate Begemane88aa5b2005-04-09 03:05:51 +000059 // PowerPC has no SREM/UREM instructions
60 setOperationAction(ISD::SREM, MVT::i64, Expand);
61 setOperationAction(ISD::UREM, MVT::i64, Expand);
62
Nate Begemand3e6b942005-04-05 08:51:15 +000063 setShiftAmountFlavor(Extend); // shl X, 32 == 0
64 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000065 addLegalFPImmediate(-0.0); //
Nate Begemand3e6b942005-04-05 08:51:15 +000066
67 computeRegisterProperties();
68 }
69
70 /// LowerArguments - This hook must be implemented to indicate how we should
71 /// lower the arguments for the specified function, into the specified DAG.
72 virtual std::vector<SDOperand>
73 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000074
Nate Begemand3e6b942005-04-05 08:51:15 +000075 /// LowerCallTo - This hook lowers an abstract call to a function into an
76 /// actual call.
77 virtual std::pair<SDOperand, SDOperand>
78 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
79 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000080
Nate Begemand3e6b942005-04-05 08:51:15 +000081 virtual std::pair<SDOperand, SDOperand>
82 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083
Nate Begemand3e6b942005-04-05 08:51:15 +000084 virtual std::pair<SDOperand,SDOperand>
85 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
86 const Type *ArgTy, SelectionDAG &DAG);
87
88 virtual std::pair<SDOperand, SDOperand>
89 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
90 SelectionDAG &DAG);
91 };
92}
93
94
95std::vector<SDOperand>
96PPC64TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
97 //
98 // add beautiful description of PPC stack frame format, or at least some docs
99 //
100 MachineFunction &MF = DAG.getMachineFunction();
101 MachineFrameInfo *MFI = MF.getFrameInfo();
102 MachineBasicBlock& BB = MF.front();
103 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000104
105 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemand3e6b942005-04-05 08:51:15 +0000106 // fixed size array of physical args, for the sake of simplicity let the STL
107 // handle tracking them for us.
108 std::vector<unsigned> argVR, argPR, argOp;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000109 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000110 unsigned GPR_remaining = 8;
111 unsigned FPR_remaining = 13;
112 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000113 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +0000114 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
115 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
116 };
117 static const unsigned FPR[] = {
118 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
119 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
120 };
121
122 // Add DAG nodes to load the arguments... On entry to a function on PPC,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000123 // the arguments start at offset 48, although they are likely to be passed
Nate Begemand3e6b942005-04-05 08:51:15 +0000124 // in registers.
125 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
126 SDOperand newroot, argt;
Nate Begemand3e6b942005-04-05 08:51:15 +0000127 bool needsLoad = false;
128 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000129
Nate Begemand3e6b942005-04-05 08:51:15 +0000130 switch (ObjectVT) {
131 default: assert(0 && "Unhandled argument type!");
132 case MVT::i1:
133 case MVT::i8:
134 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000135 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000136 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000137 if (GPR_remaining > 0) {
138 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
139 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
140 DAG.getRoot());
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000141 if (ObjectVT != MVT::i64)
Nate Begemand3e6b942005-04-05 08:51:15 +0000142 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
143 } else {
144 needsLoad = true;
145 }
146 break;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000147 case MVT::f32:
148 case MVT::f64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000149 if (FPR_remaining > 0) {
150 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000151 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemand3e6b942005-04-05 08:51:15 +0000152 DAG.getRoot());
153 --FPR_remaining;
154 ++FPR_idx;
155 } else {
156 needsLoad = true;
157 }
158 break;
159 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000160
Nate Begemand3e6b942005-04-05 08:51:15 +0000161 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000162 // that we ran out of physical registers of the appropriate type
Nate Begemand3e6b942005-04-05 08:51:15 +0000163 if (needsLoad) {
164 unsigned SubregOffset = 0;
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000165 switch (ObjectVT) {
166 default: assert(0 && "Unhandled argument type!");
167 case MVT::i1:
168 case MVT::i8: SubregOffset = 7; break;
169 case MVT::i16: SubregOffset = 6; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000170 case MVT::i32:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000171 case MVT::f32: SubregOffset = 4; break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000172 case MVT::i64:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000173 case MVT::f64: SubregOffset = 0; break;
174 }
175 int FI = MFI->CreateFixedObject(8, ArgOffset);
176 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000177 FIN = DAG.getNode(ISD::ADD, MVT::i64, FIN,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000178 DAG.getConstant(SubregOffset, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000179 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
180 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000181
Nate Begemand3e6b942005-04-05 08:51:15 +0000182 // Every 4 bytes of argument space consumes one of the GPRs available for
183 // argument passing.
184 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000185 --GPR_remaining;
186 ++GPR_idx;
Nate Begemand3e6b942005-04-05 08:51:15 +0000187 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000188 ArgOffset += 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000189
Nate Begemand3e6b942005-04-05 08:51:15 +0000190 DAG.setRoot(newroot.getValue(1));
191 ArgValues.push_back(argt);
192 }
193
194 // If the function takes variable number of arguments, make a frame index for
195 // the start of the first vararg value... for expansion of llvm.va_start.
196 if (F.isVarArg()) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000197 VarArgsFrameIndex = MFI->CreateFixedObject(8, ArgOffset);
198 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000199 // If this function is vararg, store any remaining integer argument regs
200 // to their spots on the stack so that they may be loaded by deferencing the
201 // result of va_next.
202 std::vector<SDOperand> MemOps;
203 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
204 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000205 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i64, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000206 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Nate Begemand3e6b942005-04-05 08:51:15 +0000207 Val, FIN);
208 MemOps.push_back(Store);
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000209 // Increment the address by eight for the next argument to store
210 SDOperand PtrOff = DAG.getConstant(8, getPointerTy());
Nate Begemand3e6b942005-04-05 08:51:15 +0000211 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
212 }
213 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
214 }
215
216 return ArgValues;
217}
218
219std::pair<SDOperand, SDOperand>
220PPC64TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000221 const Type *RetTy, bool isVarArg,
222 SDOperand Callee, ArgListTy &Args,
223 SelectionDAG &DAG) {
Nate Begemand3e6b942005-04-05 08:51:15 +0000224 // args_to_use will accumulate outgoing args for the ISD::CALL case in
225 // SelectExpr to use to put the arguments in the appropriate registers.
226 std::vector<SDOperand> args_to_use;
227
228 // Count how many bytes are to be pushed on the stack, including the linkage
229 // area, and parameter passing area.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000230 unsigned NumBytes = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000231
232 if (Args.empty()) {
233 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
234 DAG.getConstant(NumBytes, getPointerTy()));
235 } else {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000236 NumBytes = 8 * Args.size(); // All arguments are rounded up to 8 bytes
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000237
238 // Just to be safe, we'll always reserve the full 48 bytes of linkage area
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000239 // plus 64 bytes of argument space in case any called code gets funky on us.
240 if (NumBytes < 112) NumBytes = 112;
Nate Begemand3e6b942005-04-05 08:51:15 +0000241
242 // Adjust the stack pointer for the new arguments...
243 // These operations are automatically eliminated by the prolog/epilog pass
244 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246
247 // Set up a copy of the stack pointer for use loading and storing any
248 // arguments that may not fit in the registers available for argument
249 // passing.
250 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
251 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000252
Nate Begemand3e6b942005-04-05 08:51:15 +0000253 // Figure out which arguments are going to go in registers, and which in
254 // memory. Also, if this is a vararg function, floating point operations
255 // must be stored to our stack, and loaded into integer regs as well, if
256 // any integer regs are available for argument passing.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000257 unsigned ArgOffset = 48;
Nate Begemand3e6b942005-04-05 08:51:15 +0000258 unsigned GPR_remaining = 8;
259 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000260
Nate Begemand3e6b942005-04-05 08:51:15 +0000261 std::vector<SDOperand> MemOps;
262 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
263 // PtrOff will be used to store the current argument to the stack if a
264 // register cannot be found for it.
265 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
266 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
267 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000268
Nate Begemand3e6b942005-04-05 08:51:15 +0000269 switch (ArgVT) {
270 default: assert(0 && "Unexpected ValueType for argument!");
271 case MVT::i1:
272 case MVT::i8:
273 case MVT::i16:
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000274 case MVT::i32:
275 // Promote the integer to 64 bits. If the input type is signed use a
Nate Begemand3e6b942005-04-05 08:51:15 +0000276 // sign extend, otherwise use a zero extend.
277 if (Args[i].second->isSigned())
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000278 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000279 else
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000280 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Nate Begemand3e6b942005-04-05 08:51:15 +0000281 // FALL THROUGH
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000282 case MVT::i64:
Nate Begemand3e6b942005-04-05 08:51:15 +0000283 if (GPR_remaining > 0) {
284 args_to_use.push_back(Args[i].first);
285 --GPR_remaining;
286 } else {
287 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
288 Args[i].first, PtrOff));
289 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000290 ArgOffset += 8;
291 break;
292 case MVT::f32:
293 case MVT::f64:
294 if (FPR_remaining > 0) {
295 args_to_use.push_back(Args[i].first);
296 --FPR_remaining;
297 if (isVarArg) {
298 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
299 Args[i].first, PtrOff);
300 MemOps.push_back(Store);
301 // Float varargs are always shadowed in available integer registers
302 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000303 SDOperand Load = DAG.getLoad(MVT::i64, Store, PtrOff);
Nate Begemand3e6b942005-04-05 08:51:15 +0000304 MemOps.push_back(Load);
305 args_to_use.push_back(Load);
306 --GPR_remaining;
307 }
308 } else {
309 // If we have any FPRs remaining, we may also have GPRs remaining.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000310 // Args passed in FPRs also consume an available GPR.
Nate Begemand3e6b942005-04-05 08:51:15 +0000311 if (GPR_remaining > 0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000312 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i64));
Nate Begemand3e6b942005-04-05 08:51:15 +0000313 --GPR_remaining;
314 }
315 }
316 } else {
317 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
318 Args[i].first, PtrOff));
319 }
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000320 ArgOffset += 8;
Nate Begemand3e6b942005-04-05 08:51:15 +0000321 break;
322 }
323 }
324 if (!MemOps.empty())
325 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
326 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000327
Nate Begemand3e6b942005-04-05 08:51:15 +0000328 std::vector<MVT::ValueType> RetVals;
329 MVT::ValueType RetTyVT = getValueType(RetTy);
330 if (RetTyVT != MVT::isVoid)
331 RetVals.push_back(RetTyVT);
332 RetVals.push_back(MVT::Other);
333
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000334 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemand3e6b942005-04-05 08:51:15 +0000335 Chain, Callee, args_to_use), 0);
336 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
337 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
338 DAG.getConstant(NumBytes, getPointerTy()));
339 return std::make_pair(TheCall, Chain);
340}
341
342std::pair<SDOperand, SDOperand>
343PPC64TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
344 //vastart just returns the address of the VarArgsFrameIndex slot.
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000345 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
Nate Begemand3e6b942005-04-05 08:51:15 +0000346}
347
348std::pair<SDOperand,SDOperand> PPC64TargetLowering::
349LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
350 const Type *ArgTy, SelectionDAG &DAG) {
351 MVT::ValueType ArgVT = getValueType(ArgTy);
352 SDOperand Result;
353 if (!isVANext) {
354 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
355 } else {
Nate Begemand3e6b942005-04-05 08:51:15 +0000356 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000357 DAG.getConstant(8, VAList.getValueType()));
Nate Begemand3e6b942005-04-05 08:51:15 +0000358 }
359 return std::make_pair(Result, Chain);
360}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000361
Nate Begemand3e6b942005-04-05 08:51:15 +0000362
363std::pair<SDOperand, SDOperand> PPC64TargetLowering::
364LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
365 SelectionDAG &DAG) {
366 assert(0 && "LowerFrameReturnAddress unimplemented");
367 abort();
368}
369
370namespace {
371Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
372Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
373//===--------------------------------------------------------------------===//
374/// ISel - PPC32 specific code to select PPC32 machine instructions for
375/// SelectionDAG operations.
376//===--------------------------------------------------------------------===//
377class ISel : public SelectionDAGISel {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000378
Nate Begemand3e6b942005-04-05 08:51:15 +0000379 /// Comment Here.
380 PPC64TargetLowering PPC64Lowering;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000381
Nate Begemand3e6b942005-04-05 08:51:15 +0000382 /// ExprMap - As shared expressions are codegen'd, we keep track of which
383 /// vreg the value is produced in, so we only emit one copy of each compiled
384 /// tree.
385 std::map<SDOperand, unsigned> ExprMap;
386
387 unsigned GlobalBaseReg;
388 bool GlobalBaseInitialized;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000389
Nate Begemand3e6b942005-04-05 08:51:15 +0000390public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000391 ISel(TargetMachine &TM) : SelectionDAGISel(PPC64Lowering), PPC64Lowering(TM)
Nate Begemand3e6b942005-04-05 08:51:15 +0000392 {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000393
Nate Begemand3e6b942005-04-05 08:51:15 +0000394 /// runOnFunction - Override this function in order to reset our per-function
395 /// variables.
396 virtual bool runOnFunction(Function &Fn) {
397 // Make sure we re-emit a set of the global base reg if necessary
398 GlobalBaseInitialized = false;
399 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000400 }
401
Nate Begemand3e6b942005-04-05 08:51:15 +0000402 /// InstructionSelectBasicBlock - This callback is invoked by
403 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
404 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
405 DEBUG(BB->dump());
406 // Codegen the basic block.
407 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000408
Nate Begemand3e6b942005-04-05 08:51:15 +0000409 // Clear state used for selection.
410 ExprMap.clear();
411 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000412
Nate Begemand3e6b942005-04-05 08:51:15 +0000413 unsigned getGlobalBaseReg();
414 unsigned getConstDouble(double floatVal, unsigned Result);
415 unsigned SelectSetCR0(SDOperand CC);
416 unsigned SelectExpr(SDOperand N);
417 unsigned SelectExprFP(SDOperand N, unsigned Result);
418 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000419
Nate Begemand3e6b942005-04-05 08:51:15 +0000420 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
421 void SelectBranchCC(SDOperand N);
422};
423
424/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
425/// returns zero when the input is not exactly a power of two.
426static unsigned ExactLog2(unsigned Val) {
427 if (Val == 0 || (Val & (Val-1))) return 0;
428 unsigned Count = 0;
429 while (Val != 1) {
430 Val >>= 1;
431 ++Count;
432 }
433 return Count;
434}
435
436/// getImmediateForOpcode - This method returns a value indicating whether
437/// the ConstantSDNode N can be used as an immediate to Opcode. The return
438/// values are either 0, 1 or 2. 0 indicates that either N is not a
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000439/// ConstantSDNode, or is not suitable for use by that opcode. A return value
Nate Begemand3e6b942005-04-05 08:51:15 +0000440/// of 1 indicates that the constant may be used in normal immediate form. A
441/// return value of 2 indicates that the constant may be used in shifted
442/// immediate form. A return value of 3 indicates that log base 2 of the
443/// constant may be used.
444///
445static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
446 unsigned& Imm, bool U = false) {
447 if (N.getOpcode() != ISD::Constant) return 0;
448
449 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000450
Nate Begemand3e6b942005-04-05 08:51:15 +0000451 switch(Opcode) {
452 default: return 0;
453 case ISD::ADD:
454 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
455 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
456 break;
457 case ISD::AND:
458 case ISD::XOR:
459 case ISD::OR:
460 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
461 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
462 break;
463 case ISD::MUL:
464 case ISD::SUB:
465 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
466 break;
467 case ISD::SETCC:
468 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
469 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
470 break;
471 case ISD::SDIV:
472 if ((Imm = ExactLog2(v))) { return 3; }
473 break;
474 }
475 return 0;
476}
477
478/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
479/// to Condition. If the Condition is unordered or unsigned, the bool argument
480/// U is set to true, otherwise it is set to false.
481static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
482 U = false;
483 switch (Condition) {
484 default: assert(0 && "Unknown condition!"); abort();
485 case ISD::SETEQ: return PPC::BEQ;
486 case ISD::SETNE: return PPC::BNE;
487 case ISD::SETULT: U = true;
488 case ISD::SETLT: return PPC::BLT;
489 case ISD::SETULE: U = true;
490 case ISD::SETLE: return PPC::BLE;
491 case ISD::SETUGT: U = true;
492 case ISD::SETGT: return PPC::BGT;
493 case ISD::SETUGE: U = true;
494 case ISD::SETGE: return PPC::BGE;
495 }
496 return 0;
497}
498
499/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
500/// and store immediate instructions.
501static unsigned IndexedOpForOp(unsigned Opcode) {
502 switch(Opcode) {
503 default: assert(0 && "Unknown opcode!"); abort();
504 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
505 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
506 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
507 case PPC::LWZ: return PPC::LWZX; case PPC::STD: return PPC::STDX;
508 case PPC::LD: return PPC::LDX; case PPC::STFS: return PPC::STFSX;
509 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
510 case PPC::LFD: return PPC::LFDX;
511 }
512 return 0;
513}
514}
515
516/// getGlobalBaseReg - Output the instructions required to put the
517/// base address to use for accessing globals into a register.
518///
519unsigned ISel::getGlobalBaseReg() {
520 if (!GlobalBaseInitialized) {
521 // Insert the set of GlobalBaseReg into the first MBB of the function
522 MachineBasicBlock &FirstMBB = BB->getParent()->front();
523 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
524 GlobalBaseReg = MakeReg(MVT::i64);
525 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
526 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
527 GlobalBaseInitialized = true;
528 }
529 return GlobalBaseReg;
530}
531
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000532/// getConstDouble - Loads a floating point value into a register, via the
Nate Begemand3e6b942005-04-05 08:51:15 +0000533/// Constant Pool. Optionally takes a register in which to load the value.
534unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Nate Begemanf3f2d6d2005-04-08 21:26:05 +0000535 unsigned Tmp1 = MakeReg(MVT::i64);
Nate Begemand3e6b942005-04-05 08:51:15 +0000536 if (0 == Result) Result = MakeReg(MVT::f64);
537 MachineConstantPool *CP = BB->getParent()->getConstantPool();
538 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
539 unsigned CPI = CP->getConstantPoolIndex(CFP);
540 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
541 .addConstantPoolIndex(CPI);
542 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
543 return Result;
544}
545
546unsigned ISel::SelectSetCR0(SDOperand CC) {
547 unsigned Opc, Tmp1, Tmp2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548 static const unsigned CompareOpcodes[] =
Nate Begemand3e6b942005-04-05 08:51:15 +0000549 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000550
Nate Begemand3e6b942005-04-05 08:51:15 +0000551 // If the first operand to the select is a SETCC node, then we can fold it
552 // into the branch that selects which value to return.
553 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
554 if (SetCC && CC.getOpcode() == ISD::SETCC) {
555 bool U;
556 Opc = getBCCForSetCC(SetCC->getCondition(), U);
557 Tmp1 = SelectExpr(SetCC->getOperand(0));
558
559 // Pass the optional argument U to getImmediateForOpcode for SETCC,
560 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000561 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begemand3e6b942005-04-05 08:51:15 +0000562 Tmp2, U)) {
563 if (U)
564 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
565 else
566 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
567 } else {
568 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
569 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
570 Tmp2 = SelectExpr(SetCC->getOperand(1));
571 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
572 }
573 } else {
574 Tmp1 = SelectExpr(CC);
575 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
576 Opc = PPC::BNE;
577 }
578 return Opc;
579}
580
581/// Check to see if the load is a constant offset from a base register
582bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
583{
584 unsigned imm = 0, opcode = N.getOpcode();
585 if (N.getOpcode() == ISD::ADD) {
586 Reg = SelectExpr(N.getOperand(0));
587 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
588 offset = imm;
589 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000590 }
Nate Begemand3e6b942005-04-05 08:51:15 +0000591 offset = SelectExpr(N.getOperand(1));
592 return true;
593 }
594 Reg = SelectExpr(N);
595 offset = 0;
596 return false;
597}
598
599void ISel::SelectBranchCC(SDOperand N)
600{
601 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000602 MachineBasicBlock *Dest =
Nate Begemand3e6b942005-04-05 08:51:15 +0000603 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
604
605 // Get the MBB we will fall through to so that we can hand it off to the
606 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
607 //ilist<MachineBasicBlock>::iterator It = BB;
608 //MachineBasicBlock *Fallthrough = ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000609
Nate Begemand3e6b942005-04-05 08:51:15 +0000610 Select(N.getOperand(0)); //chain
611 unsigned Opc = SelectSetCR0(N.getOperand(1));
612 // FIXME: Use this once we have something approximating two-way branches
613 // We cannot currently use this in case the ISel hands us something like
614 // BRcc MBBx
615 // BR MBBy
616 // since the fallthrough basic block for the conditional branch does not start
617 // with the unconditional branch (it is skipped over).
618 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
619 // .addMBB(Dest).addMBB(Fallthrough);
620 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
621 return;
622}
623
624unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
625{
626 unsigned Tmp1, Tmp2, Tmp3;
627 unsigned Opc = 0;
628 SDNode *Node = N.Val;
629 MVT::ValueType DestType = N.getValueType();
630 unsigned opcode = N.getOpcode();
631
632 switch (opcode) {
633 default:
634 Node->dump();
635 assert(0 && "Node not handled!\n");
636
637 case ISD::SELECT: {
638 // Attempt to generate FSEL. We can do this whenever we have an FP result,
639 // and an FP comparison in the SetCC node.
640 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
641 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
642 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
643 SetCC->getCondition() != ISD::SETEQ &&
644 SetCC->getCondition() != ISD::SETNE) {
645 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
646 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
647 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
648 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000649
Nate Begemand3e6b942005-04-05 08:51:15 +0000650 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
651 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
652 switch(SetCC->getCondition()) {
653 default: assert(0 && "Invalid FSEL condition"); abort();
654 case ISD::SETULT:
655 case ISD::SETLT:
656 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
657 return Result;
658 case ISD::SETUGE:
659 case ISD::SETGE:
660 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
661 return Result;
662 case ISD::SETUGT:
663 case ISD::SETGT: {
664 Tmp2 = MakeReg(VT);
665 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
666 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
667 return Result;
668 }
669 case ISD::SETULE:
670 case ISD::SETLE: {
671 Tmp2 = MakeReg(VT);
672 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
673 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
674 return Result;
675 }
676 }
677 } else {
678 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
679 Tmp2 = SelectExpr(SetCC->getOperand(1));
680 Tmp3 = MakeReg(VT);
681 switch(SetCC->getCondition()) {
682 default: assert(0 && "Invalid FSEL condition"); abort();
683 case ISD::SETULT:
684 case ISD::SETLT:
685 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
686 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
687 return Result;
688 case ISD::SETUGE:
689 case ISD::SETGE:
690 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
691 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
692 return Result;
693 case ISD::SETUGT:
694 case ISD::SETGT:
695 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
696 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
697 return Result;
698 case ISD::SETULE:
699 case ISD::SETLE:
700 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
701 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
702 return Result;
703 }
704 }
705 assert(0 && "Should never get here");
706 return 0;
707 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000708
Nate Begemand3e6b942005-04-05 08:51:15 +0000709 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
710 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
711 Opc = SelectSetCR0(N.getOperand(0));
712
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000713 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +0000714 // value and the MBB to hold the PHI instruction for this SetCC.
715 MachineBasicBlock *thisMBB = BB;
716 const BasicBlock *LLVM_BB = BB->getBasicBlock();
717 ilist<MachineBasicBlock>::iterator It = BB;
718 ++It;
719
720 // thisMBB:
721 // ...
722 // TrueVal = ...
723 // cmpTY cr0, r1, r2
724 // bCC copy1MBB
725 // fallthrough --> copy0MBB
726 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
727 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
728 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
729 MachineFunction *F = BB->getParent();
730 F->getBasicBlockList().insert(It, copy0MBB);
731 F->getBasicBlockList().insert(It, sinkMBB);
732 // Update machine-CFG edges
733 BB->addSuccessor(copy0MBB);
734 BB->addSuccessor(sinkMBB);
735
736 // copy0MBB:
737 // %FalseValue = ...
738 // # fallthrough to sinkMBB
739 BB = copy0MBB;
740 // Update machine-CFG edges
741 BB->addSuccessor(sinkMBB);
742
743 // sinkMBB:
744 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
745 // ...
746 BB = sinkMBB;
747 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
748 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
749 return Result;
750 }
751
752 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000753 if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000754 ISD::ADD == N.getOperand(0).getOpcode() &&
755 N.getOperand(0).Val->hasOneUse() &&
756 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
757 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
758 ++FusedFP; // Statistic
759 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
760 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
761 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
762 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
763 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000764 } else if (!NoExcessFPPrecision &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000765 ISD::SUB == N.getOperand(0).getOpcode() &&
766 N.getOperand(0).Val->hasOneUse() &&
767 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
768 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
769 ++FusedFP; // Statistic
770 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
771 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
772 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
773 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
774 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
775 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
776 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
777 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
778 } else {
779 Tmp1 = SelectExpr(N.getOperand(0));
780 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
781 }
782 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000783
Nate Begemand3e6b942005-04-05 08:51:15 +0000784 case ISD::FABS:
785 Tmp1 = SelectExpr(N.getOperand(0));
786 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
787 return Result;
788
789 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000790 assert (DestType == MVT::f32 &&
791 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000792 "only f64 to f32 conversion supported here");
793 Tmp1 = SelectExpr(N.getOperand(0));
794 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
795 return Result;
796
797 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000798 assert (DestType == MVT::f64 &&
799 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemand3e6b942005-04-05 08:51:15 +0000800 "only f32 to f64 conversion supported here");
801 Tmp1 = SelectExpr(N.getOperand(0));
802 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
803 return Result;
804
805 case ISD::CopyFromReg:
806 if (Result == 1)
807 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
808 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
809 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
810 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000811
Nate Begemand3e6b942005-04-05 08:51:15 +0000812 case ISD::ConstantFP: {
813 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
814 Result = getConstDouble(CN->getValue(), Result);
815 return Result;
816 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000817
Nate Begemand3e6b942005-04-05 08:51:15 +0000818 case ISD::ADD:
819 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
820 N.getOperand(0).Val->hasOneUse()) {
821 ++FusedFP; // Statistic
822 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
823 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
824 Tmp3 = SelectExpr(N.getOperand(1));
825 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
826 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
827 return Result;
828 }
829 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
830 Tmp1 = SelectExpr(N.getOperand(0));
831 Tmp2 = SelectExpr(N.getOperand(1));
832 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
833 return Result;
834
835 case ISD::SUB:
836 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
837 N.getOperand(0).Val->hasOneUse()) {
838 ++FusedFP; // Statistic
839 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
840 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
841 Tmp3 = SelectExpr(N.getOperand(1));
842 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
843 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
844 return Result;
845 }
846 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
847 Tmp1 = SelectExpr(N.getOperand(0));
848 Tmp2 = SelectExpr(N.getOperand(1));
849 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
850 return Result;
851
852 case ISD::MUL:
853 case ISD::SDIV:
854 switch( opcode ) {
855 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
856 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
857 };
858 Tmp1 = SelectExpr(N.getOperand(0));
859 Tmp2 = SelectExpr(N.getOperand(1));
860 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
861 return Result;
862
863 case ISD::UINT_TO_FP:
864 case ISD::SINT_TO_FP: {
865 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
866 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
867 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
868 Tmp3 = MakeReg(MVT::i64); // temp reg to hold the conversion constant
869 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000870
Nate Begemand3e6b942005-04-05 08:51:15 +0000871 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
872 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000873
Nate Begemand3e6b942005-04-05 08:51:15 +0000874 // FIXME: pull this FP constant generation stuff out into something like
875 // the simple ISel's getReg.
876 if (IsUnsigned) {
877 addFrameReference(BuildMI(BB, PPC::STD, 3).addReg(Tmp1), FrameIdx);
878 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
879 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp2);
880 } else {
881 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
882 unsigned CPI = CP->getConstantPoolIndex(CFP);
883 // Load constant fp value
884 unsigned Tmp4 = MakeReg(MVT::i32);
885 unsigned TmpL = MakeReg(MVT::i32);
886 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
887 .addConstantPoolIndex(CPI);
888 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
889 // Store the hi & low halves of the fp value, currently in int regs
890 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
891 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
892 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
893 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
894 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
895 // Generate the return value with a subtract
896 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
897 }
898 return Result;
899 }
900 }
901 assert(0 && "Should never get here");
902 return 0;
903}
904
905unsigned ISel::SelectExpr(SDOperand N) {
906 unsigned Result;
907 unsigned Tmp1, Tmp2, Tmp3;
908 unsigned Opc = 0;
909 unsigned opcode = N.getOpcode();
910
911 SDNode *Node = N.Val;
912 MVT::ValueType DestType = N.getValueType();
913
914 unsigned &Reg = ExprMap[N];
915 if (Reg) return Reg;
916
917 switch (N.getOpcode()) {
918 default:
919 Reg = Result = (N.getValueType() != MVT::Other) ?
920 MakeReg(N.getValueType()) : 1;
921 break;
922 case ISD::CALL:
923 // If this is a call instruction, make sure to prepare ALL of the result
924 // values as well as the chain.
925 if (Node->getNumValues() == 1)
926 Reg = Result = 1; // Void call, just a chain.
927 else {
928 Result = MakeReg(Node->getValueType(0));
929 ExprMap[N.getValue(0)] = Result;
930 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
931 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
932 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
933 }
934 break;
935 }
936
937 if (ISD::CopyFromReg == opcode)
938 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000939
Nate Begemand3e6b942005-04-05 08:51:15 +0000940 if (DestType == MVT::f64 || DestType == MVT::f32)
941 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
942 return SelectExprFP(N, Result);
943
944 switch (opcode) {
945 default:
946 Node->dump();
947 assert(0 && "Node not handled!\n");
948 case ISD::UNDEF:
949 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
950 return Result;
951 case ISD::DYNAMIC_STACKALLOC:
952 // Generate both result values. FIXME: Need a better commment here?
953 if (Result != 1)
954 ExprMap[N.getValue(1)] = 1;
955 else
956 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
957
958 // FIXME: We are currently ignoring the requested alignment for handling
959 // greater than the stack alignment. This will need to be revisited at some
960 // point. Align = N.getOperand(2);
961 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
962 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
963 std::cerr << "Cannot allocate stack object with greater alignment than"
964 << " the stack alignment yet!";
965 abort();
966 }
967 Select(N.getOperand(0));
968 Tmp1 = SelectExpr(N.getOperand(1));
969 // Subtract size from stack pointer, thereby allocating some space.
970 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
971 // Put a pointer to the space into the result register by copying the SP
972 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
973 return Result;
974
975 case ISD::ConstantPool:
976 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
977 Tmp2 = MakeReg(MVT::i64);
978 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
979 .addConstantPoolIndex(Tmp1);
980 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
981 return Result;
982
983 case ISD::FrameIndex:
984 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
985 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
986 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000987
Nate Begemand3e6b942005-04-05 08:51:15 +0000988 case ISD::GlobalAddress: {
989 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
990 Tmp1 = MakeReg(MVT::i64);
991 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
992 .addGlobalAddress(GV);
993 if (GV->hasWeakLinkage() || GV->isExternal()) {
Nate Begemana9532d52005-04-08 23:45:01 +0000994 BuildMI(BB, PPC::LD, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +0000995 } else {
996 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
997 }
998 return Result;
999 }
1000
1001 case ISD::LOAD:
1002 case ISD::EXTLOAD:
1003 case ISD::ZEXTLOAD:
1004 case ISD::SEXTLOAD: {
1005 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1006 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
1007 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001008
Nate Begemand3e6b942005-04-05 08:51:15 +00001009 // Make sure we generate both values.
1010 if (Result != 1)
1011 ExprMap[N.getValue(1)] = 1; // Generate the token
1012 else
1013 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1014
1015 SDOperand Chain = N.getOperand(0);
1016 SDOperand Address = N.getOperand(1);
1017 Select(Chain);
1018
1019 switch (TypeBeingLoaded) {
1020 default: Node->dump(); assert(0 && "Cannot load this type!");
1021 case MVT::i1: Opc = PPC::LBZ; break;
1022 case MVT::i8: Opc = PPC::LBZ; break;
1023 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1024 case MVT::i32: Opc = sext ? PPC::LWA : PPC::LWZ; break;
1025 case MVT::i64: Opc = PPC::LD; break;
1026 case MVT::f32: Opc = PPC::LFS; break;
1027 case MVT::f64: Opc = PPC::LFD; break;
1028 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001029
Nate Begemand3e6b942005-04-05 08:51:15 +00001030 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1031 Tmp1 = MakeReg(MVT::i64);
1032 int CPI = CP->getIndex();
1033 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1034 .addConstantPoolIndex(CPI);
1035 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
1036 }
1037 else if(Address.getOpcode() == ISD::FrameIndex) {
1038 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1039 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
1040 } else {
1041 int offset;
1042 bool idx = SelectAddr(Address, Tmp1, offset);
1043 if (idx) {
1044 Opc = IndexedOpForOp(Opc);
1045 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1046 } else {
1047 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1048 }
1049 }
1050 return Result;
1051 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001052
Nate Begemand3e6b942005-04-05 08:51:15 +00001053 case ISD::CALL: {
1054 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001055 static const unsigned GPR[] = {
Nate Begemand3e6b942005-04-05 08:51:15 +00001056 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1057 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1058 };
1059 static const unsigned FPR[] = {
1060 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1061 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1062 };
1063
1064 // Lower the chain for this call.
1065 Select(N.getOperand(0));
1066 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
1067
1068 MachineInstr *CallMI;
1069 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001070 if (GlobalAddressSDNode *GASD =
Nate Begemand3e6b942005-04-05 08:51:15 +00001071 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001072 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001073 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001074 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand3e6b942005-04-05 08:51:15 +00001075 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001076 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand3e6b942005-04-05 08:51:15 +00001077 true);
1078 } else {
1079 Tmp1 = SelectExpr(N.getOperand(1));
1080 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1081 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1082 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1083 .addReg(PPC::R12);
1084 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001085
Nate Begemand3e6b942005-04-05 08:51:15 +00001086 // Load the register args to virtual regs
1087 std::vector<unsigned> ArgVR;
1088 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
1089 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1090
1091 // Copy the virtual registers into the appropriate argument register
1092 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1093 switch(N.getOperand(i+2).getValueType()) {
1094 default: Node->dump(); assert(0 && "Unknown value type for call");
1095 case MVT::i1:
1096 case MVT::i8:
1097 case MVT::i16:
1098 case MVT::i32:
1099 case MVT::i64:
1100 assert(GPR_idx < 8 && "Too many int args");
1101 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
1102 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1103 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1104 }
1105 ++GPR_idx;
1106 break;
1107 case MVT::f64:
1108 case MVT::f32:
1109 assert(FPR_idx < 13 && "Too many fp args");
1110 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1111 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1112 ++FPR_idx;
1113 break;
1114 }
1115 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001116
Nate Begemand3e6b942005-04-05 08:51:15 +00001117 // Put the call instruction in the correct place in the MachineBasicBlock
1118 BB->push_back(CallMI);
1119
1120 switch (Node->getValueType(0)) {
1121 default: assert(0 && "Unknown value type for call result!");
1122 case MVT::Other: return 1;
1123 case MVT::i1:
1124 case MVT::i8:
1125 case MVT::i16:
1126 case MVT::i32:
1127 case MVT::i64:
1128 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1129 break;
1130 case MVT::f32:
1131 case MVT::f64:
1132 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1133 break;
1134 }
1135 return Result+N.ResNo;
1136 }
1137
1138 case ISD::SIGN_EXTEND:
1139 case ISD::SIGN_EXTEND_INREG:
1140 Tmp1 = SelectExpr(N.getOperand(0));
1141 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1142 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1143 case MVT::i32:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001144 BuildMI(BB, PPC::EXTSW, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001145 break;
1146 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001147 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001148 break;
1149 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001150 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begemand3e6b942005-04-05 08:51:15 +00001151 break;
1152 case MVT::i1:
1153 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1154 break;
1155 }
1156 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001157
Nate Begemand3e6b942005-04-05 08:51:15 +00001158 case ISD::CopyFromReg:
1159 if (Result == 1)
1160 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1161 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1162 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1163 return Result;
1164
1165 case ISD::SHL:
1166 Tmp1 = SelectExpr(N.getOperand(0));
1167 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001168 Tmp2 = CN->getValue() & 0x3F;
1169 BuildMI(BB, PPC::RLDICR, 3, Result).addReg(Tmp1).addImm(Tmp2)
1170 .addImm(63-Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001171 } else {
1172 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001173 BuildMI(BB, PPC::SLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001174 }
1175 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001176
Nate Begemand3e6b942005-04-05 08:51:15 +00001177 case ISD::SRL:
1178 Tmp1 = SelectExpr(N.getOperand(0));
1179 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001180 Tmp2 = CN->getValue() & 0x3F;
1181 BuildMI(BB, PPC::RLDICL, 3, Result).addReg(Tmp1).addImm(64-Tmp2)
1182 .addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001183 } else {
1184 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001185 BuildMI(BB, PPC::SRD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001186 }
1187 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001188
Nate Begemand3e6b942005-04-05 08:51:15 +00001189 case ISD::SRA:
1190 Tmp1 = SelectExpr(N.getOperand(0));
1191 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Nate Begemana9532d52005-04-08 23:45:01 +00001192 Tmp2 = CN->getValue() & 0x3F;
1193 BuildMI(BB, PPC::SRADI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001194 } else {
1195 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana9532d52005-04-08 23:45:01 +00001196 BuildMI(BB, PPC::SRAD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemand3e6b942005-04-05 08:51:15 +00001197 }
1198 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001199
Nate Begemand3e6b942005-04-05 08:51:15 +00001200 case ISD::ADD:
1201 Tmp1 = SelectExpr(N.getOperand(0));
1202 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1203 default: assert(0 && "unhandled result code");
1204 case 0: // No immediate
1205 Tmp2 = SelectExpr(N.getOperand(1));
1206 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1207 break;
1208 case 1: // Low immediate
1209 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1210 break;
1211 case 2: // Shifted immediate
1212 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1213 break;
1214 }
1215 return Result;
1216
1217 case ISD::AND:
1218 case ISD::OR:
1219 Tmp1 = SelectExpr(N.getOperand(0));
1220 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1221 default: assert(0 && "unhandled result code");
1222 case 0: // No immediate
1223 Tmp2 = SelectExpr(N.getOperand(1));
1224 switch (opcode) {
1225 case ISD::AND: Opc = PPC::AND; break;
1226 case ISD::OR: Opc = PPC::OR; break;
1227 }
1228 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1229 break;
1230 case 1: // Low immediate
1231 switch (opcode) {
1232 case ISD::AND: Opc = PPC::ANDIo; break;
1233 case ISD::OR: Opc = PPC::ORI; break;
1234 }
1235 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1236 break;
1237 case 2: // Shifted immediate
1238 switch (opcode) {
1239 case ISD::AND: Opc = PPC::ANDISo; break;
1240 case ISD::OR: Opc = PPC::ORIS; break;
1241 }
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1243 break;
1244 }
1245 return Result;
1246
1247 case ISD::XOR: {
1248 // Check for EQV: xor, (xor a, -1), b
1249 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1250 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1251 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1252 ++NotLogic;
1253 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1254 Tmp2 = SelectExpr(N.getOperand(1));
1255 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1256 return Result;
1257 }
1258 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1259 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1260 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1261 ++NotLogic;
1262 switch(N.getOperand(0).getOpcode()) {
1263 case ISD::OR:
1264 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1265 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1266 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1267 break;
1268 case ISD::AND:
1269 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1270 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1271 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1272 break;
1273 default:
1274 Tmp1 = SelectExpr(N.getOperand(0));
1275 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1276 break;
1277 }
1278 return Result;
1279 }
1280 Tmp1 = SelectExpr(N.getOperand(0));
1281 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1282 default: assert(0 && "unhandled result code");
1283 case 0: // No immediate
1284 Tmp2 = SelectExpr(N.getOperand(1));
1285 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1286 break;
1287 case 1: // Low immediate
1288 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1289 break;
1290 case 2: // Shifted immediate
1291 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1292 break;
1293 }
1294 return Result;
1295 }
1296
1297 case ISD::SUB:
1298 Tmp2 = SelectExpr(N.getOperand(1));
1299 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1300 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1301 else {
1302 Tmp1 = SelectExpr(N.getOperand(0));
1303 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1304 }
1305 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001306
Nate Begemand3e6b942005-04-05 08:51:15 +00001307 case ISD::MUL:
1308 Tmp1 = SelectExpr(N.getOperand(0));
1309 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1310 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1311 else {
1312 Tmp2 = SelectExpr(N.getOperand(1));
1313 BuildMI(BB, PPC::MULLD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1314 }
1315 return Result;
1316
1317 case ISD::SDIV:
1318 case ISD::UDIV:
1319 if (3 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1320 Tmp1 = MakeReg(MVT::i64);
1321 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begemana9532d52005-04-08 23:45:01 +00001322 BuildMI(BB, PPC::SRADI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
Nate Begemand3e6b942005-04-05 08:51:15 +00001323 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1324 return Result;
1325 }
1326 Tmp1 = SelectExpr(N.getOperand(0));
1327 Tmp2 = SelectExpr(N.getOperand(1));
1328 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1329 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1330 return Result;
1331
Nate Begemand3e6b942005-04-05 08:51:15 +00001332 case ISD::FP_TO_UINT:
1333 case ISD::FP_TO_SINT: {
Nate Begemand3e6b942005-04-05 08:51:15 +00001334 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemana3829d52005-04-05 17:32:30 +00001335 Tmp2 = MakeReg(MVT::f64);
1336 BuildMI(BB, PPC::FCTIDZ, 1, Tmp2).addReg(Tmp1);
1337 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1338 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1339 addFrameReference(BuildMI(BB, PPC::LD, 2, Result), FrameIdx);
1340 return Result;
Nate Begemand3e6b942005-04-05 08:51:15 +00001341 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001342
Nate Begemand3e6b942005-04-05 08:51:15 +00001343 case ISD::SETCC:
1344 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1345 Opc = SelectSetCR0(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001346
Nate Begemand3e6b942005-04-05 08:51:15 +00001347 unsigned TrueValue = MakeReg(MVT::i32);
1348 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1349 unsigned FalseValue = MakeReg(MVT::i32);
1350 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1351
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001352 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001353 // value and the MBB to hold the PHI instruction for this SetCC.
1354 MachineBasicBlock *thisMBB = BB;
1355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1356 ilist<MachineBasicBlock>::iterator It = BB;
1357 ++It;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001358
Nate Begemand3e6b942005-04-05 08:51:15 +00001359 // thisMBB:
1360 // ...
1361 // cmpTY cr0, r1, r2
1362 // %TrueValue = li 1
1363 // bCC sinkMBB
1364 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1365 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1366 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1367 MachineFunction *F = BB->getParent();
1368 F->getBasicBlockList().insert(It, copy0MBB);
1369 F->getBasicBlockList().insert(It, sinkMBB);
1370 // Update machine-CFG edges
1371 BB->addSuccessor(copy0MBB);
1372 BB->addSuccessor(sinkMBB);
1373
1374 // copy0MBB:
1375 // %FalseValue = li 0
1376 // fallthrough
1377 BB = copy0MBB;
1378 // Update machine-CFG edges
1379 BB->addSuccessor(sinkMBB);
1380
1381 // sinkMBB:
1382 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1383 // ...
1384 BB = sinkMBB;
1385 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1386 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1387 return Result;
1388 }
1389 assert(0 && "Is this legal?");
1390 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001391
Nate Begemand3e6b942005-04-05 08:51:15 +00001392 case ISD::SELECT: {
1393 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1394 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1395 Opc = SelectSetCR0(N.getOperand(0));
1396
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001397 // Create an iterator with which to insert the MBB for copying the false
Nate Begemand3e6b942005-04-05 08:51:15 +00001398 // value and the MBB to hold the PHI instruction for this SetCC.
1399 MachineBasicBlock *thisMBB = BB;
1400 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1401 ilist<MachineBasicBlock>::iterator It = BB;
1402 ++It;
1403
1404 // thisMBB:
1405 // ...
1406 // TrueVal = ...
1407 // cmpTY cr0, r1, r2
1408 // bCC copy1MBB
1409 // fallthrough --> copy0MBB
1410 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1411 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1412 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1413 MachineFunction *F = BB->getParent();
1414 F->getBasicBlockList().insert(It, copy0MBB);
1415 F->getBasicBlockList().insert(It, sinkMBB);
1416 // Update machine-CFG edges
1417 BB->addSuccessor(copy0MBB);
1418 BB->addSuccessor(sinkMBB);
1419
1420 // copy0MBB:
1421 // %FalseValue = ...
1422 // # fallthrough to sinkMBB
1423 BB = copy0MBB;
1424 // Update machine-CFG edges
1425 BB->addSuccessor(sinkMBB);
1426
1427 // sinkMBB:
1428 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1429 // ...
1430 BB = sinkMBB;
1431 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1432 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1433
1434 // FIXME: Select i64?
1435 return Result;
1436 }
1437
1438 case ISD::Constant:
1439 switch (N.getValueType()) {
1440 default: assert(0 && "Cannot use constants of this type!");
1441 case MVT::i1:
1442 BuildMI(BB, PPC::LI, 1, Result)
1443 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1444 break;
1445 case MVT::i32:
1446 {
1447 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1448 if (v < 32768 && v >= -32768) {
1449 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1450 } else {
1451 Tmp1 = MakeReg(MVT::i32);
1452 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1453 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
1454 }
1455 }
1456 }
1457 return Result;
1458 }
1459
1460 return 0;
1461}
1462
1463void ISel::Select(SDOperand N) {
1464 unsigned Tmp1, Tmp2, Opc;
1465 unsigned opcode = N.getOpcode();
1466
1467 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1468 return; // Already selected.
1469
1470 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001471
Nate Begemand3e6b942005-04-05 08:51:15 +00001472 switch (Node->getOpcode()) {
1473 default:
1474 Node->dump(); std::cerr << "\n";
1475 assert(0 && "Node not handled yet!");
1476 case ISD::EntryToken: return; // Noop
1477 case ISD::TokenFactor:
1478 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1479 Select(Node->getOperand(i));
1480 return;
1481 case ISD::ADJCALLSTACKDOWN:
1482 case ISD::ADJCALLSTACKUP:
1483 Select(N.getOperand(0));
1484 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1485 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1486 PPC::ADJCALLSTACKUP;
1487 BuildMI(BB, Opc, 1).addImm(Tmp1);
1488 return;
1489 case ISD::BR: {
1490 MachineBasicBlock *Dest =
1491 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1492 Select(N.getOperand(0));
1493 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1494 return;
1495 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001496 case ISD::BRCOND:
Nate Begemand3e6b942005-04-05 08:51:15 +00001497 SelectBranchCC(N);
1498 return;
1499 case ISD::CopyToReg:
1500 Select(N.getOperand(0));
1501 Tmp1 = SelectExpr(N.getOperand(1));
1502 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001503
Nate Begemand3e6b942005-04-05 08:51:15 +00001504 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001505 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemand3e6b942005-04-05 08:51:15 +00001506 N.getOperand(1).getValueType() == MVT::f32)
1507 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1508 else
1509 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1510 }
1511 return;
1512 case ISD::ImplicitDef:
1513 Select(N.getOperand(0));
1514 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1515 return;
1516 case ISD::RET:
1517 switch (N.getNumOperands()) {
1518 default:
1519 assert(0 && "Unknown return instruction!");
1520 case 3:
1521 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1522 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001523 "Unknown two-register value!");
Nate Begemand3e6b942005-04-05 08:51:15 +00001524 Select(N.getOperand(0));
1525 Tmp1 = SelectExpr(N.getOperand(1));
1526 Tmp2 = SelectExpr(N.getOperand(2));
1527 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1528 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
1529 break;
1530 case 2:
1531 Select(N.getOperand(0));
1532 Tmp1 = SelectExpr(N.getOperand(1));
1533 switch (N.getOperand(1).getValueType()) {
1534 default:
1535 assert(0 && "Unknown return type!");
1536 case MVT::f64:
1537 case MVT::f32:
1538 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1539 break;
1540 case MVT::i32:
1541 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1542 break;
1543 }
1544 case 1:
1545 Select(N.getOperand(0));
1546 break;
1547 }
1548 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1549 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001550 case ISD::TRUNCSTORE:
1551 case ISD::STORE:
Nate Begemand3e6b942005-04-05 08:51:15 +00001552 {
1553 SDOperand Chain = N.getOperand(0);
1554 SDOperand Value = N.getOperand(1);
1555 SDOperand Address = N.getOperand(2);
1556 Select(Chain);
1557
1558 Tmp1 = SelectExpr(Value); //value
1559
1560 if (opcode == ISD::STORE) {
1561 switch(Value.getValueType()) {
1562 default: assert(0 && "unknown Type in store");
1563 case MVT::i64: Opc = PPC::STD; break;
1564 case MVT::f64: Opc = PPC::STFD; break;
1565 case MVT::f32: Opc = PPC::STFS; break;
1566 }
1567 } else { //ISD::TRUNCSTORE
1568 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1569 default: assert(0 && "unknown Type in store");
1570 case MVT::i1: //FIXME: DAG does not promote this load
1571 case MVT::i8: Opc= PPC::STB; break;
1572 case MVT::i16: Opc = PPC::STH; break;
1573 case MVT::i32: Opc = PPC::STW; break;
1574 }
1575 }
1576
1577 if(Address.getOpcode() == ISD::FrameIndex)
1578 {
1579 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1580 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
1581 }
1582 else
1583 {
1584 int offset;
1585 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001586 if (idx) {
Nate Begemand3e6b942005-04-05 08:51:15 +00001587 Opc = IndexedOpForOp(Opc);
1588 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1589 } else {
1590 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1591 }
1592 }
1593 return;
1594 }
1595 case ISD::EXTLOAD:
1596 case ISD::SEXTLOAD:
1597 case ISD::ZEXTLOAD:
1598 case ISD::LOAD:
1599 case ISD::CopyFromReg:
1600 case ISD::CALL:
1601 case ISD::DYNAMIC_STACKALLOC:
1602 ExprMap.erase(N);
1603 SelectExpr(N);
1604 return;
1605 }
1606 assert(0 && "Should not be reached!");
1607}
1608
1609
1610/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1611/// into a machine code representation using pattern matching and a machine
1612/// description file.
1613///
1614FunctionPass *llvm::createPPC64ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001615 return new ISel(TM);
Nate Begemand3e6b942005-04-05 08:51:15 +00001616}
1617