blob: 4365e39646b70445d8e2b80ebd48edea8c82357e [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
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000046 // Fold away setcc operations if possible.
47 setSetCCIsExpensive();
48
Nate Begemana9795f82005-03-24 04:41:43 +000049 // Set up the register classes.
50 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000051 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000052 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begeman74d73452005-03-31 00:15:26 +000054 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000055 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
56 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
57 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
58
Nate Begeman74d73452005-03-31 00:15:26 +000059 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
60 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
61 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000062
Nate Begeman815d6da2005-04-06 00:25:27 +000063 // PowerPC has no SREM/UREM instructions
64 setOperationAction(ISD::SREM, MVT::i32, Expand);
65 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
Chris Lattner32f3cf62005-05-13 16:20:22 +000067 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000068 setOperationAction(ISD::FSIN , MVT::f64, Expand);
69 setOperationAction(ISD::FCOS , MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000071 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000072 setOperationAction(ISD::FSIN , MVT::f32, Expand);
73 setOperationAction(ISD::FCOS , MVT::f32, Expand);
74 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000075 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000076
Nate Begemand7c4a4a2005-05-11 23:43:56 +000077 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000078 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000080
Chris Lattnercbd06fc2005-04-07 19:41:49 +000081 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000082 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000084
Nate Begemana9795f82005-03-24 04:41:43 +000085 computeRegisterProperties();
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemana9795f82005-03-24 04:41:43 +000093 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
95 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +000097 bool isTailCall, SDOperand Callee, ArgListTy &Args,
98 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Chris Lattnere0fe2252005-07-05 19:58:54 +0000100 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
101 Value *VAListV, SelectionDAG &DAG);
102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000104 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
105 const Type *ArgTy, SelectionDAG &DAG);
106
Nate Begemana9795f82005-03-24 04:41:43 +0000107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
109 SelectionDAG &DAG);
110 };
111}
112
113
114std::vector<SDOperand>
115PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 //
117 // add beautiful description of PPC stack frame format, or at least some docs
118 //
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000123
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
128 unsigned ArgOffset = 24;
129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 };
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
139 };
140
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
142 // the arguments start at offset 24, although they are likely to be passed
143 // in registers.
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
146 unsigned ObjSize;
147 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000148 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000149 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150
Nate Begemana9795f82005-03-24 04:41:43 +0000151 switch (ObjectVT) {
152 default: assert(0 && "Unhandled argument type!");
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000156 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000157 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000158 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000159 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000160 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
162 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000163 if (ObjectVT != MVT::i32)
164 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000165 } else {
166 needsLoad = true;
167 }
168 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000169 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000170 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000171 if (GPR_remaining > 0) {
172 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000173 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000174 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
175 // If we have two or more remaining argument registers, then both halves
176 // of the i64 can be sourced from there. Otherwise, the lower half will
177 // have to come off the stack. This can happen when an i64 is preceded
178 // by 28 bytes of arguments.
179 if (GPR_remaining > 1) {
180 MF.addLiveIn(GPR[GPR_idx+1]);
181 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
182 } else {
183 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000185 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
186 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000187 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000188 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000189 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
190 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000191 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000192 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000193 }
194 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000195 case MVT::f32:
196 case MVT::f64:
197 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
198 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000202 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000203 --FPR_remaining;
204 ++FPR_idx;
205 } else {
206 needsLoad = true;
207 }
208 break;
209 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210
Nate Begemana9795f82005-03-24 04:41:43 +0000211 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000213 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000214 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000215 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000216 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000217 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
218 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000220 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000221 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
222 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000223 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224
Nate Begemana9795f82005-03-24 04:41:43 +0000225 // Every 4 bytes of argument space consumes one of the GPRs available for
226 // argument passing.
227 if (GPR_remaining > 0) {
228 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
229 GPR_remaining -= delta;
230 GPR_idx += delta;
231 }
232 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000233 if (newroot.Val)
234 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000235
Nate Begemana9795f82005-03-24 04:41:43 +0000236 ArgValues.push_back(argt);
237 }
238
Nate Begemana9795f82005-03-24 04:41:43 +0000239 // If the function takes variable number of arguments, make a frame index for
240 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000241 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000242 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000243 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000244 // If this function is vararg, store any remaining integer argument regs
245 // to their spots on the stack so that they may be loaded by deferencing the
246 // result of va_next.
247 std::vector<SDOperand> MemOps;
248 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000249 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000250 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000251 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000252 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000253 MemOps.push_back(Store);
254 // Increment the address by four for the next argument to store
255 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
256 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
257 }
258 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000259 }
Nate Begemana9795f82005-03-24 04:41:43 +0000260
Nate Begemancd08e4c2005-04-09 20:09:12 +0000261 // Finally, inform the code generator which regs we return values in.
262 switch (getValueType(F.getReturnType())) {
263 default: assert(0 && "Unknown type!");
264 case MVT::isVoid: break;
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 case MVT::i32:
269 MF.addLiveOut(PPC::R3);
270 break;
271 case MVT::i64:
272 MF.addLiveOut(PPC::R3);
273 MF.addLiveOut(PPC::R4);
274 break;
275 case MVT::f32:
276 case MVT::f64:
277 MF.addLiveOut(PPC::F1);
278 break;
279 }
280
Nate Begemana9795f82005-03-24 04:41:43 +0000281 return ArgValues;
282}
283
284std::pair<SDOperand, SDOperand>
285PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000286 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000287 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000288 SDOperand Callee, ArgListTy &Args,
289 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000290 // args_to_use will accumulate outgoing args for the ISD::CALL case in
291 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000292 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000293
294 // Count how many bytes are to be pushed on the stack, including the linkage
295 // area, and parameter passing area.
296 unsigned NumBytes = 24;
297
298 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000299 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000300 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000301 } else {
302 for (unsigned i = 0, e = Args.size(); i != e; ++i)
303 switch (getValueType(Args[i].second)) {
304 default: assert(0 && "Unknown value type!");
305 case MVT::i1:
306 case MVT::i8:
307 case MVT::i16:
308 case MVT::i32:
309 case MVT::f32:
310 NumBytes += 4;
311 break;
312 case MVT::i64:
313 case MVT::f64:
314 NumBytes += 8;
315 break;
316 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000317
318 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000319 // plus 32 bytes of argument space in case any called code gets funky on us.
320 if (NumBytes < 56) NumBytes = 56;
321
322 // Adjust the stack pointer for the new arguments...
323 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000324 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000325 DAG.getConstant(NumBytes, getPointerTy()));
326
327 // Set up a copy of the stack pointer for use loading and storing any
328 // arguments that may not fit in the registers available for argument
329 // passing.
330 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
331 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000332
Nate Begeman307e7442005-03-26 01:28:53 +0000333 // Figure out which arguments are going to go in registers, and which in
334 // memory. Also, if this is a vararg function, floating point operations
335 // must be stored to our stack, and loaded into integer regs as well, if
336 // any integer regs are available for argument passing.
337 unsigned ArgOffset = 24;
338 unsigned GPR_remaining = 8;
339 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000340
Nate Begeman74d73452005-03-31 00:15:26 +0000341 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000342 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
343 // PtrOff will be used to store the current argument to the stack if a
344 // register cannot be found for it.
345 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
346 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000348
Nate Begemanf7e43382005-03-26 07:46:36 +0000349 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000350 default: assert(0 && "Unexpected ValueType for argument!");
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 // Promote the integer to 32 bits. If the input type is signed use a
355 // sign extend, otherwise use a zero extend.
356 if (Args[i].second->isSigned())
357 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
358 else
359 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
360 // FALL THROUGH
361 case MVT::i32:
362 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000363 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000364 --GPR_remaining;
365 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000367 Args[i].first, PtrOff,
368 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000369 }
370 ArgOffset += 4;
371 break;
372 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000373 // If we have one free GPR left, we can place the upper half of the i64
374 // in it, and store the other half to the stack. If we have two or more
375 // free GPRs, then we can pass both halves of the i64 in registers.
376 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000378 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000379 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000380 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000381 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000382 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000383 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000384 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000386 } else {
387 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
388 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000389 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000390 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000391 }
Nate Begeman307e7442005-03-26 01:28:53 +0000392 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000394 Args[i].first, PtrOff,
395 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000396 }
397 ArgOffset += 8;
398 break;
399 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000400 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000401 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000402 args_to_use.push_back(Args[i].first);
403 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000404 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000405 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000406 Args[i].first, PtrOff,
407 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000408 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000409 // Float varargs are always shadowed in available integer registers
410 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000411 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
412 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000414 args_to_use.push_back(Load);
415 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000416 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000417 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000418 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
419 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
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 } else {
427 // If we have any FPRs remaining, we may also have GPRs remaining.
428 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
429 // GPRs.
430 if (GPR_remaining > 0) {
431 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
432 --GPR_remaining;
433 }
434 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
435 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
436 --GPR_remaining;
437 }
Nate Begeman74d73452005-03-31 00:15:26 +0000438 }
Nate Begeman307e7442005-03-26 01:28:53 +0000439 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000440 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000441 Args[i].first, PtrOff,
442 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000443 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000444 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000445 break;
446 }
Nate Begemana9795f82005-03-24 04:41:43 +0000447 }
Nate Begeman74d73452005-03-31 00:15:26 +0000448 if (!MemOps.empty())
449 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000450 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000451
Nate Begemana9795f82005-03-24 04:41:43 +0000452 std::vector<MVT::ValueType> RetVals;
453 MVT::ValueType RetTyVT = getValueType(RetTy);
454 if (RetTyVT != MVT::isVoid)
455 RetVals.push_back(RetTyVT);
456 RetVals.push_back(MVT::Other);
457
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000459 Chain, Callee, args_to_use), 0);
460 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000461 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000462 DAG.getConstant(NumBytes, getPointerTy()));
463 return std::make_pair(TheCall, Chain);
464}
465
Chris Lattnere0fe2252005-07-05 19:58:54 +0000466SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
467 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000468 // vastart just stores the address of the VarArgsFrameIndex slot into the
469 // memory location argument.
470 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000471 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
472 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000473}
474
Chris Lattnere0fe2252005-07-05 19:58:54 +0000475std::pair<SDOperand,SDOperand>
476PPC32TargetLowering::LowerVAArg(SDOperand Chain,
477 SDOperand VAListP, Value *VAListV,
478 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000479 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000480
481 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000482 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
483 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000484 unsigned Amt;
485 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
486 Amt = 4;
487 else {
488 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
489 "Other types should have been promoted for varargs!");
490 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000491 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000492 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
493 DAG.getConstant(Amt, VAList.getValueType()));
494 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000495 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000496 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000497}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000498
Nate Begemana9795f82005-03-24 04:41:43 +0000499
500std::pair<SDOperand, SDOperand> PPC32TargetLowering::
501LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
502 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000503 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000504 abort();
505}
506
507namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000508Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000509Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000510Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000511//===--------------------------------------------------------------------===//
512/// ISel - PPC32 specific code to select PPC32 machine instructions for
513/// SelectionDAG operations.
514//===--------------------------------------------------------------------===//
515class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000516 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000517 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
518 // for sdiv and udiv until it is put into the future
519 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000520
Nate Begemana9795f82005-03-24 04:41:43 +0000521 /// ExprMap - As shared expressions are codegen'd, we keep track of which
522 /// vreg the value is produced in, so we only emit one copy of each compiled
523 /// tree.
524 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000525
526 unsigned GlobalBaseReg;
527 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000528 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000529public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000530 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
531 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000532
Nate Begemanc7b09f12005-03-25 08:34:25 +0000533 /// runOnFunction - Override this function in order to reset our per-function
534 /// variables.
535 virtual bool runOnFunction(Function &Fn) {
536 // Make sure we re-emit a set of the global base reg if necessary
537 GlobalBaseInitialized = false;
538 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000539 }
540
Nate Begemana9795f82005-03-24 04:41:43 +0000541 /// InstructionSelectBasicBlock - This callback is invoked by
542 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
543 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
544 DEBUG(BB->dump());
545 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000546 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000547 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548
Nate Begemana9795f82005-03-24 04:41:43 +0000549 // Clear state used for selection.
550 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000551 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000552 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000553
554 // dag -> dag expanders for integer divide by constant
555 SDOperand BuildSDIVSequence(SDOperand N);
556 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000557
Nate Begemandffcfcc2005-04-01 00:32:34 +0000558 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000559 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000560 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000561 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000562 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000563 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
564 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000565 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000566 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000567
Nate Begeman04730362005-04-01 04:45:11 +0000568 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000569 void SelectBranchCC(SDOperand N);
570};
571
Nate Begeman80196b12005-04-05 00:15:08 +0000572/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
573/// returns zero when the input is not exactly a power of two.
574static unsigned ExactLog2(unsigned Val) {
575 if (Val == 0 || (Val & (Val-1))) return 0;
576 unsigned Count = 0;
577 while (Val != 1) {
578 Val >>= 1;
579 ++Count;
580 }
581 return Count;
582}
583
Nate Begeman7ddecb42005-04-06 23:51:40 +0000584// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
585// any number of 0's on either side. the 1's are allowed to wrap from LSB to
586// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
587// not, since all 1's are not contiguous.
588static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
589 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000590 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000591 ME = 0;
592
593 // look for first set bit
594 int i = 0;
595 for (; i < 32; i++) {
596 if ((Val & (1 << (31 - i))) != 0) {
597 MB = i;
598 ME = i;
599 break;
600 }
601 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000602
Nate Begeman7ddecb42005-04-06 23:51:40 +0000603 // look for last set bit
604 for (; i < 32; i++) {
605 if ((Val & (1 << (31 - i))) == 0)
606 break;
607 ME = i;
608 }
609
610 // look for next set bit
611 for (; i < 32; i++) {
612 if ((Val & (1 << (31 - i))) != 0)
613 break;
614 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000615
Nate Begeman7ddecb42005-04-06 23:51:40 +0000616 // if we exhausted all the bits, we found a match at this point for 0*1*0*
617 if (i == 32)
618 return true;
619
620 // since we just encountered more 1's, if it doesn't wrap around to the
621 // most significant bit of the word, then we did not find a match to 1*0*1* so
622 // exit.
623 if (MB != 0)
624 return false;
625
626 // look for last set bit
627 for (MB = i; i < 32; i++) {
628 if ((Val & (1 << (31 - i))) == 0)
629 break;
630 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000631
Nate Begeman7ddecb42005-04-06 23:51:40 +0000632 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
633 // the value is not a run of ones.
634 if (i == 32)
635 return true;
636 return false;
637}
638
Nate Begeman439b4442005-04-05 04:22:58 +0000639/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000640/// the ConstantSDNode N can be used as an immediate to Opcode. The return
641/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000642/// ConstantSDNode, or is not suitable for use by that opcode.
643/// Return value codes for turning into an enum someday:
644/// 1: constant may be used in normal immediate form.
645/// 2: constant may be used in shifted immediate form.
646/// 3: log base 2 of the constant may be used.
647/// 4: constant is suitable for integer division conversion
648/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000649///
Nate Begeman439b4442005-04-05 04:22:58 +0000650static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
651 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000652 if (N.getOpcode() != ISD::Constant) return 0;
653
654 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000655
Nate Begemana9795f82005-03-24 04:41:43 +0000656 switch(Opcode) {
657 default: return 0;
658 case ISD::ADD:
659 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
660 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
661 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000662 case ISD::AND: {
663 unsigned MB, ME;
664 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
665 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
666 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
667 break;
668 }
Nate Begemana9795f82005-03-24 04:41:43 +0000669 case ISD::XOR:
670 case ISD::OR:
671 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
672 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
673 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000674 case ISD::MUL:
675 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
676 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000677 case ISD::SUB:
678 // handle subtract-from separately from subtract, since subi is really addi
679 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
680 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
681 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000682 case ISD::SETCC:
683 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
684 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
685 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000686 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000687 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000688 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000689 if (v <= -2 || v >= 2) { return 4; }
690 break;
691 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000692 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000693 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000694 }
695 return 0;
696}
Nate Begeman3e897162005-03-31 23:55:40 +0000697
Nate Begemanc7bd4822005-04-11 06:34:10 +0000698/// NodeHasRecordingVariant - If SelectExpr can always produce code for
699/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
700/// return false.
701static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
702 switch(NodeOpcode) {
703 default: return false;
704 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000705 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000706 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000707 }
708}
709
Nate Begeman3e897162005-03-31 23:55:40 +0000710/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
711/// to Condition. If the Condition is unordered or unsigned, the bool argument
712/// U is set to true, otherwise it is set to false.
713static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
714 U = false;
715 switch (Condition) {
716 default: assert(0 && "Unknown condition!"); abort();
717 case ISD::SETEQ: return PPC::BEQ;
718 case ISD::SETNE: return PPC::BNE;
719 case ISD::SETULT: U = true;
720 case ISD::SETLT: return PPC::BLT;
721 case ISD::SETULE: U = true;
722 case ISD::SETLE: return PPC::BLE;
723 case ISD::SETUGT: U = true;
724 case ISD::SETGT: return PPC::BGT;
725 case ISD::SETUGE: U = true;
726 case ISD::SETGE: return PPC::BGE;
727 }
Nate Begeman04730362005-04-01 04:45:11 +0000728 return 0;
729}
730
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000731/// getCROpForOp - Return the condition register opcode (or inverted opcode)
732/// associated with the SelectionDAG opcode.
733static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
734 switch (Opcode) {
735 default: assert(0 && "Unknown opcode!"); abort();
736 case ISD::AND:
737 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
738 if (!Inv1 && !Inv2) return PPC::CRAND;
739 if (Inv1 ^ Inv2) return PPC::CRANDC;
740 case ISD::OR:
741 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
742 if (!Inv1 && !Inv2) return PPC::CROR;
743 if (Inv1 ^ Inv2) return PPC::CRORC;
744 }
745 return 0;
746}
747
748/// getCRIdxForSetCC - Return the index of the condition register field
749/// associated with the SetCC condition, and whether or not the field is
750/// treated as inverted. That is, lt = 0; ge = 0 inverted.
751static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
752 switch (Condition) {
753 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000754 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000755 case ISD::SETLT: Inv = false; return 0;
756 case ISD::SETUGE:
757 case ISD::SETGE: Inv = true; return 0;
758 case ISD::SETUGT:
759 case ISD::SETGT: Inv = false; return 1;
760 case ISD::SETULE:
761 case ISD::SETLE: Inv = true; return 1;
762 case ISD::SETEQ: Inv = false; return 2;
763 case ISD::SETNE: Inv = true; return 2;
764 }
765 return 0;
766}
767
Nate Begeman04730362005-04-01 04:45:11 +0000768/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
769/// and store immediate instructions.
770static unsigned IndexedOpForOp(unsigned Opcode) {
771 switch(Opcode) {
772 default: assert(0 && "Unknown opcode!"); abort();
773 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
774 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
775 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
776 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
777 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
778 case PPC::LFD: return PPC::LFDX;
779 }
780 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000781}
Nate Begeman815d6da2005-04-06 00:25:27 +0000782
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000783// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000784// a multiply.
785struct ms {
786 int m; // magic number
787 int s; // shift amount
788};
789
790struct mu {
791 unsigned int m; // magic number
792 int a; // add indicator
793 int s; // shift amount
794};
795
796/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000797/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000798/// or -1.
799static struct ms magic(int d) {
800 int p;
801 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
802 const unsigned int two31 = 2147483648U; // 2^31
803 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000804
Nate Begeman815d6da2005-04-06 00:25:27 +0000805 ad = abs(d);
806 t = two31 + ((unsigned int)d >> 31);
807 anc = t - 1 - t%ad; // absolute value of nc
808 p = 31; // initialize p
809 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
810 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
811 q2 = two31/ad; // initialize q2 = 2p/abs(d)
812 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
813 do {
814 p = p + 1;
815 q1 = 2*q1; // update q1 = 2p/abs(nc)
816 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
817 if (r1 >= anc) { // must be unsigned comparison
818 q1 = q1 + 1;
819 r1 = r1 - anc;
820 }
821 q2 = 2*q2; // update q2 = 2p/abs(d)
822 r2 = 2*r2; // update r2 = rem(2p/abs(d))
823 if (r2 >= ad) { // must be unsigned comparison
824 q2 = q2 + 1;
825 r2 = r2 - ad;
826 }
827 delta = ad - r2;
828 } while (q1 < delta || (q1 == delta && r1 == 0));
829
830 mag.m = q2 + 1;
831 if (d < 0) mag.m = -mag.m; // resulting magic number
832 mag.s = p - 32; // resulting shift
833 return mag;
834}
835
836/// magicu - calculate the magic numbers required to codegen an integer udiv as
837/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
838static struct mu magicu(unsigned d)
839{
840 int p;
841 unsigned int nc, delta, q1, r1, q2, r2;
842 struct mu magu;
843 magu.a = 0; // initialize "add" indicator
844 nc = - 1 - (-d)%d;
845 p = 31; // initialize p
846 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
847 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
848 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
849 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
850 do {
851 p = p + 1;
852 if (r1 >= nc - r1 ) {
853 q1 = 2*q1 + 1; // update q1
854 r1 = 2*r1 - nc; // update r1
855 }
856 else {
857 q1 = 2*q1; // update q1
858 r1 = 2*r1; // update r1
859 }
860 if (r2 + 1 >= d - r2) {
861 if (q2 >= 0x7FFFFFFF) magu.a = 1;
862 q2 = 2*q2 + 1; // update q2
863 r2 = 2*r2 + 1 - d; // update r2
864 }
865 else {
866 if (q2 >= 0x80000000) magu.a = 1;
867 q2 = 2*q2; // update q2
868 r2 = 2*r2 + 1; // update r2
869 }
870 delta = d - 1 - r2;
871 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
872 magu.m = q2 + 1; // resulting magic number
873 magu.s = p - 32; // resulting shift
874 return magu;
875}
876}
877
878/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
879/// return a DAG expression to select that will generate the same value by
880/// multiplying by a magic number. See:
881/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
882SDOperand ISel::BuildSDIVSequence(SDOperand N) {
883 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
884 ms magics = magic(d);
885 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000886 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000887 ISelDAG->getConstant(magics.m, MVT::i32));
888 // If d > 0 and m < 0, add the numerator
889 if (d > 0 && magics.m < 0)
890 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
891 // If d < 0 and m > 0, subtract the numerator.
892 if (d < 0 && magics.m > 0)
893 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
894 // Shift right algebraic if shift value is nonzero
895 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000896 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000897 ISelDAG->getConstant(magics.s, MVT::i32));
898 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000899 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000900 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000901 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000902}
903
904/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
905/// return a DAG expression to select that will generate the same value by
906/// multiplying by a magic number. See:
907/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
908SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000909 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000910 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
911 mu magics = magicu(d);
912 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000913 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000914 ISelDAG->getConstant(magics.m, MVT::i32));
915 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000916 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000917 ISelDAG->getConstant(magics.s, MVT::i32));
918 } else {
919 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000920 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000921 ISelDAG->getConstant(1, MVT::i32));
922 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000923 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000924 ISelDAG->getConstant(magics.s-1, MVT::i32));
925 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000926 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000927}
928
Nate Begemanc7b09f12005-03-25 08:34:25 +0000929/// getGlobalBaseReg - Output the instructions required to put the
930/// base address to use for accessing globals into a register.
931///
932unsigned ISel::getGlobalBaseReg() {
933 if (!GlobalBaseInitialized) {
934 // Insert the set of GlobalBaseReg into the first MBB of the function
935 MachineBasicBlock &FirstMBB = BB->getParent()->front();
936 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
937 GlobalBaseReg = MakeReg(MVT::i32);
938 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
939 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
940 GlobalBaseInitialized = true;
941 }
942 return GlobalBaseReg;
943}
944
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000945/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000946/// Constant Pool. Optionally takes a register in which to load the value.
947unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
948 unsigned Tmp1 = MakeReg(MVT::i32);
949 if (0 == Result) Result = MakeReg(MVT::f64);
950 MachineConstantPool *CP = BB->getParent()->getConstantPool();
951 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
952 unsigned CPI = CP->getConstantPoolIndex(CFP);
953 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
954 .addConstantPoolIndex(CPI);
955 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
956 return Result;
957}
958
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000959/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000960/// Inv is true, then invert the result.
961void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
962 unsigned IntCR = MakeReg(MVT::i32);
963 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
964 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
965 if (Inv) {
966 unsigned Tmp1 = MakeReg(MVT::i32);
967 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
968 .addImm(31).addImm(31);
969 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
970 } else {
971 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
972 .addImm(31).addImm(31);
973 }
974}
975
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000976/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000977/// the rotate left word immediate then mask insert (rlwimi) instruction.
978/// Returns true on success, false if the caller still needs to select OR.
979///
980/// Patterns matched:
981/// 1. or shl, and 5. or and, and
982/// 2. or and, shl 6. or shl, shr
983/// 3. or shr, and 7. or shr, shl
984/// 4. or and, shr
985bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000986 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000987 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000988
989 SDOperand Op0 = OR.getOperand(0);
990 SDOperand Op1 = OR.getOperand(1);
991
992 unsigned Op0Opc = Op0.getOpcode();
993 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000994
Nate Begeman7ddecb42005-04-06 23:51:40 +0000995 // Verify that we have the correct opcodes
996 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
997 return false;
998 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
999 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001000
Nate Begeman7ddecb42005-04-06 23:51:40 +00001001 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001002 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001003 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001004 switch(Op0Opc) {
1005 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1006 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1007 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1008 }
1009 } else {
1010 return false;
1011 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001012
Nate Begeman7ddecb42005-04-06 23:51:40 +00001013 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001014 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001015 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001016 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001017 case ISD::SHL:
1018 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001019 InsMask <<= Amount;
1020 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001021 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001022 case ISD::SRL:
1023 Amount = CN->getValue();
1024 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001025 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001026 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001027 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001028 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001029 InsMask &= (unsigned)CN->getValue();
1030 break;
1031 }
1032 } else {
1033 return false;
1034 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001035
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001036 unsigned Tmp3 = 0;
1037
1038 // If both of the inputs are ANDs and one of them has a logical shift by
1039 // constant as its input, make that the inserted value so that we can combine
1040 // the shift into the rotate part of the rlwimi instruction
1041 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1042 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1043 Op1.getOperand(0).getOpcode() == ISD::SRL) {
1044 if (ConstantSDNode *CN =
1045 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1046 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1047 CN->getValue() : 32 - CN->getValue();
1048 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1049 }
1050 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1051 Op0.getOperand(0).getOpcode() == ISD::SRL) {
1052 if (ConstantSDNode *CN =
1053 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1054 std::swap(Op0, Op1);
1055 std::swap(TgtMask, InsMask);
1056 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1057 CN->getValue() : 32 - CN->getValue();
1058 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1059 }
1060 }
1061 }
1062
Nate Begeman7ddecb42005-04-06 23:51:40 +00001063 // Verify that the Target mask and Insert mask together form a full word mask
1064 // and that the Insert mask is a run of set bits (which implies both are runs
1065 // of set bits). Given that, Select the arguments and generate the rlwimi
1066 // instruction.
1067 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001068 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001069 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001070 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001071 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1072 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001073 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001074 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001075 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1076 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1077 .addImm(0).addImm(31);
1078 return true;
1079 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001080 if (Op0Opc == ISD::AND && fullMask)
1081 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001082 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001083 Tmp1 = SelectExpr(Op0);
1084 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001085 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1086 .addImm(Amount).addImm(MB).addImm(ME);
1087 return true;
1088 }
1089 return false;
1090}
1091
Nate Begeman3664cef2005-04-13 22:14:14 +00001092/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1093/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1094/// wider than the implicit mask, then we can get rid of the AND and let the
1095/// shift do the mask.
1096unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1097 unsigned C;
1098 if (N.getOpcode() == ISD::AND &&
1099 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1100 31 == (C & 0xFFFF) && // ME
1101 26 >= (C >> 16)) // MB
1102 return SelectExpr(N.getOperand(0));
1103 else
1104 return SelectExpr(N);
1105}
1106
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001107unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001108 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001109 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001110 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001111 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001112
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001113 // Allocate a condition register for this expression
1114 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001115
Nate Begemandffcfcc2005-04-01 00:32:34 +00001116 // If the first operand to the select is a SETCC node, then we can fold it
1117 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001118 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001119 bool U;
1120 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001121 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001122
Nate Begeman439b4442005-04-05 04:22:58 +00001123 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001124 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001125 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001126 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001127 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001128 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1129 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001130 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001131 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1132 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001133 RecordSuccess = false;
1134 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1135 if (RecordSuccess) {
1136 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001137 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1138 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001139 }
1140 AlreadySelected = true;
1141 }
1142 // If we could not implicitly set CR0, then emit a compare immediate
1143 // instead.
1144 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001145 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001146 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001147 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001148 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001149 } else {
1150 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1151 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001152 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001153 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001154 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001155 }
1156 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001157 // If this isn't a SetCC, then select the value and compare it against zero,
1158 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001159 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001160 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001161 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001162 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001163 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001164 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001165}
1166
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001167unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001168 unsigned &Idx) {
1169 bool Inv0, Inv1;
1170 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1171
1172 // Allocate a condition register for this expression
1173 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1174
1175 // Check for the operations we support:
1176 switch(N.getOpcode()) {
1177 default:
1178 Opc = PPC::BNE;
1179 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1180 Tmp1 = SelectExpr(N);
1181 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1182 break;
1183 case ISD::OR:
1184 case ISD::AND:
1185 ++MultiBranch;
1186 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1187 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1188 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1189 if (Inv0 && !Inv1) {
1190 std::swap(Tmp1, Tmp2);
1191 std::swap(Idx0, Idx1);
1192 Opc = Opc1;
1193 }
1194 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1195 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1196 .addReg(Tmp2).addImm(Idx1);
1197 Inv = false;
1198 Idx = Idx0;
1199 break;
1200 case ISD::SETCC:
1201 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1202 Result = Tmp1;
1203 break;
1204 }
1205 return Result;
1206}
1207
Nate Begemandffcfcc2005-04-01 00:32:34 +00001208/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001209bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001210{
Nate Begeman96fc6812005-03-31 02:05:53 +00001211 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001212 if (N.getOpcode() == ISD::ADD) {
1213 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001214 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001215 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001216 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001217 }
Nate Begeman04730362005-04-01 04:45:11 +00001218 offset = SelectExpr(N.getOperand(1));
1219 return true;
1220 }
Nate Begemana9795f82005-03-24 04:41:43 +00001221 Reg = SelectExpr(N);
1222 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001223 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001224}
1225
1226void ISel::SelectBranchCC(SDOperand N)
1227{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001228 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001229 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001230
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001231 bool Inv;
1232 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001233 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001234 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001235
Nate Begeman439009c2005-06-15 18:22:43 +00001236 // Iterate to the next basic block
1237 ilist<MachineBasicBlock>::iterator It = BB;
1238 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001239
1240 // If this is a two way branch, then grab the fallthrough basic block argument
1241 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1242 // if necessary by the branch selection pass. Otherwise, emit a standard
1243 // conditional branch.
1244 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001245 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001246 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1247 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001248 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001249 .addMBB(Dest).addMBB(Fallthrough);
1250 if (Fallthrough != It)
1251 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1252 } else {
1253 if (Fallthrough != It) {
1254 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001255 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001256 .addMBB(Fallthrough).addMBB(Dest);
1257 }
1258 }
1259 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001260 // If the fallthrough path is off the end of the function, which would be
1261 // undefined behavior, set it to be the same as the current block because
1262 // we have nothing better to set it to, and leaving it alone will cause the
1263 // PowerPC Branch Selection pass to crash.
1264 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001265 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001266 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001267 }
Nate Begemana9795f82005-03-24 04:41:43 +00001268 return;
1269}
1270
Nate Begemanc7bd4822005-04-11 06:34:10 +00001271unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001272 unsigned Result;
1273 unsigned Tmp1, Tmp2, Tmp3;
1274 unsigned Opc = 0;
1275 unsigned opcode = N.getOpcode();
1276
1277 SDNode *Node = N.Val;
1278 MVT::ValueType DestType = N.getValueType();
1279
Nate Begemana43b1762005-06-14 03:55:23 +00001280 if (Node->getOpcode() == ISD::CopyFromReg &&
1281 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1282 // Just use the specified register as our input.
1283 return cast<RegSDNode>(Node)->getReg();
1284
Nate Begemana9795f82005-03-24 04:41:43 +00001285 unsigned &Reg = ExprMap[N];
1286 if (Reg) return Reg;
1287
Nate Begeman27eeb002005-04-02 05:59:34 +00001288 switch (N.getOpcode()) {
1289 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001290 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001291 MakeReg(N.getValueType()) : 1;
1292 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001293 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001294 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001295 // If this is a call instruction, make sure to prepare ALL of the result
1296 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001297 if (Node->getNumValues() == 1)
1298 Reg = Result = 1; // Void call, just a chain.
1299 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001300 Result = MakeReg(Node->getValueType(0));
1301 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001302 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001303 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001304 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001305 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001306 break;
1307 case ISD::ADD_PARTS:
1308 case ISD::SUB_PARTS:
1309 case ISD::SHL_PARTS:
1310 case ISD::SRL_PARTS:
1311 case ISD::SRA_PARTS:
1312 Result = MakeReg(Node->getValueType(0));
1313 ExprMap[N.getValue(0)] = Result;
1314 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1315 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1316 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001317 }
1318
Nate Begemana9795f82005-03-24 04:41:43 +00001319 switch (opcode) {
1320 default:
1321 Node->dump();
1322 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001323 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001324 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1325 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001326 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001327 // Generate both result values. FIXME: Need a better commment here?
1328 if (Result != 1)
1329 ExprMap[N.getValue(1)] = 1;
1330 else
1331 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1332
1333 // FIXME: We are currently ignoring the requested alignment for handling
1334 // greater than the stack alignment. This will need to be revisited at some
1335 // point. Align = N.getOperand(2);
1336 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1337 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1338 std::cerr << "Cannot allocate stack object with greater alignment than"
1339 << " the stack alignment yet!";
1340 abort();
1341 }
1342 Select(N.getOperand(0));
1343 Tmp1 = SelectExpr(N.getOperand(1));
1344 // Subtract size from stack pointer, thereby allocating some space.
1345 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1346 // Put a pointer to the space into the result register by copying the SP
1347 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1348 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001349
1350 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001351 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1352 Tmp2 = MakeReg(MVT::i32);
1353 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1354 .addConstantPoolIndex(Tmp1);
1355 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1356 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001357
1358 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001359 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001360 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001361 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001362
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001363 case ISD::GlobalAddress: {
1364 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001365 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001366 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1367 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001368 if (GV->hasWeakLinkage() || GV->isExternal()) {
1369 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1370 } else {
1371 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1372 }
1373 return Result;
1374 }
1375
Nate Begeman5e966612005-03-24 06:28:42 +00001376 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001377 case ISD::EXTLOAD:
1378 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001379 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001380 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001381 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001382 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001383
Nate Begeman5e966612005-03-24 06:28:42 +00001384 // Make sure we generate both values.
1385 if (Result != 1)
1386 ExprMap[N.getValue(1)] = 1; // Generate the token
1387 else
1388 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1389
1390 SDOperand Chain = N.getOperand(0);
1391 SDOperand Address = N.getOperand(1);
1392 Select(Chain);
1393
Nate Begeman9db505c2005-03-28 19:36:43 +00001394 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001395 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001396 case MVT::i1: Opc = PPC::LBZ; break;
1397 case MVT::i8: Opc = PPC::LBZ; break;
1398 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1399 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001400 case MVT::f32: Opc = PPC::LFS; break;
1401 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001402 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001403
Nate Begeman74d73452005-03-31 00:15:26 +00001404 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1405 Tmp1 = MakeReg(MVT::i32);
1406 int CPI = CP->getIndex();
1407 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1408 .addConstantPoolIndex(CPI);
1409 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001410 }
Nate Begeman74d73452005-03-31 00:15:26 +00001411 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001412 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1413 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001414 } else {
1415 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001416 bool idx = SelectAddr(Address, Tmp1, offset);
1417 if (idx) {
1418 Opc = IndexedOpForOp(Opc);
1419 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1420 } else {
1421 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1422 }
Nate Begeman5e966612005-03-24 06:28:42 +00001423 }
1424 return Result;
1425 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001426
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001427 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001428 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001429 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001430 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001431 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1432 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1433 };
1434 static const unsigned FPR[] = {
1435 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1436 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1437 };
1438
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001439 // Lower the chain for this call.
1440 Select(N.getOperand(0));
1441 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001442
Nate Begemand860aa62005-04-04 22:17:48 +00001443 MachineInstr *CallMI;
1444 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001445 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001446 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001447 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001448 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001449 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001450 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001451 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001452 true);
1453 } else {
1454 Tmp1 = SelectExpr(N.getOperand(1));
1455 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1456 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1457 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1458 .addReg(PPC::R12);
1459 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001460
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001461 // Load the register args to virtual regs
1462 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001463 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001464 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1465
1466 // Copy the virtual registers into the appropriate argument register
1467 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1468 switch(N.getOperand(i+2).getValueType()) {
1469 default: Node->dump(); assert(0 && "Unknown value type for call");
1470 case MVT::i1:
1471 case MVT::i8:
1472 case MVT::i16:
1473 case MVT::i32:
1474 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001475 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001476 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001477 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1478 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001479 ++GPR_idx;
1480 break;
1481 case MVT::f64:
1482 case MVT::f32:
1483 assert(FPR_idx < 13 && "Too many fp args");
1484 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001485 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001486 ++FPR_idx;
1487 break;
1488 }
1489 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001490
Nate Begemand860aa62005-04-04 22:17:48 +00001491 // Put the call instruction in the correct place in the MachineBasicBlock
1492 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001493
1494 switch (Node->getValueType(0)) {
1495 default: assert(0 && "Unknown value type for call result!");
1496 case MVT::Other: return 1;
1497 case MVT::i1:
1498 case MVT::i8:
1499 case MVT::i16:
1500 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001501 if (Node->getValueType(1) == MVT::i32) {
1502 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1503 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1504 } else {
1505 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1506 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001507 break;
1508 case MVT::f32:
1509 case MVT::f64:
1510 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1511 break;
1512 }
1513 return Result+N.ResNo;
1514 }
Nate Begemana9795f82005-03-24 04:41:43 +00001515
1516 case ISD::SIGN_EXTEND:
1517 case ISD::SIGN_EXTEND_INREG:
1518 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001519 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001520 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001521 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001522 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001523 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001524 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001525 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001526 break;
Nate Begeman74747862005-03-29 22:24:51 +00001527 case MVT::i1:
1528 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1529 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001530 }
Nate Begemana9795f82005-03-24 04:41:43 +00001531 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001532
Nate Begemana9795f82005-03-24 04:41:43 +00001533 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001534 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001535 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001536 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001537 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001538 if (MVT::isInteger(DestType))
1539 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1540 else
1541 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001542 return Result;
1543
1544 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001545 Tmp1 = SelectExpr(N.getOperand(0));
1546 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1547 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001548 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001549 .addImm(31-Tmp2);
1550 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001551 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001552 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1553 }
1554 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001555
Nate Begeman5e966612005-03-24 06:28:42 +00001556 case ISD::SRL:
1557 Tmp1 = SelectExpr(N.getOperand(0));
1558 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1559 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001560 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001561 .addImm(Tmp2).addImm(31);
1562 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001563 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001564 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1565 }
1566 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001567
Nate Begeman5e966612005-03-24 06:28:42 +00001568 case ISD::SRA:
1569 Tmp1 = SelectExpr(N.getOperand(0));
1570 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1571 Tmp2 = CN->getValue() & 0x1F;
1572 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1573 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001574 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001575 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1576 }
1577 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001578
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001579 case ISD::CTLZ:
1580 Tmp1 = SelectExpr(N.getOperand(0));
1581 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1582 return Result;
1583
Nate Begemana9795f82005-03-24 04:41:43 +00001584 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001585 if (!MVT::isInteger(DestType)) {
1586 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1587 N.getOperand(0).Val->hasOneUse()) {
1588 ++FusedFP; // Statistic
1589 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1590 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1591 Tmp3 = SelectExpr(N.getOperand(1));
1592 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1593 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1594 return Result;
1595 }
1596 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1597 N.getOperand(1).Val->hasOneUse()) {
1598 ++FusedFP; // Statistic
1599 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1600 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1601 Tmp3 = SelectExpr(N.getOperand(0));
1602 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1603 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1604 return Result;
1605 }
1606 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1607 Tmp1 = SelectExpr(N.getOperand(0));
1608 Tmp2 = SelectExpr(N.getOperand(1));
1609 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1610 return Result;
1611 }
Nate Begemana9795f82005-03-24 04:41:43 +00001612 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001613 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001614 default: assert(0 && "unhandled result code");
1615 case 0: // No immediate
1616 Tmp2 = SelectExpr(N.getOperand(1));
1617 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1618 break;
1619 case 1: // Low immediate
1620 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1621 break;
1622 case 2: // Shifted immediate
1623 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1624 break;
1625 }
1626 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001627
Nate Begemana9795f82005-03-24 04:41:43 +00001628 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001629 // FIXME: should add check in getImmediateForOpcode to return a value
1630 // indicating the immediate is a run of set bits so we can emit a bitfield
1631 // clear with RLWINM instead.
1632 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1633 default: assert(0 && "unhandled result code");
1634 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001635 // Check for andc: and, (xor a, -1), b
1636 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1637 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1638 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1639 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1640 Tmp2 = SelectExpr(N.getOperand(1));
1641 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1642 return Result;
1643 }
1644 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001645 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001646 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001647 Opc = Recording ? PPC::ANDo : PPC::AND;
1648 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001649 break;
1650 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001651 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001652 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1653 break;
1654 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001655 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001656 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1657 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001658 case 5: // Bitfield mask
1659 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1660 Tmp3 = Tmp2 >> 16; // MB
1661 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001662
1663 if (N.getOperand(0).getOpcode() == ISD::SRL)
1664 if (ConstantSDNode *SA =
1665 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1666
1667 // We can fold the RLWINM and the SRL together if the mask is
1668 // clearing the top bits which are rotated around.
1669 unsigned RotAmt = 32-(SA->getValue() & 31);
1670 if (Tmp2 <= RotAmt) {
1671 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1672 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1673 .addImm(Tmp3).addImm(Tmp2);
1674 break;
1675 }
1676 }
1677
1678 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001679 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1680 .addImm(Tmp3).addImm(Tmp2);
1681 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001682 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001683 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001684 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001685
Nate Begemana9795f82005-03-24 04:41:43 +00001686 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001687 if (SelectBitfieldInsert(N, Result))
1688 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001689 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001690 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001691 default: assert(0 && "unhandled result code");
1692 case 0: // No immediate
1693 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001694 Opc = Recording ? PPC::ORo : PPC::OR;
1695 RecordSuccess = true;
1696 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001697 break;
1698 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001699 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001700 break;
1701 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001702 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001703 break;
1704 }
1705 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001706
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001707 case ISD::XOR: {
1708 // Check for EQV: xor, (xor a, -1), b
1709 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1710 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1711 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001712 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1713 Tmp2 = SelectExpr(N.getOperand(1));
1714 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1715 return Result;
1716 }
Chris Lattner837a5212005-04-21 21:09:11 +00001717 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001718 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1719 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001720 switch(N.getOperand(0).getOpcode()) {
1721 case ISD::OR:
1722 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1723 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1724 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1725 break;
1726 case ISD::AND:
1727 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1728 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1729 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1730 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001731 case ISD::XOR:
1732 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1733 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1734 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1735 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001736 default:
1737 Tmp1 = SelectExpr(N.getOperand(0));
1738 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1739 break;
1740 }
1741 return Result;
1742 }
1743 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001744 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001745 default: assert(0 && "unhandled result code");
1746 case 0: // No immediate
1747 Tmp2 = SelectExpr(N.getOperand(1));
1748 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1749 break;
1750 case 1: // Low immediate
1751 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1752 break;
1753 case 2: // Shifted immediate
1754 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1755 break;
1756 }
1757 return Result;
1758 }
1759
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001760 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001761 if (!MVT::isInteger(DestType)) {
1762 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1763 N.getOperand(0).Val->hasOneUse()) {
1764 ++FusedFP; // Statistic
1765 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1766 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1767 Tmp3 = SelectExpr(N.getOperand(1));
1768 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1769 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1770 return Result;
1771 }
1772 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1773 N.getOperand(1).Val->hasOneUse()) {
1774 ++FusedFP; // Statistic
1775 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1776 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1777 Tmp3 = SelectExpr(N.getOperand(0));
1778 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1779 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1780 return Result;
1781 }
1782 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1783 Tmp1 = SelectExpr(N.getOperand(0));
1784 Tmp2 = SelectExpr(N.getOperand(1));
1785 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 return Result;
1787 }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001788 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
1789 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001790 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001791 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00001792 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001793 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1794 } else {
1795 Tmp1 = SelectExpr(N.getOperand(0));
1796 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001797 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1798 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001799 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001800
Nate Begeman5e966612005-03-24 06:28:42 +00001801 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001802 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001803 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001804 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1805 else {
1806 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001807 switch (DestType) {
1808 default: assert(0 && "Unknown type to ISD::MUL"); break;
1809 case MVT::i32: Opc = PPC::MULLW; break;
1810 case MVT::f32: Opc = PPC::FMULS; break;
1811 case MVT::f64: Opc = PPC::FMUL; break;
1812 }
1813 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001814 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001815 return Result;
1816
Nate Begeman815d6da2005-04-06 00:25:27 +00001817 case ISD::MULHS:
1818 case ISD::MULHU:
1819 Tmp1 = SelectExpr(N.getOperand(0));
1820 Tmp2 = SelectExpr(N.getOperand(1));
1821 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 return Result;
1824
Nate Begemanf3d08f32005-03-29 00:03:27 +00001825 case ISD::SDIV:
1826 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001827 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1828 default: break;
1829 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1830 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001831 Tmp1 = MakeReg(MVT::i32);
1832 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001833 if ((int)Tmp3 < 0) {
1834 unsigned Tmp4 = MakeReg(MVT::i32);
1835 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1836 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1837 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1838 } else {
1839 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1840 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1841 }
Nate Begeman80196b12005-04-05 00:15:08 +00001842 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001843 // If this is a divide by constant, we can emit code using some magic
1844 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001845 case 4:
1846 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001847 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001848 return SelectExpr(BuildSDIVSequence(N));
1849 else
1850 return SelectExpr(BuildUDIVSequence(N));
Nate Begemana3fd4002005-07-19 16:51:05 +00001851 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001852 Tmp1 = SelectExpr(N.getOperand(0));
1853 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001854 switch (DestType) {
1855 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1856 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1857 case MVT::f32: Opc = PPC::FDIVS; break;
1858 case MVT::f64: Opc = PPC::FDIV; break;
1859 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001860 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1861 return Result;
1862
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001863 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001864 case ISD::SUB_PARTS: {
1865 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1866 "Not an i64 add/sub!");
1867 // Emit all of the operands.
1868 std::vector<unsigned> InVals;
1869 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1870 InVals.push_back(SelectExpr(N.getOperand(i)));
1871 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001872 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1873 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001874 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001875 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1876 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1877 }
1878 return Result+N.ResNo;
1879 }
1880
1881 case ISD::SHL_PARTS:
1882 case ISD::SRA_PARTS:
1883 case ISD::SRL_PARTS: {
1884 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1885 "Not an i64 shift!");
1886 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1887 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001888 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1889 Tmp1 = MakeReg(MVT::i32);
1890 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001891 Tmp3 = MakeReg(MVT::i32);
1892 unsigned Tmp4 = MakeReg(MVT::i32);
1893 unsigned Tmp5 = MakeReg(MVT::i32);
1894 unsigned Tmp6 = MakeReg(MVT::i32);
1895 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1896 if (ISD::SHL_PARTS == opcode) {
1897 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1898 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1899 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1900 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001901 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001902 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1903 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1904 } else if (ISD::SRL_PARTS == opcode) {
1905 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1906 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1907 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1908 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1909 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1910 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1911 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1912 } else {
1913 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1914 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1915 MachineBasicBlock *OldMBB = BB;
1916 MachineFunction *F = BB->getParent();
1917 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1918 F->getBasicBlockList().insert(It, TmpMBB);
1919 F->getBasicBlockList().insert(It, PhiMBB);
1920 BB->addSuccessor(TmpMBB);
1921 BB->addSuccessor(PhiMBB);
1922 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1923 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1924 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1925 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1926 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1927 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1928 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1929 // Select correct least significant half if the shift amount > 32
1930 BB = TmpMBB;
1931 unsigned Tmp7 = MakeReg(MVT::i32);
1932 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1933 TmpMBB->addSuccessor(PhiMBB);
1934 BB = PhiMBB;
1935 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1936 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001937 }
1938 return Result+N.ResNo;
1939 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001940
Nate Begemana9795f82005-03-24 04:41:43 +00001941 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001942 case ISD::FP_TO_SINT: {
1943 bool U = (ISD::FP_TO_UINT == opcode);
1944 Tmp1 = SelectExpr(N.getOperand(0));
1945 if (!U) {
1946 Tmp2 = MakeReg(MVT::f64);
1947 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1948 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1949 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1950 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1951 return Result;
1952 } else {
1953 unsigned Zero = getConstDouble(0.0);
1954 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1955 unsigned Border = getConstDouble(1LL << 31);
1956 unsigned UseZero = MakeReg(MVT::f64);
1957 unsigned UseMaxInt = MakeReg(MVT::f64);
1958 unsigned UseChoice = MakeReg(MVT::f64);
1959 unsigned TmpReg = MakeReg(MVT::f64);
1960 unsigned TmpReg2 = MakeReg(MVT::f64);
1961 unsigned ConvReg = MakeReg(MVT::f64);
1962 unsigned IntTmp = MakeReg(MVT::i32);
1963 unsigned XorReg = MakeReg(MVT::i32);
1964 MachineFunction *F = BB->getParent();
1965 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1966 // Update machine-CFG edges
1967 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1968 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1969 MachineBasicBlock *OldMBB = BB;
1970 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1971 F->getBasicBlockList().insert(It, XorMBB);
1972 F->getBasicBlockList().insert(It, PhiMBB);
1973 BB->addSuccessor(XorMBB);
1974 BB->addSuccessor(PhiMBB);
1975 // Convert from floating point to unsigned 32-bit value
1976 // Use 0 if incoming value is < 0.0
1977 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1978 // Use 2**32 - 1 if incoming value is >= 2**32
1979 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1980 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1981 .addReg(MaxInt);
1982 // Subtract 2**31
1983 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1984 // Use difference if >= 2**31
1985 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1986 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1987 .addReg(UseChoice);
1988 // Convert to integer
1989 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1990 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1991 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1992 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1993 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1994
1995 // XorMBB:
1996 // add 2**31 if input was >= 2**31
1997 BB = XorMBB;
1998 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1999 XorMBB->addSuccessor(PhiMBB);
2000
2001 // PhiMBB:
2002 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2003 BB = PhiMBB;
2004 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2005 .addReg(XorReg).addMBB(XorMBB);
2006 return Result;
2007 }
2008 assert(0 && "Should never get here");
2009 return 0;
2010 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002011
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002012 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002013 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002014 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002015 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002016 // We can codegen setcc op, imm very efficiently compared to a brcond.
2017 // Check for those cases here.
2018 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002019 if (CN->getValue() == 0) {
2020 Tmp1 = SelectExpr(SetCC->getOperand(0));
2021 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002022 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002023 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002024 Tmp2 = MakeReg(MVT::i32);
2025 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2026 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2027 .addImm(5).addImm(31);
2028 break;
2029 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002030 Tmp2 = MakeReg(MVT::i32);
2031 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2032 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2033 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002034 case ISD::SETLT:
2035 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2036 .addImm(31).addImm(31);
2037 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002038 case ISD::SETGT:
2039 Tmp2 = MakeReg(MVT::i32);
2040 Tmp3 = MakeReg(MVT::i32);
2041 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2042 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2043 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2044 .addImm(31).addImm(31);
2045 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002046 }
2047 return Result;
2048 }
2049 // setcc op, -1
2050 if (CN->isAllOnesValue()) {
2051 Tmp1 = SelectExpr(SetCC->getOperand(0));
2052 switch (SetCC->getCondition()) {
2053 default: assert(0 && "Unhandled SetCC condition"); abort();
2054 case ISD::SETEQ:
2055 Tmp2 = MakeReg(MVT::i32);
2056 Tmp3 = MakeReg(MVT::i32);
2057 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2058 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2059 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002060 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002061 case ISD::SETNE:
2062 Tmp2 = MakeReg(MVT::i32);
2063 Tmp3 = MakeReg(MVT::i32);
2064 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2065 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2066 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2067 break;
2068 case ISD::SETLT:
2069 Tmp2 = MakeReg(MVT::i32);
2070 Tmp3 = MakeReg(MVT::i32);
2071 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2072 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2073 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2074 .addImm(31).addImm(31);
2075 break;
2076 case ISD::SETGT:
2077 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002078 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2079 .addImm(31).addImm(31);
2080 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2081 break;
2082 }
2083 return Result;
2084 }
2085 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002086
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002087 bool Inv;
2088 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2089 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002090 return Result;
2091 }
2092 assert(0 && "Is this legal?");
2093 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002094
Nate Begeman74747862005-03-29 22:24:51 +00002095 case ISD::SELECT: {
Nate Begemana3fd4002005-07-19 16:51:05 +00002096 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2097 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2098 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2099 !MVT::isInteger(N.getOperand(1).getValueType()) &&
2100 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2101 SetCC->getCondition() != ISD::SETEQ &&
2102 SetCC->getCondition() != ISD::SETNE) {
2103 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2104 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2105 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2106
2107 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2108 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2109 switch(SetCC->getCondition()) {
2110 default: assert(0 && "Invalid FSEL condition"); abort();
2111 case ISD::SETULT:
2112 case ISD::SETLT:
2113 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2114 case ISD::SETUGE:
2115 case ISD::SETGE:
2116 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2117 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2118 return Result;
2119 case ISD::SETUGT:
2120 case ISD::SETGT:
2121 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2122 case ISD::SETULE:
2123 case ISD::SETLE: {
2124 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2125 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2126 } else {
2127 Tmp2 = MakeReg(VT);
2128 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2129 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2130 }
2131 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2132 return Result;
2133 }
2134 }
2135 } else {
2136 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2137 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2138 Tmp2 = SelectExpr(SetCC->getOperand(1));
2139 Tmp3 = MakeReg(VT);
2140 switch(SetCC->getCondition()) {
2141 default: assert(0 && "Invalid FSEL condition"); abort();
2142 case ISD::SETULT:
2143 case ISD::SETLT:
2144 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2145 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2146 return Result;
2147 case ISD::SETUGE:
2148 case ISD::SETGE:
2149 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2150 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2151 return Result;
2152 case ISD::SETUGT:
2153 case ISD::SETGT:
2154 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2155 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2156 return Result;
2157 case ISD::SETULE:
2158 case ISD::SETLE:
2159 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2160 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2161 return Result;
2162 }
2163 }
2164 assert(0 && "Should never get here");
2165 return 0;
2166 }
2167
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002168 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002169 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2170 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002171 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002172
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002173 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002174 // value and the MBB to hold the PHI instruction for this SetCC.
2175 MachineBasicBlock *thisMBB = BB;
2176 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2177 ilist<MachineBasicBlock>::iterator It = BB;
2178 ++It;
2179
2180 // thisMBB:
2181 // ...
2182 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002183 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002184 // bCC copy1MBB
2185 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002186 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2187 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002188 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002189 MachineFunction *F = BB->getParent();
2190 F->getBasicBlockList().insert(It, copy0MBB);
2191 F->getBasicBlockList().insert(It, sinkMBB);
2192 // Update machine-CFG edges
2193 BB->addSuccessor(copy0MBB);
2194 BB->addSuccessor(sinkMBB);
2195
2196 // copy0MBB:
2197 // %FalseValue = ...
2198 // # fallthrough to sinkMBB
2199 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002200 // Update machine-CFG edges
2201 BB->addSuccessor(sinkMBB);
2202
2203 // sinkMBB:
2204 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2205 // ...
2206 BB = sinkMBB;
2207 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2208 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002209 return Result;
2210 }
Nate Begemana9795f82005-03-24 04:41:43 +00002211
2212 case ISD::Constant:
2213 switch (N.getValueType()) {
2214 default: assert(0 && "Cannot use constants of this type!");
2215 case MVT::i1:
2216 BuildMI(BB, PPC::LI, 1, Result)
2217 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2218 break;
2219 case MVT::i32:
2220 {
2221 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2222 if (v < 32768 && v >= -32768) {
2223 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2224 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002225 Tmp1 = MakeReg(MVT::i32);
2226 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2227 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002228 }
2229 }
2230 }
2231 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002232
2233 case ISD::ConstantFP: {
2234 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2235 Result = getConstDouble(CN->getValue(), Result);
2236 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002237 }
2238
Nate Begemana3fd4002005-07-19 16:51:05 +00002239 case ISD::FNEG:
2240 if (!NoExcessFPPrecision &&
2241 ISD::ADD == N.getOperand(0).getOpcode() &&
2242 N.getOperand(0).Val->hasOneUse() &&
2243 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2244 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2245 ++FusedFP; // Statistic
2246 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2247 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2248 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2249 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2250 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2251 } else if (!NoExcessFPPrecision &&
2252 ISD::ADD == N.getOperand(0).getOpcode() &&
2253 N.getOperand(0).Val->hasOneUse() &&
2254 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2255 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2256 ++FusedFP; // Statistic
2257 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2258 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2259 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2260 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2261 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2262 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2263 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2264 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2265 } else {
2266 Tmp1 = SelectExpr(N.getOperand(0));
2267 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2268 }
2269 return Result;
2270
2271 case ISD::FABS:
2272 Tmp1 = SelectExpr(N.getOperand(0));
2273 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2274 return Result;
2275
2276 case ISD::FP_ROUND:
2277 assert (DestType == MVT::f32 &&
2278 N.getOperand(0).getValueType() == MVT::f64 &&
2279 "only f64 to f32 conversion supported here");
2280 Tmp1 = SelectExpr(N.getOperand(0));
2281 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2282 return Result;
2283
2284 case ISD::FP_EXTEND:
2285 assert (DestType == MVT::f64 &&
2286 N.getOperand(0).getValueType() == MVT::f32 &&
2287 "only f32 to f64 conversion supported here");
2288 Tmp1 = SelectExpr(N.getOperand(0));
2289 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2290 return Result;
2291
2292 case ISD::UINT_TO_FP:
2293 case ISD::SINT_TO_FP: {
2294 assert (N.getOperand(0).getValueType() == MVT::i32
2295 && "int to float must operate on i32");
2296 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2297 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2298 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2299 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2300
2301 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2302 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2303
2304 if (IsUnsigned) {
2305 unsigned ConstF = getConstDouble(0x1.000000p52);
2306 // Store the hi & low halves of the fp value, currently in int regs
2307 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2308 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2309 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2310 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2311 // Generate the return value with a subtract
2312 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2313 } else {
2314 unsigned ConstF = getConstDouble(0x1.000008p52);
2315 unsigned TmpL = MakeReg(MVT::i32);
2316 // Store the hi & low halves of the fp value, currently in int regs
2317 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2318 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2319 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2320 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2321 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2322 // Generate the return value with a subtract
2323 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2324 }
2325 return Result;
2326 }
2327 }
Nate Begemana9795f82005-03-24 04:41:43 +00002328 return 0;
2329}
2330
2331void ISel::Select(SDOperand N) {
2332 unsigned Tmp1, Tmp2, Opc;
2333 unsigned opcode = N.getOpcode();
2334
2335 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2336 return; // Already selected.
2337
2338 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002339
Nate Begemana9795f82005-03-24 04:41:43 +00002340 switch (Node->getOpcode()) {
2341 default:
2342 Node->dump(); std::cerr << "\n";
2343 assert(0 && "Node not handled yet!");
2344 case ISD::EntryToken: return; // Noop
2345 case ISD::TokenFactor:
2346 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2347 Select(Node->getOperand(i));
2348 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002349 case ISD::CALLSEQ_START:
2350 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002351 Select(N.getOperand(0));
2352 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002353 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002354 PPC::ADJCALLSTACKUP;
2355 BuildMI(BB, Opc, 1).addImm(Tmp1);
2356 return;
2357 case ISD::BR: {
2358 MachineBasicBlock *Dest =
2359 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002360 Select(N.getOperand(0));
2361 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2362 return;
2363 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002364 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002365 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002366 SelectBranchCC(N);
2367 return;
2368 case ISD::CopyToReg:
2369 Select(N.getOperand(0));
2370 Tmp1 = SelectExpr(N.getOperand(1));
2371 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002372
Nate Begemana9795f82005-03-24 04:41:43 +00002373 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002374 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002375 N.getOperand(1).getValueType() == MVT::f32)
2376 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2377 else
2378 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2379 }
2380 return;
2381 case ISD::ImplicitDef:
2382 Select(N.getOperand(0));
2383 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2384 return;
2385 case ISD::RET:
2386 switch (N.getNumOperands()) {
2387 default:
2388 assert(0 && "Unknown return instruction!");
2389 case 3:
2390 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2391 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002392 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002393 Select(N.getOperand(0));
2394 Tmp1 = SelectExpr(N.getOperand(1));
2395 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002396 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2397 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002398 break;
2399 case 2:
2400 Select(N.getOperand(0));
2401 Tmp1 = SelectExpr(N.getOperand(1));
2402 switch (N.getOperand(1).getValueType()) {
2403 default:
2404 assert(0 && "Unknown return type!");
2405 case MVT::f64:
2406 case MVT::f32:
2407 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2408 break;
2409 case MVT::i32:
2410 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2411 break;
2412 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002413 case 1:
2414 Select(N.getOperand(0));
2415 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002416 }
2417 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2418 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002419 case ISD::TRUNCSTORE:
2420 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002421 {
2422 SDOperand Chain = N.getOperand(0);
2423 SDOperand Value = N.getOperand(1);
2424 SDOperand Address = N.getOperand(2);
2425 Select(Chain);
2426
2427 Tmp1 = SelectExpr(Value); //value
2428
2429 if (opcode == ISD::STORE) {
2430 switch(Value.getValueType()) {
2431 default: assert(0 && "unknown Type in store");
2432 case MVT::i32: Opc = PPC::STW; break;
2433 case MVT::f64: Opc = PPC::STFD; break;
2434 case MVT::f32: Opc = PPC::STFS; break;
2435 }
2436 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002437 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Nate Begemana9795f82005-03-24 04:41:43 +00002438 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002439 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002440 case MVT::i8: Opc = PPC::STB; break;
2441 case MVT::i16: Opc = PPC::STH; break;
2442 }
2443 }
2444
Nate Begemana7e11a42005-04-01 05:57:17 +00002445 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002446 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002447 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2448 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002449 }
2450 else
2451 {
2452 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002453 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002454 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002455 Opc = IndexedOpForOp(Opc);
2456 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2457 } else {
2458 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2459 }
Nate Begemana9795f82005-03-24 04:41:43 +00002460 }
2461 return;
2462 }
2463 case ISD::EXTLOAD:
2464 case ISD::SEXTLOAD:
2465 case ISD::ZEXTLOAD:
2466 case ISD::LOAD:
2467 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002468 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002469 case ISD::CALL:
2470 case ISD::DYNAMIC_STACKALLOC:
2471 ExprMap.erase(N);
2472 SelectExpr(N);
2473 return;
2474 }
2475 assert(0 && "Should not be reached!");
2476}
2477
2478
2479/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2480/// into a machine code representation using pattern matching and a machine
2481/// description file.
2482///
2483FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002484 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002485}
2486