blob: 19ef823ad61e2d77b4fd7e8596b1c5061cf779ea [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000020#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000021#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
Nate Begemanadeb43d2005-07-20 22:42:00 +000038// FIXME: temporary.
39#include "llvm/Support/CommandLine.h"
40static cl::opt<bool> EnableGPOPT("enable-gpopt", cl::Hidden,
41 cl::desc("Enable optimizations for GP cpus"));
42
Nate Begemana9795f82005-03-24 04:41:43 +000043//===----------------------------------------------------------------------===//
44// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
45namespace {
46 class PPC32TargetLowering : public TargetLowering {
47 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
48 int ReturnAddrIndex; // FrameIndex for return slot.
49 public:
50 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000051 // Fold away setcc operations if possible.
52 setSetCCIsExpensive();
53
Nate Begemana9795f82005-03-24 04:41:43 +000054 // Set up the register classes.
55 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000056 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000057 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000058
Nate Begeman74d73452005-03-31 00:15:26 +000059 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000060 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
61 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
62 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
63
Nate Begeman74d73452005-03-31 00:15:26 +000064 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
65 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
66 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000067
Nate Begeman815d6da2005-04-06 00:25:27 +000068 // PowerPC has no SREM/UREM instructions
69 setOperationAction(ISD::SREM, MVT::i32, Expand);
70 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000071
Chris Lattner32f3cf62005-05-13 16:20:22 +000072 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000073 setOperationAction(ISD::FSIN , MVT::f64, Expand);
74 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000075 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000076 setOperationAction(ISD::FSIN , MVT::f32, Expand);
77 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000078 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000079
Nate Begemanadeb43d2005-07-20 22:42:00 +000080 // If we're enabling GP optimizations, use hardware square root
81 if (!EnableGPOPT) {
82 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
83 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
84 }
85
Nate Begemand7c4a4a2005-05-11 23:43:56 +000086 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000087 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
88 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000089
Chris Lattnercbd06fc2005-04-07 19:41:49 +000090 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000091 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000093
Nate Begemana9795f82005-03-24 04:41:43 +000094 computeRegisterProperties();
95 }
96
97 /// LowerArguments - This hook must be implemented to indicate how we should
98 /// lower the arguments for the specified function, into the specified DAG.
99 virtual std::vector<SDOperand>
100 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000101
Nate Begemana9795f82005-03-24 04:41:43 +0000102 /// LowerCallTo - This hook lowers an abstract call to a function into an
103 /// actual call.
104 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000105 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000106 bool isTailCall, SDOperand Callee, ArgListTy &Args,
107 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000108
Chris Lattnere0fe2252005-07-05 19:58:54 +0000109 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
110 Value *VAListV, SelectionDAG &DAG);
111
Nate Begemana9795f82005-03-24 04:41:43 +0000112 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000113 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
114 const Type *ArgTy, SelectionDAG &DAG);
115
Nate Begemana9795f82005-03-24 04:41:43 +0000116 virtual std::pair<SDOperand, SDOperand>
117 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
118 SelectionDAG &DAG);
119 };
120}
121
122
123std::vector<SDOperand>
124PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
125 //
126 // add beautiful description of PPC stack frame format, or at least some docs
127 //
128 MachineFunction &MF = DAG.getMachineFunction();
129 MachineFrameInfo *MFI = MF.getFrameInfo();
130 MachineBasicBlock& BB = MF.front();
131 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132
133 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000134 // fixed size array of physical args, for the sake of simplicity let the STL
135 // handle tracking them for us.
136 std::vector<unsigned> argVR, argPR, argOp;
137 unsigned ArgOffset = 24;
138 unsigned GPR_remaining = 8;
139 unsigned FPR_remaining = 13;
140 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000141 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000142 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
143 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
144 };
145 static const unsigned FPR[] = {
146 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
147 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
148 };
149
150 // Add DAG nodes to load the arguments... On entry to a function on PPC,
151 // the arguments start at offset 24, although they are likely to be passed
152 // in registers.
153 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
154 SDOperand newroot, argt;
155 unsigned ObjSize;
156 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000157 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000158 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000159
Nate Begemana9795f82005-03-24 04:41:43 +0000160 switch (ObjectVT) {
161 default: assert(0 && "Unhandled argument type!");
162 case MVT::i1:
163 case MVT::i8:
164 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000165 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000166 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000167 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000168 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000169 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000170 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
171 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000172 if (ObjectVT != MVT::i32)
173 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000174 } else {
175 needsLoad = true;
176 }
177 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000178 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000179 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000180 if (GPR_remaining > 0) {
181 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000182 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000183 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
184 // If we have two or more remaining argument registers, then both halves
185 // of the i64 can be sourced from there. Otherwise, the lower half will
186 // have to come off the stack. This can happen when an i64 is preceded
187 // by 28 bytes of arguments.
188 if (GPR_remaining > 1) {
189 MF.addLiveIn(GPR[GPR_idx+1]);
190 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
191 } else {
192 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
193 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000194 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
195 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000196 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000197 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000198 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
199 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000200 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000202 }
203 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000204 case MVT::f32:
205 case MVT::f64:
206 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
207 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000208 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000209 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000211 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000212 --FPR_remaining;
213 ++FPR_idx;
214 } else {
215 needsLoad = true;
216 }
217 break;
218 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219
Nate Begemana9795f82005-03-24 04:41:43 +0000220 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000221 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000222 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000223 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000224 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000225 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000226 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
227 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000228 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000229 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000230 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
231 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000232 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000233
Nate Begemana9795f82005-03-24 04:41:43 +0000234 // Every 4 bytes of argument space consumes one of the GPRs available for
235 // argument passing.
236 if (GPR_remaining > 0) {
237 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
238 GPR_remaining -= delta;
239 GPR_idx += delta;
240 }
241 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000242 if (newroot.Val)
243 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000244
Nate Begemana9795f82005-03-24 04:41:43 +0000245 ArgValues.push_back(argt);
246 }
247
Nate Begemana9795f82005-03-24 04:41:43 +0000248 // If the function takes variable number of arguments, make a frame index for
249 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000250 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000251 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000252 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000253 // If this function is vararg, store any remaining integer argument regs
254 // to their spots on the stack so that they may be loaded by deferencing the
255 // result of va_next.
256 std::vector<SDOperand> MemOps;
257 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000258 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000259 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000260 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000261 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000262 MemOps.push_back(Store);
263 // Increment the address by four for the next argument to store
264 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
265 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
266 }
267 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000268 }
Nate Begemana9795f82005-03-24 04:41:43 +0000269
Nate Begemancd08e4c2005-04-09 20:09:12 +0000270 // Finally, inform the code generator which regs we return values in.
271 switch (getValueType(F.getReturnType())) {
272 default: assert(0 && "Unknown type!");
273 case MVT::isVoid: break;
274 case MVT::i1:
275 case MVT::i8:
276 case MVT::i16:
277 case MVT::i32:
278 MF.addLiveOut(PPC::R3);
279 break;
280 case MVT::i64:
281 MF.addLiveOut(PPC::R3);
282 MF.addLiveOut(PPC::R4);
283 break;
284 case MVT::f32:
285 case MVT::f64:
286 MF.addLiveOut(PPC::F1);
287 break;
288 }
289
Nate Begemana9795f82005-03-24 04:41:43 +0000290 return ArgValues;
291}
292
293std::pair<SDOperand, SDOperand>
294PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000295 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000296 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000297 SDOperand Callee, ArgListTy &Args,
298 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000299 // args_to_use will accumulate outgoing args for the ISD::CALL case in
300 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000301 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000302
303 // Count how many bytes are to be pushed on the stack, including the linkage
304 // area, and parameter passing area.
305 unsigned NumBytes = 24;
306
307 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000308 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000309 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000310 } else {
311 for (unsigned i = 0, e = Args.size(); i != e; ++i)
312 switch (getValueType(Args[i].second)) {
313 default: assert(0 && "Unknown value type!");
314 case MVT::i1:
315 case MVT::i8:
316 case MVT::i16:
317 case MVT::i32:
318 case MVT::f32:
319 NumBytes += 4;
320 break;
321 case MVT::i64:
322 case MVT::f64:
323 NumBytes += 8;
324 break;
325 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000326
327 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000328 // plus 32 bytes of argument space in case any called code gets funky on us.
329 if (NumBytes < 56) NumBytes = 56;
330
331 // Adjust the stack pointer for the new arguments...
332 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000333 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000334 DAG.getConstant(NumBytes, getPointerTy()));
335
336 // Set up a copy of the stack pointer for use loading and storing any
337 // arguments that may not fit in the registers available for argument
338 // passing.
339 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
340 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000341
Nate Begeman307e7442005-03-26 01:28:53 +0000342 // Figure out which arguments are going to go in registers, and which in
343 // memory. Also, if this is a vararg function, floating point operations
344 // must be stored to our stack, and loaded into integer regs as well, if
345 // any integer regs are available for argument passing.
346 unsigned ArgOffset = 24;
347 unsigned GPR_remaining = 8;
348 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000349
Nate Begeman74d73452005-03-31 00:15:26 +0000350 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000351 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
352 // PtrOff will be used to store the current argument to the stack if a
353 // register cannot be found for it.
354 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
355 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000356 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000357
Nate Begemanf7e43382005-03-26 07:46:36 +0000358 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000359 default: assert(0 && "Unexpected ValueType for argument!");
360 case MVT::i1:
361 case MVT::i8:
362 case MVT::i16:
363 // Promote the integer to 32 bits. If the input type is signed use a
364 // sign extend, otherwise use a zero extend.
365 if (Args[i].second->isSigned())
366 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
367 else
368 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
369 // FALL THROUGH
370 case MVT::i32:
371 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000372 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000373 --GPR_remaining;
374 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000375 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000376 Args[i].first, PtrOff,
377 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000378 }
379 ArgOffset += 4;
380 break;
381 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000382 // If we have one free GPR left, we can place the upper half of the i64
383 // in it, and store the other half to the stack. If we have two or more
384 // free GPRs, then we can pass both halves of the i64 in registers.
385 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000386 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000387 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000388 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000389 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000390 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000391 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000392 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000393 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000394 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000395 } else {
396 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
397 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000398 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000399 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000400 }
Nate Begeman307e7442005-03-26 01:28:53 +0000401 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000402 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000403 Args[i].first, PtrOff,
404 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000405 }
406 ArgOffset += 8;
407 break;
408 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000409 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000410 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000411 args_to_use.push_back(Args[i].first);
412 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000413 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000414 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000415 Args[i].first, PtrOff,
416 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000417 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000418 // Float varargs are always shadowed in available integer registers
419 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000420 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
421 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000422 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000423 args_to_use.push_back(Load);
424 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000425 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000426 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000427 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
428 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000429 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
430 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000431 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000432 args_to_use.push_back(Load);
433 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000434 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000435 } else {
436 // If we have any FPRs remaining, we may also have GPRs remaining.
437 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
438 // GPRs.
439 if (GPR_remaining > 0) {
440 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
441 --GPR_remaining;
442 }
443 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
444 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
445 --GPR_remaining;
446 }
Nate Begeman74d73452005-03-31 00:15:26 +0000447 }
Nate Begeman307e7442005-03-26 01:28:53 +0000448 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000449 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000450 Args[i].first, PtrOff,
451 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000452 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000453 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000454 break;
455 }
Nate Begemana9795f82005-03-24 04:41:43 +0000456 }
Nate Begeman74d73452005-03-31 00:15:26 +0000457 if (!MemOps.empty())
458 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000459 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000460
Nate Begemana9795f82005-03-24 04:41:43 +0000461 std::vector<MVT::ValueType> RetVals;
462 MVT::ValueType RetTyVT = getValueType(RetTy);
463 if (RetTyVT != MVT::isVoid)
464 RetVals.push_back(RetTyVT);
465 RetVals.push_back(MVT::Other);
466
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000467 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000468 Chain, Callee, args_to_use), 0);
469 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000470 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000471 DAG.getConstant(NumBytes, getPointerTy()));
472 return std::make_pair(TheCall, Chain);
473}
474
Chris Lattnere0fe2252005-07-05 19:58:54 +0000475SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
476 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000477 // vastart just stores the address of the VarArgsFrameIndex slot into the
478 // memory location argument.
479 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000480 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
481 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000482}
483
Chris Lattnere0fe2252005-07-05 19:58:54 +0000484std::pair<SDOperand,SDOperand>
485PPC32TargetLowering::LowerVAArg(SDOperand Chain,
486 SDOperand VAListP, Value *VAListV,
487 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000488 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000489
490 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000491 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
492 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000493 unsigned Amt;
494 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
495 Amt = 4;
496 else {
497 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
498 "Other types should have been promoted for varargs!");
499 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000501 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
502 DAG.getConstant(Amt, VAList.getValueType()));
503 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000504 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000505 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000506}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000507
Nate Begemana9795f82005-03-24 04:41:43 +0000508
509std::pair<SDOperand, SDOperand> PPC32TargetLowering::
510LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
511 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000512 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000513 abort();
514}
515
516namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000517Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000518Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000519Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000520//===--------------------------------------------------------------------===//
521/// ISel - PPC32 specific code to select PPC32 machine instructions for
522/// SelectionDAG operations.
523//===--------------------------------------------------------------------===//
524class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000525 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000526 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
527 // for sdiv and udiv until it is put into the future
528 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000529
Nate Begemana9795f82005-03-24 04:41:43 +0000530 /// ExprMap - As shared expressions are codegen'd, we keep track of which
531 /// vreg the value is produced in, so we only emit one copy of each compiled
532 /// tree.
533 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000534
535 unsigned GlobalBaseReg;
536 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000537 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000538public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000539 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
540 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000541
Nate Begemanc7b09f12005-03-25 08:34:25 +0000542 /// runOnFunction - Override this function in order to reset our per-function
543 /// variables.
544 virtual bool runOnFunction(Function &Fn) {
545 // Make sure we re-emit a set of the global base reg if necessary
546 GlobalBaseInitialized = false;
547 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548 }
549
Nate Begemana9795f82005-03-24 04:41:43 +0000550 /// InstructionSelectBasicBlock - This callback is invoked by
551 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
552 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
553 DEBUG(BB->dump());
554 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000555 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000556 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000557
Nate Begemana9795f82005-03-24 04:41:43 +0000558 // Clear state used for selection.
559 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000560 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000561 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000562
563 // dag -> dag expanders for integer divide by constant
564 SDOperand BuildSDIVSequence(SDOperand N);
565 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000566
Nate Begemandffcfcc2005-04-01 00:32:34 +0000567 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000568 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000569 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000570 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000571 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000572 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
573 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000574 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000575 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000576
Nate Begeman04730362005-04-01 04:45:11 +0000577 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000578 void SelectBranchCC(SDOperand N);
579};
580
Nate Begeman80196b12005-04-05 00:15:08 +0000581/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
582/// returns zero when the input is not exactly a power of two.
583static unsigned ExactLog2(unsigned Val) {
584 if (Val == 0 || (Val & (Val-1))) return 0;
585 unsigned Count = 0;
586 while (Val != 1) {
587 Val >>= 1;
588 ++Count;
589 }
590 return Count;
591}
592
Nate Begeman7ddecb42005-04-06 23:51:40 +0000593// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
594// any number of 0's on either side. the 1's are allowed to wrap from LSB to
595// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
596// not, since all 1's are not contiguous.
597static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
598 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000599 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000600 ME = 0;
601
602 // look for first set bit
603 int i = 0;
604 for (; i < 32; i++) {
605 if ((Val & (1 << (31 - i))) != 0) {
606 MB = i;
607 ME = i;
608 break;
609 }
610 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000611
Nate Begeman7ddecb42005-04-06 23:51:40 +0000612 // look for last set bit
613 for (; i < 32; i++) {
614 if ((Val & (1 << (31 - i))) == 0)
615 break;
616 ME = i;
617 }
618
619 // look for next set bit
620 for (; i < 32; i++) {
621 if ((Val & (1 << (31 - i))) != 0)
622 break;
623 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000624
Nate Begeman7ddecb42005-04-06 23:51:40 +0000625 // if we exhausted all the bits, we found a match at this point for 0*1*0*
626 if (i == 32)
627 return true;
628
629 // since we just encountered more 1's, if it doesn't wrap around to the
630 // most significant bit of the word, then we did not find a match to 1*0*1* so
631 // exit.
632 if (MB != 0)
633 return false;
634
635 // look for last set bit
636 for (MB = i; i < 32; i++) {
637 if ((Val & (1 << (31 - i))) == 0)
638 break;
639 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000640
Nate Begeman7ddecb42005-04-06 23:51:40 +0000641 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
642 // the value is not a run of ones.
643 if (i == 32)
644 return true;
645 return false;
646}
647
Nate Begeman439b4442005-04-05 04:22:58 +0000648/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000649/// the ConstantSDNode N can be used as an immediate to Opcode. The return
650/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000651/// ConstantSDNode, or is not suitable for use by that opcode.
652/// Return value codes for turning into an enum someday:
653/// 1: constant may be used in normal immediate form.
654/// 2: constant may be used in shifted immediate form.
655/// 3: log base 2 of the constant may be used.
656/// 4: constant is suitable for integer division conversion
657/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000658///
Nate Begeman439b4442005-04-05 04:22:58 +0000659static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
660 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000661 if (N.getOpcode() != ISD::Constant) return 0;
662
663 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000664
Nate Begemana9795f82005-03-24 04:41:43 +0000665 switch(Opcode) {
666 default: return 0;
667 case ISD::ADD:
668 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
669 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
670 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000671 case ISD::AND: {
672 unsigned MB, ME;
673 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
674 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
675 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
676 break;
677 }
Nate Begemana9795f82005-03-24 04:41:43 +0000678 case ISD::XOR:
679 case ISD::OR:
680 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
681 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
682 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000683 case ISD::MUL:
684 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
685 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000686 case ISD::SUB:
687 // handle subtract-from separately from subtract, since subi is really addi
688 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
689 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
690 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000691 case ISD::SETCC:
692 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
693 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
694 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000695 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000696 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000697 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000698 if (v <= -2 || v >= 2) { return 4; }
699 break;
700 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000701 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000702 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000703 }
704 return 0;
705}
Nate Begeman3e897162005-03-31 23:55:40 +0000706
Nate Begemanc7bd4822005-04-11 06:34:10 +0000707/// NodeHasRecordingVariant - If SelectExpr can always produce code for
708/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
709/// return false.
710static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
711 switch(NodeOpcode) {
712 default: return false;
713 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000714 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000715 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000716 }
717}
718
Nate Begeman3e897162005-03-31 23:55:40 +0000719/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
720/// to Condition. If the Condition is unordered or unsigned, the bool argument
721/// U is set to true, otherwise it is set to false.
722static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
723 U = false;
724 switch (Condition) {
725 default: assert(0 && "Unknown condition!"); abort();
726 case ISD::SETEQ: return PPC::BEQ;
727 case ISD::SETNE: return PPC::BNE;
728 case ISD::SETULT: U = true;
729 case ISD::SETLT: return PPC::BLT;
730 case ISD::SETULE: U = true;
731 case ISD::SETLE: return PPC::BLE;
732 case ISD::SETUGT: U = true;
733 case ISD::SETGT: return PPC::BGT;
734 case ISD::SETUGE: U = true;
735 case ISD::SETGE: return PPC::BGE;
736 }
Nate Begeman04730362005-04-01 04:45:11 +0000737 return 0;
738}
739
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000740/// getCROpForOp - Return the condition register opcode (or inverted opcode)
741/// associated with the SelectionDAG opcode.
742static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
743 switch (Opcode) {
744 default: assert(0 && "Unknown opcode!"); abort();
745 case ISD::AND:
746 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
747 if (!Inv1 && !Inv2) return PPC::CRAND;
748 if (Inv1 ^ Inv2) return PPC::CRANDC;
749 case ISD::OR:
750 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
751 if (!Inv1 && !Inv2) return PPC::CROR;
752 if (Inv1 ^ Inv2) return PPC::CRORC;
753 }
754 return 0;
755}
756
757/// getCRIdxForSetCC - Return the index of the condition register field
758/// associated with the SetCC condition, and whether or not the field is
759/// treated as inverted. That is, lt = 0; ge = 0 inverted.
760static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
761 switch (Condition) {
762 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000763 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000764 case ISD::SETLT: Inv = false; return 0;
765 case ISD::SETUGE:
766 case ISD::SETGE: Inv = true; return 0;
767 case ISD::SETUGT:
768 case ISD::SETGT: Inv = false; return 1;
769 case ISD::SETULE:
770 case ISD::SETLE: Inv = true; return 1;
771 case ISD::SETEQ: Inv = false; return 2;
772 case ISD::SETNE: Inv = true; return 2;
773 }
774 return 0;
775}
776
Nate Begeman04730362005-04-01 04:45:11 +0000777/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
778/// and store immediate instructions.
779static unsigned IndexedOpForOp(unsigned Opcode) {
780 switch(Opcode) {
781 default: assert(0 && "Unknown opcode!"); abort();
782 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
783 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
784 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
785 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
786 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
787 case PPC::LFD: return PPC::LFDX;
788 }
789 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000790}
Nate Begeman815d6da2005-04-06 00:25:27 +0000791
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000792// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000793// a multiply.
794struct ms {
795 int m; // magic number
796 int s; // shift amount
797};
798
799struct mu {
800 unsigned int m; // magic number
801 int a; // add indicator
802 int s; // shift amount
803};
804
805/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000806/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000807/// or -1.
808static struct ms magic(int d) {
809 int p;
810 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
811 const unsigned int two31 = 2147483648U; // 2^31
812 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000813
Nate Begeman815d6da2005-04-06 00:25:27 +0000814 ad = abs(d);
815 t = two31 + ((unsigned int)d >> 31);
816 anc = t - 1 - t%ad; // absolute value of nc
817 p = 31; // initialize p
818 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
819 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
820 q2 = two31/ad; // initialize q2 = 2p/abs(d)
821 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
822 do {
823 p = p + 1;
824 q1 = 2*q1; // update q1 = 2p/abs(nc)
825 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
826 if (r1 >= anc) { // must be unsigned comparison
827 q1 = q1 + 1;
828 r1 = r1 - anc;
829 }
830 q2 = 2*q2; // update q2 = 2p/abs(d)
831 r2 = 2*r2; // update r2 = rem(2p/abs(d))
832 if (r2 >= ad) { // must be unsigned comparison
833 q2 = q2 + 1;
834 r2 = r2 - ad;
835 }
836 delta = ad - r2;
837 } while (q1 < delta || (q1 == delta && r1 == 0));
838
839 mag.m = q2 + 1;
840 if (d < 0) mag.m = -mag.m; // resulting magic number
841 mag.s = p - 32; // resulting shift
842 return mag;
843}
844
845/// magicu - calculate the magic numbers required to codegen an integer udiv as
846/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
847static struct mu magicu(unsigned d)
848{
849 int p;
850 unsigned int nc, delta, q1, r1, q2, r2;
851 struct mu magu;
852 magu.a = 0; // initialize "add" indicator
853 nc = - 1 - (-d)%d;
854 p = 31; // initialize p
855 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
856 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
857 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
858 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
859 do {
860 p = p + 1;
861 if (r1 >= nc - r1 ) {
862 q1 = 2*q1 + 1; // update q1
863 r1 = 2*r1 - nc; // update r1
864 }
865 else {
866 q1 = 2*q1; // update q1
867 r1 = 2*r1; // update r1
868 }
869 if (r2 + 1 >= d - r2) {
870 if (q2 >= 0x7FFFFFFF) magu.a = 1;
871 q2 = 2*q2 + 1; // update q2
872 r2 = 2*r2 + 1 - d; // update r2
873 }
874 else {
875 if (q2 >= 0x80000000) magu.a = 1;
876 q2 = 2*q2; // update q2
877 r2 = 2*r2 + 1; // update r2
878 }
879 delta = d - 1 - r2;
880 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
881 magu.m = q2 + 1; // resulting magic number
882 magu.s = p - 32; // resulting shift
883 return magu;
884}
885}
886
887/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
888/// return a DAG expression to select that will generate the same value by
889/// multiplying by a magic number. See:
890/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
891SDOperand ISel::BuildSDIVSequence(SDOperand N) {
892 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
893 ms magics = magic(d);
894 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000895 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000896 ISelDAG->getConstant(magics.m, MVT::i32));
897 // If d > 0 and m < 0, add the numerator
898 if (d > 0 && magics.m < 0)
899 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
900 // If d < 0 and m > 0, subtract the numerator.
901 if (d < 0 && magics.m > 0)
902 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
903 // Shift right algebraic if shift value is nonzero
904 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000905 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000906 ISelDAG->getConstant(magics.s, MVT::i32));
907 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000908 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000909 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000910 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000911}
912
913/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
914/// return a DAG expression to select that will generate the same value by
915/// multiplying by a magic number. See:
916/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
917SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000918 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000919 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
920 mu magics = magicu(d);
921 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000922 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000923 ISelDAG->getConstant(magics.m, MVT::i32));
924 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000925 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000926 ISelDAG->getConstant(magics.s, MVT::i32));
927 } else {
928 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000929 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000930 ISelDAG->getConstant(1, MVT::i32));
931 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000932 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000933 ISelDAG->getConstant(magics.s-1, MVT::i32));
934 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000935 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000936}
937
Nate Begemanc7b09f12005-03-25 08:34:25 +0000938/// getGlobalBaseReg - Output the instructions required to put the
939/// base address to use for accessing globals into a register.
940///
941unsigned ISel::getGlobalBaseReg() {
942 if (!GlobalBaseInitialized) {
943 // Insert the set of GlobalBaseReg into the first MBB of the function
944 MachineBasicBlock &FirstMBB = BB->getParent()->front();
945 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
946 GlobalBaseReg = MakeReg(MVT::i32);
947 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
948 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
949 GlobalBaseInitialized = true;
950 }
951 return GlobalBaseReg;
952}
953
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000954/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000955/// Constant Pool. Optionally takes a register in which to load the value.
956unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
957 unsigned Tmp1 = MakeReg(MVT::i32);
958 if (0 == Result) Result = MakeReg(MVT::f64);
959 MachineConstantPool *CP = BB->getParent()->getConstantPool();
960 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
961 unsigned CPI = CP->getConstantPoolIndex(CFP);
962 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
963 .addConstantPoolIndex(CPI);
964 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
965 return Result;
966}
967
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000968/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000969/// Inv is true, then invert the result.
970void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
971 unsigned IntCR = MakeReg(MVT::i32);
972 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Nate Begemanadeb43d2005-07-20 22:42:00 +0000973 BuildMI(BB, EnableGPOPT ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000974 if (Inv) {
975 unsigned Tmp1 = MakeReg(MVT::i32);
976 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
977 .addImm(31).addImm(31);
978 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
979 } else {
980 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
981 .addImm(31).addImm(31);
982 }
983}
984
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000985/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000986/// the rotate left word immediate then mask insert (rlwimi) instruction.
987/// Returns true on success, false if the caller still needs to select OR.
988///
989/// Patterns matched:
990/// 1. or shl, and 5. or and, and
991/// 2. or and, shl 6. or shl, shr
992/// 3. or shr, and 7. or shr, shl
993/// 4. or and, shr
994bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000995 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000996 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000997
998 SDOperand Op0 = OR.getOperand(0);
999 SDOperand Op1 = OR.getOperand(1);
1000
1001 unsigned Op0Opc = Op0.getOpcode();
1002 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003
Nate Begeman7ddecb42005-04-06 23:51:40 +00001004 // Verify that we have the correct opcodes
1005 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
1006 return false;
1007 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1008 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001009
Nate Begeman7ddecb42005-04-06 23:51:40 +00001010 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001011 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001012 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001013 switch(Op0Opc) {
1014 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1015 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1016 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1017 }
1018 } else {
1019 return false;
1020 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001021
Nate Begeman7ddecb42005-04-06 23:51:40 +00001022 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001023 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001024 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001025 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001026 case ISD::SHL:
1027 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001028 InsMask <<= Amount;
1029 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001030 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001031 case ISD::SRL:
1032 Amount = CN->getValue();
1033 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001034 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001035 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001036 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001037 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001038 InsMask &= (unsigned)CN->getValue();
1039 break;
1040 }
1041 } else {
1042 return false;
1043 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001044
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001045 unsigned Tmp3 = 0;
1046
1047 // If both of the inputs are ANDs and one of them has a logical shift by
1048 // constant as its input, make that the inserted value so that we can combine
1049 // the shift into the rotate part of the rlwimi instruction
1050 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1051 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1052 Op1.getOperand(0).getOpcode() == ISD::SRL) {
1053 if (ConstantSDNode *CN =
1054 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1055 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1056 CN->getValue() : 32 - CN->getValue();
1057 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1058 }
1059 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1060 Op0.getOperand(0).getOpcode() == ISD::SRL) {
1061 if (ConstantSDNode *CN =
1062 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1063 std::swap(Op0, Op1);
1064 std::swap(TgtMask, InsMask);
1065 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1066 CN->getValue() : 32 - CN->getValue();
1067 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1068 }
1069 }
1070 }
1071
Nate Begeman7ddecb42005-04-06 23:51:40 +00001072 // Verify that the Target mask and Insert mask together form a full word mask
1073 // and that the Insert mask is a run of set bits (which implies both are runs
1074 // of set bits). Given that, Select the arguments and generate the rlwimi
1075 // instruction.
1076 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001077 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001078 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001079 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001080 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1081 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001082 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001083 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001084 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1085 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1086 .addImm(0).addImm(31);
1087 return true;
1088 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001089 if (Op0Opc == ISD::AND && fullMask)
1090 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001091 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001092 Tmp1 = SelectExpr(Op0);
1093 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001094 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1095 .addImm(Amount).addImm(MB).addImm(ME);
1096 return true;
1097 }
1098 return false;
1099}
1100
Nate Begeman3664cef2005-04-13 22:14:14 +00001101/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1102/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1103/// wider than the implicit mask, then we can get rid of the AND and let the
1104/// shift do the mask.
1105unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1106 unsigned C;
1107 if (N.getOpcode() == ISD::AND &&
1108 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1109 31 == (C & 0xFFFF) && // ME
1110 26 >= (C >> 16)) // MB
1111 return SelectExpr(N.getOperand(0));
1112 else
1113 return SelectExpr(N);
1114}
1115
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001116unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001117 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001118 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001119 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001120 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001121
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001122 // Allocate a condition register for this expression
1123 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001124
Nate Begemandffcfcc2005-04-01 00:32:34 +00001125 // If the first operand to the select is a SETCC node, then we can fold it
1126 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001127 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001128 bool U;
1129 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001130 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001131
Nate Begeman439b4442005-04-05 04:22:58 +00001132 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001133 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001134 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001135 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001136 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001137 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1138 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001139 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001140 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1141 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001142 RecordSuccess = false;
1143 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1144 if (RecordSuccess) {
1145 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001146 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1147 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001148 }
1149 AlreadySelected = true;
1150 }
1151 // If we could not implicitly set CR0, then emit a compare immediate
1152 // instead.
1153 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001154 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001155 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001157 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001158 } else {
1159 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1160 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001161 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001162 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001163 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001164 }
1165 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001166 // If this isn't a SetCC, then select the value and compare it against zero,
1167 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001168 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001169 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001170 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001171 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001172 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001173 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001174}
1175
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001176unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001177 unsigned &Idx) {
1178 bool Inv0, Inv1;
1179 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1180
1181 // Allocate a condition register for this expression
1182 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1183
1184 // Check for the operations we support:
1185 switch(N.getOpcode()) {
1186 default:
1187 Opc = PPC::BNE;
1188 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1189 Tmp1 = SelectExpr(N);
1190 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1191 break;
1192 case ISD::OR:
1193 case ISD::AND:
1194 ++MultiBranch;
1195 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1196 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1197 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1198 if (Inv0 && !Inv1) {
1199 std::swap(Tmp1, Tmp2);
1200 std::swap(Idx0, Idx1);
1201 Opc = Opc1;
1202 }
1203 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1204 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1205 .addReg(Tmp2).addImm(Idx1);
1206 Inv = false;
1207 Idx = Idx0;
1208 break;
1209 case ISD::SETCC:
1210 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1211 Result = Tmp1;
1212 break;
1213 }
1214 return Result;
1215}
1216
Nate Begemandffcfcc2005-04-01 00:32:34 +00001217/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001218bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001219{
Nate Begeman96fc6812005-03-31 02:05:53 +00001220 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001221 if (N.getOpcode() == ISD::ADD) {
1222 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001223 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001224 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001225 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001226 }
Nate Begeman04730362005-04-01 04:45:11 +00001227 offset = SelectExpr(N.getOperand(1));
1228 return true;
1229 }
Nate Begemana9795f82005-03-24 04:41:43 +00001230 Reg = SelectExpr(N);
1231 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001232 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001233}
1234
1235void ISel::SelectBranchCC(SDOperand N)
1236{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001237 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001238 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001239
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001240 bool Inv;
1241 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001242 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001243 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001244
Nate Begeman439009c2005-06-15 18:22:43 +00001245 // Iterate to the next basic block
1246 ilist<MachineBasicBlock>::iterator It = BB;
1247 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001248
1249 // If this is a two way branch, then grab the fallthrough basic block argument
1250 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1251 // if necessary by the branch selection pass. Otherwise, emit a standard
1252 // conditional branch.
1253 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001254 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001255 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1256 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001257 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001258 .addMBB(Dest).addMBB(Fallthrough);
1259 if (Fallthrough != It)
1260 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1261 } else {
1262 if (Fallthrough != It) {
1263 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001264 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001265 .addMBB(Fallthrough).addMBB(Dest);
1266 }
1267 }
1268 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001269 // If the fallthrough path is off the end of the function, which would be
1270 // undefined behavior, set it to be the same as the current block because
1271 // we have nothing better to set it to, and leaving it alone will cause the
1272 // PowerPC Branch Selection pass to crash.
1273 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001274 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001275 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001276 }
Nate Begemana9795f82005-03-24 04:41:43 +00001277 return;
1278}
1279
Nate Begemanc7bd4822005-04-11 06:34:10 +00001280unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001281 unsigned Result;
1282 unsigned Tmp1, Tmp2, Tmp3;
1283 unsigned Opc = 0;
1284 unsigned opcode = N.getOpcode();
1285
1286 SDNode *Node = N.Val;
1287 MVT::ValueType DestType = N.getValueType();
1288
Nate Begemana43b1762005-06-14 03:55:23 +00001289 if (Node->getOpcode() == ISD::CopyFromReg &&
1290 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1291 // Just use the specified register as our input.
1292 return cast<RegSDNode>(Node)->getReg();
1293
Nate Begemana9795f82005-03-24 04:41:43 +00001294 unsigned &Reg = ExprMap[N];
1295 if (Reg) return Reg;
1296
Nate Begeman27eeb002005-04-02 05:59:34 +00001297 switch (N.getOpcode()) {
1298 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001299 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001300 MakeReg(N.getValueType()) : 1;
1301 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001302 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001303 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001304 // If this is a call instruction, make sure to prepare ALL of the result
1305 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001306 if (Node->getNumValues() == 1)
1307 Reg = Result = 1; // Void call, just a chain.
1308 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001309 Result = MakeReg(Node->getValueType(0));
1310 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001311 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001312 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001313 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001314 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001315 break;
1316 case ISD::ADD_PARTS:
1317 case ISD::SUB_PARTS:
1318 case ISD::SHL_PARTS:
1319 case ISD::SRL_PARTS:
1320 case ISD::SRA_PARTS:
1321 Result = MakeReg(Node->getValueType(0));
1322 ExprMap[N.getValue(0)] = Result;
1323 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1324 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1325 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001326 }
1327
Nate Begemana9795f82005-03-24 04:41:43 +00001328 switch (opcode) {
1329 default:
1330 Node->dump();
1331 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001332 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001333 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1334 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001335 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001336 // Generate both result values. FIXME: Need a better commment here?
1337 if (Result != 1)
1338 ExprMap[N.getValue(1)] = 1;
1339 else
1340 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1341
1342 // FIXME: We are currently ignoring the requested alignment for handling
1343 // greater than the stack alignment. This will need to be revisited at some
1344 // point. Align = N.getOperand(2);
1345 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1346 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1347 std::cerr << "Cannot allocate stack object with greater alignment than"
1348 << " the stack alignment yet!";
1349 abort();
1350 }
1351 Select(N.getOperand(0));
1352 Tmp1 = SelectExpr(N.getOperand(1));
1353 // Subtract size from stack pointer, thereby allocating some space.
1354 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1355 // Put a pointer to the space into the result register by copying the SP
1356 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1357 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001358
1359 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001360 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1361 Tmp2 = MakeReg(MVT::i32);
1362 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1363 .addConstantPoolIndex(Tmp1);
1364 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1365 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001366
1367 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001368 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001369 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001370 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001371
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001372 case ISD::GlobalAddress: {
1373 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001374 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001375 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1376 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001377 if (GV->hasWeakLinkage() || GV->isExternal()) {
1378 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1379 } else {
1380 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1381 }
1382 return Result;
1383 }
1384
Nate Begeman5e966612005-03-24 06:28:42 +00001385 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001386 case ISD::EXTLOAD:
1387 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001388 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001389 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001390 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001391 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001392
Nate Begeman5e966612005-03-24 06:28:42 +00001393 // Make sure we generate both values.
1394 if (Result != 1)
1395 ExprMap[N.getValue(1)] = 1; // Generate the token
1396 else
1397 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1398
1399 SDOperand Chain = N.getOperand(0);
1400 SDOperand Address = N.getOperand(1);
1401 Select(Chain);
1402
Nate Begeman9db505c2005-03-28 19:36:43 +00001403 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001404 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001405 case MVT::i1: Opc = PPC::LBZ; break;
1406 case MVT::i8: Opc = PPC::LBZ; break;
1407 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1408 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001409 case MVT::f32: Opc = PPC::LFS; break;
1410 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001411 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001412
Nate Begeman74d73452005-03-31 00:15:26 +00001413 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1414 Tmp1 = MakeReg(MVT::i32);
1415 int CPI = CP->getIndex();
1416 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1417 .addConstantPoolIndex(CPI);
1418 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001419 }
Nate Begeman74d73452005-03-31 00:15:26 +00001420 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001421 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1422 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001423 } else {
1424 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001425 bool idx = SelectAddr(Address, Tmp1, offset);
1426 if (idx) {
1427 Opc = IndexedOpForOp(Opc);
1428 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1429 } else {
1430 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1431 }
Nate Begeman5e966612005-03-24 06:28:42 +00001432 }
1433 return Result;
1434 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001435
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001436 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001437 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001438 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001439 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001440 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1441 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1442 };
1443 static const unsigned FPR[] = {
1444 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1445 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1446 };
1447
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001448 // Lower the chain for this call.
1449 Select(N.getOperand(0));
1450 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001451
Nate Begemand860aa62005-04-04 22:17:48 +00001452 MachineInstr *CallMI;
1453 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001454 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001455 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001456 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001457 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001458 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001459 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001460 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001461 true);
1462 } else {
1463 Tmp1 = SelectExpr(N.getOperand(1));
1464 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1465 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1466 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1467 .addReg(PPC::R12);
1468 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001469
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001470 // Load the register args to virtual regs
1471 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001472 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001473 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1474
1475 // Copy the virtual registers into the appropriate argument register
1476 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1477 switch(N.getOperand(i+2).getValueType()) {
1478 default: Node->dump(); assert(0 && "Unknown value type for call");
1479 case MVT::i1:
1480 case MVT::i8:
1481 case MVT::i16:
1482 case MVT::i32:
1483 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001484 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001485 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001486 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1487 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001488 ++GPR_idx;
1489 break;
1490 case MVT::f64:
1491 case MVT::f32:
1492 assert(FPR_idx < 13 && "Too many fp args");
1493 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001494 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001495 ++FPR_idx;
1496 break;
1497 }
1498 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001499
Nate Begemand860aa62005-04-04 22:17:48 +00001500 // Put the call instruction in the correct place in the MachineBasicBlock
1501 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001502
1503 switch (Node->getValueType(0)) {
1504 default: assert(0 && "Unknown value type for call result!");
1505 case MVT::Other: return 1;
1506 case MVT::i1:
1507 case MVT::i8:
1508 case MVT::i16:
1509 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001510 if (Node->getValueType(1) == MVT::i32) {
1511 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1512 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1513 } else {
1514 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1515 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001516 break;
1517 case MVT::f32:
1518 case MVT::f64:
1519 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1520 break;
1521 }
1522 return Result+N.ResNo;
1523 }
Nate Begemana9795f82005-03-24 04:41:43 +00001524
1525 case ISD::SIGN_EXTEND:
1526 case ISD::SIGN_EXTEND_INREG:
1527 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001528 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001529 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001530 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001531 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001532 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001533 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001534 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001535 break;
Nate Begeman74747862005-03-29 22:24:51 +00001536 case MVT::i1:
1537 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1538 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001539 }
Nate Begemana9795f82005-03-24 04:41:43 +00001540 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001541
Nate Begemana9795f82005-03-24 04:41:43 +00001542 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001543 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001544 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001545 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001546 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001547 if (MVT::isInteger(DestType))
1548 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1549 else
1550 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001551 return Result;
1552
1553 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001554 Tmp1 = SelectExpr(N.getOperand(0));
1555 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1556 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001557 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001558 .addImm(31-Tmp2);
1559 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001560 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001561 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1562 }
1563 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001564
Nate Begeman5e966612005-03-24 06:28:42 +00001565 case ISD::SRL:
1566 Tmp1 = SelectExpr(N.getOperand(0));
1567 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1568 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001569 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001570 .addImm(Tmp2).addImm(31);
1571 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001572 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001573 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1574 }
1575 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001576
Nate Begeman5e966612005-03-24 06:28:42 +00001577 case ISD::SRA:
1578 Tmp1 = SelectExpr(N.getOperand(0));
1579 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1580 Tmp2 = CN->getValue() & 0x1F;
1581 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1582 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001583 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001584 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1585 }
1586 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001587
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001588 case ISD::CTLZ:
1589 Tmp1 = SelectExpr(N.getOperand(0));
1590 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1591 return Result;
1592
Nate Begemana9795f82005-03-24 04:41:43 +00001593 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001594 if (!MVT::isInteger(DestType)) {
1595 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1596 N.getOperand(0).Val->hasOneUse()) {
1597 ++FusedFP; // Statistic
1598 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1599 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1600 Tmp3 = SelectExpr(N.getOperand(1));
1601 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1602 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1603 return Result;
1604 }
1605 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1606 N.getOperand(1).Val->hasOneUse()) {
1607 ++FusedFP; // Statistic
1608 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1609 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1610 Tmp3 = SelectExpr(N.getOperand(0));
1611 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1612 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1613 return Result;
1614 }
1615 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1616 Tmp1 = SelectExpr(N.getOperand(0));
1617 Tmp2 = SelectExpr(N.getOperand(1));
1618 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1619 return Result;
1620 }
Nate Begemana9795f82005-03-24 04:41:43 +00001621 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001622 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001623 default: assert(0 && "unhandled result code");
1624 case 0: // No immediate
1625 Tmp2 = SelectExpr(N.getOperand(1));
1626 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1627 break;
1628 case 1: // Low immediate
1629 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1630 break;
1631 case 2: // Shifted immediate
1632 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1633 break;
1634 }
1635 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001636
Nate Begemana9795f82005-03-24 04:41:43 +00001637 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001638 // FIXME: should add check in getImmediateForOpcode to return a value
1639 // indicating the immediate is a run of set bits so we can emit a bitfield
1640 // clear with RLWINM instead.
1641 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1642 default: assert(0 && "unhandled result code");
1643 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001644 // Check for andc: and, (xor a, -1), b
1645 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1646 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1647 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1648 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1649 Tmp2 = SelectExpr(N.getOperand(1));
1650 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1651 return Result;
1652 }
1653 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001654 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001655 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001656 Opc = Recording ? PPC::ANDo : PPC::AND;
1657 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001658 break;
1659 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001660 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001661 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1662 break;
1663 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001664 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001665 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1666 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001667 case 5: // Bitfield mask
1668 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1669 Tmp3 = Tmp2 >> 16; // MB
1670 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001671
1672 if (N.getOperand(0).getOpcode() == ISD::SRL)
1673 if (ConstantSDNode *SA =
1674 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1675
1676 // We can fold the RLWINM and the SRL together if the mask is
1677 // clearing the top bits which are rotated around.
1678 unsigned RotAmt = 32-(SA->getValue() & 31);
1679 if (Tmp2 <= RotAmt) {
1680 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1681 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1682 .addImm(Tmp3).addImm(Tmp2);
1683 break;
1684 }
1685 }
1686
1687 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001688 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1689 .addImm(Tmp3).addImm(Tmp2);
1690 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001691 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001692 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001693 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001694
Nate Begemana9795f82005-03-24 04:41:43 +00001695 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001696 if (SelectBitfieldInsert(N, Result))
1697 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001698 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001699 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001700 default: assert(0 && "unhandled result code");
1701 case 0: // No immediate
1702 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001703 Opc = Recording ? PPC::ORo : PPC::OR;
1704 RecordSuccess = true;
1705 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001706 break;
1707 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001708 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001709 break;
1710 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001711 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001712 break;
1713 }
1714 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001715
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001716 case ISD::XOR: {
1717 // Check for EQV: xor, (xor a, -1), b
1718 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1719 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1720 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001721 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1722 Tmp2 = SelectExpr(N.getOperand(1));
1723 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1724 return Result;
1725 }
Chris Lattner837a5212005-04-21 21:09:11 +00001726 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001727 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1728 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001729 switch(N.getOperand(0).getOpcode()) {
1730 case ISD::OR:
1731 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1732 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1733 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1734 break;
1735 case ISD::AND:
1736 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1737 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1738 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1739 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001740 case ISD::XOR:
1741 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1742 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1743 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1744 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001745 default:
1746 Tmp1 = SelectExpr(N.getOperand(0));
1747 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1748 break;
1749 }
1750 return Result;
1751 }
1752 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001753 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001754 default: assert(0 && "unhandled result code");
1755 case 0: // No immediate
1756 Tmp2 = SelectExpr(N.getOperand(1));
1757 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1758 break;
1759 case 1: // Low immediate
1760 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1761 break;
1762 case 2: // Shifted immediate
1763 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1764 break;
1765 }
1766 return Result;
1767 }
1768
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001769 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001770 if (!MVT::isInteger(DestType)) {
1771 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1772 N.getOperand(0).Val->hasOneUse()) {
1773 ++FusedFP; // Statistic
1774 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1775 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1776 Tmp3 = SelectExpr(N.getOperand(1));
1777 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1778 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1779 return Result;
1780 }
1781 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1782 N.getOperand(1).Val->hasOneUse()) {
1783 ++FusedFP; // Statistic
1784 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1785 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1786 Tmp3 = SelectExpr(N.getOperand(0));
1787 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1788 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1789 return Result;
1790 }
1791 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1792 Tmp1 = SelectExpr(N.getOperand(0));
1793 Tmp2 = SelectExpr(N.getOperand(1));
1794 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1795 return Result;
1796 }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001797 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
1798 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001799 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001800 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00001801 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001802 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1803 } else {
1804 Tmp1 = SelectExpr(N.getOperand(0));
1805 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001806 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1807 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001808 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001809
Nate Begeman5e966612005-03-24 06:28:42 +00001810 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001811 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001812 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001813 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1814 else {
1815 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001816 switch (DestType) {
1817 default: assert(0 && "Unknown type to ISD::MUL"); break;
1818 case MVT::i32: Opc = PPC::MULLW; break;
1819 case MVT::f32: Opc = PPC::FMULS; break;
1820 case MVT::f64: Opc = PPC::FMUL; break;
1821 }
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001823 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001824 return Result;
1825
Nate Begeman815d6da2005-04-06 00:25:27 +00001826 case ISD::MULHS:
1827 case ISD::MULHU:
1828 Tmp1 = SelectExpr(N.getOperand(0));
1829 Tmp2 = SelectExpr(N.getOperand(1));
1830 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1831 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1832 return Result;
1833
Nate Begemanf3d08f32005-03-29 00:03:27 +00001834 case ISD::SDIV:
1835 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001836 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1837 default: break;
1838 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1839 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001840 Tmp1 = MakeReg(MVT::i32);
1841 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001842 if ((int)Tmp3 < 0) {
1843 unsigned Tmp4 = MakeReg(MVT::i32);
1844 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1845 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1846 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1847 } else {
1848 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1849 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1850 }
Nate Begeman80196b12005-04-05 00:15:08 +00001851 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001852 // If this is a divide by constant, we can emit code using some magic
1853 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001854 case 4:
1855 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001856 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001857 return SelectExpr(BuildSDIVSequence(N));
1858 else
1859 return SelectExpr(BuildUDIVSequence(N));
Nate Begemana3fd4002005-07-19 16:51:05 +00001860 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001861 Tmp1 = SelectExpr(N.getOperand(0));
1862 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001863 switch (DestType) {
1864 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1865 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1866 case MVT::f32: Opc = PPC::FDIVS; break;
1867 case MVT::f64: Opc = PPC::FDIV; break;
1868 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 return Result;
1871
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001872 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001873 case ISD::SUB_PARTS: {
1874 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1875 "Not an i64 add/sub!");
1876 // Emit all of the operands.
1877 std::vector<unsigned> InVals;
1878 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1879 InVals.push_back(SelectExpr(N.getOperand(i)));
1880 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001881 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1882 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001883 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001884 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1885 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1886 }
1887 return Result+N.ResNo;
1888 }
1889
1890 case ISD::SHL_PARTS:
1891 case ISD::SRA_PARTS:
1892 case ISD::SRL_PARTS: {
1893 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1894 "Not an i64 shift!");
1895 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1896 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001897 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1898 Tmp1 = MakeReg(MVT::i32);
1899 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001900 Tmp3 = MakeReg(MVT::i32);
1901 unsigned Tmp4 = MakeReg(MVT::i32);
1902 unsigned Tmp5 = MakeReg(MVT::i32);
1903 unsigned Tmp6 = MakeReg(MVT::i32);
1904 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1905 if (ISD::SHL_PARTS == opcode) {
1906 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1907 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1908 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1909 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001910 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001911 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1912 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1913 } else if (ISD::SRL_PARTS == opcode) {
1914 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1915 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1916 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1917 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1918 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1919 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1920 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1921 } else {
1922 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1923 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1924 MachineBasicBlock *OldMBB = BB;
1925 MachineFunction *F = BB->getParent();
1926 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1927 F->getBasicBlockList().insert(It, TmpMBB);
1928 F->getBasicBlockList().insert(It, PhiMBB);
1929 BB->addSuccessor(TmpMBB);
1930 BB->addSuccessor(PhiMBB);
1931 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1932 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1933 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1934 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1935 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1936 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1937 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1938 // Select correct least significant half if the shift amount > 32
1939 BB = TmpMBB;
1940 unsigned Tmp7 = MakeReg(MVT::i32);
1941 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1942 TmpMBB->addSuccessor(PhiMBB);
1943 BB = PhiMBB;
1944 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1945 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001946 }
1947 return Result+N.ResNo;
1948 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001949
Nate Begemana9795f82005-03-24 04:41:43 +00001950 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001951 case ISD::FP_TO_SINT: {
1952 bool U = (ISD::FP_TO_UINT == opcode);
1953 Tmp1 = SelectExpr(N.getOperand(0));
1954 if (!U) {
1955 Tmp2 = MakeReg(MVT::f64);
1956 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1957 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1958 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1959 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1960 return Result;
1961 } else {
1962 unsigned Zero = getConstDouble(0.0);
1963 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1964 unsigned Border = getConstDouble(1LL << 31);
1965 unsigned UseZero = MakeReg(MVT::f64);
1966 unsigned UseMaxInt = MakeReg(MVT::f64);
1967 unsigned UseChoice = MakeReg(MVT::f64);
1968 unsigned TmpReg = MakeReg(MVT::f64);
1969 unsigned TmpReg2 = MakeReg(MVT::f64);
1970 unsigned ConvReg = MakeReg(MVT::f64);
1971 unsigned IntTmp = MakeReg(MVT::i32);
1972 unsigned XorReg = MakeReg(MVT::i32);
1973 MachineFunction *F = BB->getParent();
1974 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1975 // Update machine-CFG edges
1976 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1977 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1978 MachineBasicBlock *OldMBB = BB;
1979 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1980 F->getBasicBlockList().insert(It, XorMBB);
1981 F->getBasicBlockList().insert(It, PhiMBB);
1982 BB->addSuccessor(XorMBB);
1983 BB->addSuccessor(PhiMBB);
1984 // Convert from floating point to unsigned 32-bit value
1985 // Use 0 if incoming value is < 0.0
1986 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1987 // Use 2**32 - 1 if incoming value is >= 2**32
1988 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1989 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1990 .addReg(MaxInt);
1991 // Subtract 2**31
1992 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1993 // Use difference if >= 2**31
1994 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1995 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1996 .addReg(UseChoice);
1997 // Convert to integer
1998 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1999 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2000 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2001 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2002 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2003
2004 // XorMBB:
2005 // add 2**31 if input was >= 2**31
2006 BB = XorMBB;
2007 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2008 XorMBB->addSuccessor(PhiMBB);
2009
2010 // PhiMBB:
2011 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2012 BB = PhiMBB;
2013 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2014 .addReg(XorReg).addMBB(XorMBB);
2015 return Result;
2016 }
2017 assert(0 && "Should never get here");
2018 return 0;
2019 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002020
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002021 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002022 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002023 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002024 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002025 // We can codegen setcc op, imm very efficiently compared to a brcond.
2026 // Check for those cases here.
2027 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002028 if (CN->getValue() == 0) {
2029 Tmp1 = SelectExpr(SetCC->getOperand(0));
2030 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002031 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002032 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002033 Tmp2 = MakeReg(MVT::i32);
2034 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2035 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2036 .addImm(5).addImm(31);
2037 break;
2038 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002039 Tmp2 = MakeReg(MVT::i32);
2040 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2041 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2042 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002043 case ISD::SETLT:
2044 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2045 .addImm(31).addImm(31);
2046 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002047 case ISD::SETGT:
2048 Tmp2 = MakeReg(MVT::i32);
2049 Tmp3 = MakeReg(MVT::i32);
2050 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2051 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2052 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2053 .addImm(31).addImm(31);
2054 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002055 }
2056 return Result;
2057 }
2058 // setcc op, -1
2059 if (CN->isAllOnesValue()) {
2060 Tmp1 = SelectExpr(SetCC->getOperand(0));
2061 switch (SetCC->getCondition()) {
2062 default: assert(0 && "Unhandled SetCC condition"); abort();
2063 case ISD::SETEQ:
2064 Tmp2 = MakeReg(MVT::i32);
2065 Tmp3 = MakeReg(MVT::i32);
2066 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2067 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2068 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002069 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002070 case ISD::SETNE:
2071 Tmp2 = MakeReg(MVT::i32);
2072 Tmp3 = MakeReg(MVT::i32);
2073 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2074 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2075 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2076 break;
2077 case ISD::SETLT:
2078 Tmp2 = MakeReg(MVT::i32);
2079 Tmp3 = MakeReg(MVT::i32);
2080 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2081 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2082 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2083 .addImm(31).addImm(31);
2084 break;
2085 case ISD::SETGT:
2086 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002087 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2088 .addImm(31).addImm(31);
2089 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2090 break;
2091 }
2092 return Result;
2093 }
2094 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002095
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002096 bool Inv;
2097 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2098 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002099 return Result;
2100 }
2101 assert(0 && "Is this legal?");
2102 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002103
Nate Begeman74747862005-03-29 22:24:51 +00002104 case ISD::SELECT: {
Nate Begemana3fd4002005-07-19 16:51:05 +00002105 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2106 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2107 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2108 !MVT::isInteger(N.getOperand(1).getValueType()) &&
2109 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2110 SetCC->getCondition() != ISD::SETEQ &&
2111 SetCC->getCondition() != ISD::SETNE) {
2112 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2113 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2114 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2115
2116 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2117 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2118 switch(SetCC->getCondition()) {
2119 default: assert(0 && "Invalid FSEL condition"); abort();
2120 case ISD::SETULT:
2121 case ISD::SETLT:
2122 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2123 case ISD::SETUGE:
2124 case ISD::SETGE:
2125 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2126 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2127 return Result;
2128 case ISD::SETUGT:
2129 case ISD::SETGT:
2130 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2131 case ISD::SETULE:
2132 case ISD::SETLE: {
2133 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2134 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2135 } else {
2136 Tmp2 = MakeReg(VT);
2137 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2138 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2139 }
2140 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2141 return Result;
2142 }
2143 }
2144 } else {
2145 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2146 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2147 Tmp2 = SelectExpr(SetCC->getOperand(1));
2148 Tmp3 = MakeReg(VT);
2149 switch(SetCC->getCondition()) {
2150 default: assert(0 && "Invalid FSEL condition"); abort();
2151 case ISD::SETULT:
2152 case ISD::SETLT:
2153 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2154 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2155 return Result;
2156 case ISD::SETUGE:
2157 case ISD::SETGE:
2158 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2159 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2160 return Result;
2161 case ISD::SETUGT:
2162 case ISD::SETGT:
2163 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2164 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2165 return Result;
2166 case ISD::SETULE:
2167 case ISD::SETLE:
2168 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2169 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2170 return Result;
2171 }
2172 }
2173 assert(0 && "Should never get here");
2174 return 0;
2175 }
2176
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002177 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002178 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2179 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002180 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002181
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002182 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002183 // value and the MBB to hold the PHI instruction for this SetCC.
2184 MachineBasicBlock *thisMBB = BB;
2185 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2186 ilist<MachineBasicBlock>::iterator It = BB;
2187 ++It;
2188
2189 // thisMBB:
2190 // ...
2191 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002192 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002193 // bCC copy1MBB
2194 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002195 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2196 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002197 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002198 MachineFunction *F = BB->getParent();
2199 F->getBasicBlockList().insert(It, copy0MBB);
2200 F->getBasicBlockList().insert(It, sinkMBB);
2201 // Update machine-CFG edges
2202 BB->addSuccessor(copy0MBB);
2203 BB->addSuccessor(sinkMBB);
2204
2205 // copy0MBB:
2206 // %FalseValue = ...
2207 // # fallthrough to sinkMBB
2208 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002209 // Update machine-CFG edges
2210 BB->addSuccessor(sinkMBB);
2211
2212 // sinkMBB:
2213 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2214 // ...
2215 BB = sinkMBB;
2216 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2217 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002218 return Result;
2219 }
Nate Begemana9795f82005-03-24 04:41:43 +00002220
2221 case ISD::Constant:
2222 switch (N.getValueType()) {
2223 default: assert(0 && "Cannot use constants of this type!");
2224 case MVT::i1:
2225 BuildMI(BB, PPC::LI, 1, Result)
2226 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2227 break;
2228 case MVT::i32:
2229 {
2230 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2231 if (v < 32768 && v >= -32768) {
2232 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2233 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002234 Tmp1 = MakeReg(MVT::i32);
2235 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2236 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002237 }
2238 }
2239 }
2240 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002241
2242 case ISD::ConstantFP: {
2243 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2244 Result = getConstDouble(CN->getValue(), Result);
2245 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002246 }
2247
Nate Begemana3fd4002005-07-19 16:51:05 +00002248 case ISD::FNEG:
2249 if (!NoExcessFPPrecision &&
2250 ISD::ADD == N.getOperand(0).getOpcode() &&
2251 N.getOperand(0).Val->hasOneUse() &&
2252 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2253 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2254 ++FusedFP; // Statistic
2255 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2256 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2257 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2258 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2259 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2260 } else if (!NoExcessFPPrecision &&
2261 ISD::ADD == N.getOperand(0).getOpcode() &&
2262 N.getOperand(0).Val->hasOneUse() &&
2263 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2264 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2265 ++FusedFP; // Statistic
2266 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2267 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2268 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2269 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2270 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2271 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2272 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2273 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2274 } else {
2275 Tmp1 = SelectExpr(N.getOperand(0));
2276 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2277 }
2278 return Result;
2279
2280 case ISD::FABS:
2281 Tmp1 = SelectExpr(N.getOperand(0));
2282 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2283 return Result;
2284
Nate Begemanadeb43d2005-07-20 22:42:00 +00002285 case ISD::FSQRT:
2286 Tmp1 = SelectExpr(N.getOperand(0));
2287 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2288 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2289 return Result;
2290
Nate Begemana3fd4002005-07-19 16:51:05 +00002291 case ISD::FP_ROUND:
2292 assert (DestType == MVT::f32 &&
2293 N.getOperand(0).getValueType() == MVT::f64 &&
2294 "only f64 to f32 conversion supported here");
2295 Tmp1 = SelectExpr(N.getOperand(0));
2296 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2297 return Result;
2298
2299 case ISD::FP_EXTEND:
2300 assert (DestType == MVT::f64 &&
2301 N.getOperand(0).getValueType() == MVT::f32 &&
2302 "only f32 to f64 conversion supported here");
2303 Tmp1 = SelectExpr(N.getOperand(0));
2304 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2305 return Result;
2306
2307 case ISD::UINT_TO_FP:
2308 case ISD::SINT_TO_FP: {
2309 assert (N.getOperand(0).getValueType() == MVT::i32
2310 && "int to float must operate on i32");
2311 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2312 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2313 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2314 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2315
2316 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2317 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2318
2319 if (IsUnsigned) {
2320 unsigned ConstF = getConstDouble(0x1.000000p52);
2321 // Store the hi & low halves of the fp value, currently in int regs
2322 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2323 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2324 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2325 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2326 // Generate the return value with a subtract
2327 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2328 } else {
2329 unsigned ConstF = getConstDouble(0x1.000008p52);
2330 unsigned TmpL = MakeReg(MVT::i32);
2331 // Store the hi & low halves of the fp value, currently in int regs
2332 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2333 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2334 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2335 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2336 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2337 // Generate the return value with a subtract
2338 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2339 }
2340 return Result;
2341 }
2342 }
Nate Begemana9795f82005-03-24 04:41:43 +00002343 return 0;
2344}
2345
2346void ISel::Select(SDOperand N) {
2347 unsigned Tmp1, Tmp2, Opc;
2348 unsigned opcode = N.getOpcode();
2349
2350 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2351 return; // Already selected.
2352
2353 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002354
Nate Begemana9795f82005-03-24 04:41:43 +00002355 switch (Node->getOpcode()) {
2356 default:
2357 Node->dump(); std::cerr << "\n";
2358 assert(0 && "Node not handled yet!");
2359 case ISD::EntryToken: return; // Noop
2360 case ISD::TokenFactor:
2361 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2362 Select(Node->getOperand(i));
2363 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002364 case ISD::CALLSEQ_START:
2365 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002366 Select(N.getOperand(0));
2367 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002368 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002369 PPC::ADJCALLSTACKUP;
2370 BuildMI(BB, Opc, 1).addImm(Tmp1);
2371 return;
2372 case ISD::BR: {
2373 MachineBasicBlock *Dest =
2374 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002375 Select(N.getOperand(0));
2376 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2377 return;
2378 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002379 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002380 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002381 SelectBranchCC(N);
2382 return;
2383 case ISD::CopyToReg:
2384 Select(N.getOperand(0));
2385 Tmp1 = SelectExpr(N.getOperand(1));
2386 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002387
Nate Begemana9795f82005-03-24 04:41:43 +00002388 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002389 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002390 N.getOperand(1).getValueType() == MVT::f32)
2391 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2392 else
2393 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2394 }
2395 return;
2396 case ISD::ImplicitDef:
2397 Select(N.getOperand(0));
2398 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2399 return;
2400 case ISD::RET:
2401 switch (N.getNumOperands()) {
2402 default:
2403 assert(0 && "Unknown return instruction!");
2404 case 3:
2405 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2406 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002407 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002408 Select(N.getOperand(0));
2409 Tmp1 = SelectExpr(N.getOperand(1));
2410 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002411 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2412 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002413 break;
2414 case 2:
2415 Select(N.getOperand(0));
2416 Tmp1 = SelectExpr(N.getOperand(1));
2417 switch (N.getOperand(1).getValueType()) {
2418 default:
2419 assert(0 && "Unknown return type!");
2420 case MVT::f64:
2421 case MVT::f32:
2422 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2423 break;
2424 case MVT::i32:
2425 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2426 break;
2427 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002428 case 1:
2429 Select(N.getOperand(0));
2430 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002431 }
2432 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2433 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002434 case ISD::TRUNCSTORE:
2435 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002436 {
2437 SDOperand Chain = N.getOperand(0);
2438 SDOperand Value = N.getOperand(1);
2439 SDOperand Address = N.getOperand(2);
2440 Select(Chain);
2441
2442 Tmp1 = SelectExpr(Value); //value
2443
2444 if (opcode == ISD::STORE) {
2445 switch(Value.getValueType()) {
2446 default: assert(0 && "unknown Type in store");
2447 case MVT::i32: Opc = PPC::STW; break;
2448 case MVT::f64: Opc = PPC::STFD; break;
2449 case MVT::f32: Opc = PPC::STFS; break;
2450 }
2451 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002452 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Nate Begemana9795f82005-03-24 04:41:43 +00002453 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002454 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002455 case MVT::i8: Opc = PPC::STB; break;
2456 case MVT::i16: Opc = PPC::STH; break;
2457 }
2458 }
2459
Nate Begemana7e11a42005-04-01 05:57:17 +00002460 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002461 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002462 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2463 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002464 }
2465 else
2466 {
2467 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002468 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002469 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002470 Opc = IndexedOpForOp(Opc);
2471 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2472 } else {
2473 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2474 }
Nate Begemana9795f82005-03-24 04:41:43 +00002475 }
2476 return;
2477 }
2478 case ISD::EXTLOAD:
2479 case ISD::SEXTLOAD:
2480 case ISD::ZEXTLOAD:
2481 case ISD::LOAD:
2482 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002483 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002484 case ISD::CALL:
2485 case ISD::DYNAMIC_STACKALLOC:
2486 ExprMap.erase(N);
2487 SelectExpr(N);
2488 return;
2489 }
2490 assert(0 && "Should not be reached!");
2491}
2492
2493
2494/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2495/// into a machine code representation using pattern matching and a machine
2496/// description file.
2497///
2498FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002499 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002500}
2501