Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 11 | // Magic number generation for integer divide from the PowerPC Compiler Writer's |
| 12 | // Guide, section 3.2.3.5 |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "PowerPC.h" |
| 17 | #include "PowerPCInstrBuilder.h" |
| 18 | #include "PowerPCInstrInfo.h" |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 19 | #include "PPC32TargetMachine.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 23 | #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 Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include <set> |
| 35 | #include <algorithm> |
| 36 | using namespace llvm; |
| 37 | |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 38 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface |
| 41 | namespace { |
| 42 | class PPC32TargetLowering : public TargetLowering { |
| 43 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
| 44 | int ReturnAddrIndex; // FrameIndex for return slot. |
| 45 | public: |
| 46 | PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
Chris Lattner | 9bce0f9 | 2005-05-12 02:06:00 +0000 | [diff] [blame] | 47 | // Fold away setcc operations if possible. |
| 48 | setSetCCIsExpensive(); |
| 49 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 50 | // Set up the register classes. |
| 51 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 52 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 53 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 54 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 55 | // PowerPC has no intrinsics for these particular operations |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 56 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 57 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 58 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 59 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 60 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 61 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 62 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 63 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 64 | // PowerPC has no SREM/UREM instructions |
| 65 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 66 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Chris Lattner | 43fdea0 | 2005-04-02 05:03:24 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 68 | // We don't support sin/cos/sqrt/fmod |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 70 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 71 | setOperationAction(ISD::SREM , MVT::f64, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 72 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 73 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::SREM , MVT::f32, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 75 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 76 | // If we're enabling GP optimizations, use hardware square root |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 77 | if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) { |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 78 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 79 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 80 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 81 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 82 | //PowerPC does not have CTPOP or CTTZ |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 83 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 84 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 85 | |
Chris Lattner | cbd06fc | 2005-04-07 19:41:49 +0000 | [diff] [blame] | 86 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 87 | addLegalFPImmediate(+0.0); // Necessary for FSEL |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 88 | addLegalFPImmediate(-0.0); // |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 89 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 90 | computeRegisterProperties(); |
| 91 | } |
| 92 | |
| 93 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 94 | /// lower the arguments for the specified function, into the specified DAG. |
| 95 | virtual std::vector<SDOperand> |
| 96 | LowerArguments(Function &F, SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 97 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 98 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 99 | /// actual call. |
| 100 | virtual std::pair<SDOperand, SDOperand> |
Chris Lattner | c57f682 | 2005-05-12 19:56:45 +0000 | [diff] [blame] | 101 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 102 | bool isTailCall, SDOperand Callee, ArgListTy &Args, |
| 103 | SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 104 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 105 | virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 106 | Value *VAListV, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 107 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 108 | virtual std::pair<SDOperand,SDOperand> |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 109 | LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 110 | const Type *ArgTy, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 111 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 112 | virtual std::pair<SDOperand, SDOperand> |
| 113 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 114 | SelectionDAG &DAG); |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | std::vector<SDOperand> |
| 120 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 121 | // |
| 122 | // add beautiful description of PPC stack frame format, or at least some docs |
| 123 | // |
| 124 | MachineFunction &MF = DAG.getMachineFunction(); |
| 125 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 126 | MachineBasicBlock& BB = MF.front(); |
| 127 | std::vector<SDOperand> ArgValues; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 128 | |
| 129 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 130 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 131 | // handle tracking them for us. |
| 132 | std::vector<unsigned> argVR, argPR, argOp; |
| 133 | unsigned ArgOffset = 24; |
| 134 | unsigned GPR_remaining = 8; |
| 135 | unsigned FPR_remaining = 13; |
| 136 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 137 | static const unsigned GPR[] = { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 138 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 139 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 140 | }; |
| 141 | static const unsigned FPR[] = { |
| 142 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 143 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 144 | }; |
| 145 | |
| 146 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 147 | // the arguments start at offset 24, although they are likely to be passed |
| 148 | // in registers. |
| 149 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 150 | SDOperand newroot, argt; |
| 151 | unsigned ObjSize; |
| 152 | bool needsLoad = false; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 153 | bool ArgLive = !I->use_empty(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 154 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 155 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 156 | switch (ObjectVT) { |
| 157 | default: assert(0 && "Unhandled argument type!"); |
| 158 | case MVT::i1: |
| 159 | case MVT::i8: |
| 160 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 161 | case MVT::i32: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 162 | ObjSize = 4; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 163 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 164 | if (GPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 165 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 166 | argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 167 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 168 | if (ObjectVT != MVT::i32) |
| 169 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 170 | } else { |
| 171 | needsLoad = true; |
| 172 | } |
| 173 | break; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 174 | case MVT::i64: ObjSize = 8; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 175 | if (!ArgLive) break; |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 176 | if (GPR_remaining > 0) { |
| 177 | SDOperand argHi, argLo; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 178 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 179 | argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
| 180 | // If we have two or more remaining argument registers, then both halves |
| 181 | // of the i64 can be sourced from there. Otherwise, the lower half will |
| 182 | // have to come off the stack. This can happen when an i64 is preceded |
| 183 | // by 28 bytes of arguments. |
| 184 | if (GPR_remaining > 1) { |
| 185 | MF.addLiveIn(GPR[GPR_idx+1]); |
| 186 | argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi); |
| 187 | } else { |
| 188 | int FI = MFI->CreateFixedObject(4, ArgOffset+4); |
| 189 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 190 | argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 191 | DAG.getSrcValue(NULL)); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 192 | } |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 193 | // Build the outgoing arg thingy |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 194 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 195 | newroot = argLo; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 196 | } else { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 197 | needsLoad = true; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 198 | } |
| 199 | break; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 200 | case MVT::f32: |
| 201 | case MVT::f64: |
| 202 | ObjSize = (ObjectVT == MVT::f64) ? 8 : 4; |
| 203 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 204 | if (FPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 205 | MF.addLiveIn(FPR[FPR_idx]); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 206 | argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT, |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 207 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 208 | --FPR_remaining; |
| 209 | ++FPR_idx; |
| 210 | } else { |
| 211 | needsLoad = true; |
| 212 | } |
| 213 | break; |
| 214 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 215 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 216 | // We need to load the argument to a virtual register if we determined above |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 217 | // that we ran out of physical registers of the appropriate type |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 218 | if (needsLoad) { |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 219 | unsigned SubregOffset = 0; |
Nate Begeman | c3e2db4 | 2005-04-04 09:09:00 +0000 | [diff] [blame] | 220 | if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3; |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 221 | if (ObjectVT == MVT::i16) SubregOffset = 2; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 222 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 223 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 224 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 225 | DAG.getConstant(SubregOffset, MVT::i32)); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 226 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 227 | DAG.getSrcValue(NULL)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 228 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 229 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 230 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 231 | // argument passing. |
| 232 | if (GPR_remaining > 0) { |
| 233 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 234 | GPR_remaining -= delta; |
| 235 | GPR_idx += delta; |
| 236 | } |
| 237 | ArgOffset += ObjSize; |
Chris Lattner | 91277ea | 2005-04-09 21:23:24 +0000 | [diff] [blame] | 238 | if (newroot.Val) |
| 239 | DAG.setRoot(newroot.getValue(1)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 240 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 241 | ArgValues.push_back(argt); |
| 242 | } |
| 243 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 244 | // If the function takes variable number of arguments, make a frame index for |
| 245 | // the start of the first vararg value... for expansion of llvm.va_start. |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 246 | if (F.isVarArg()) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 247 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 248 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 249 | // If this function is vararg, store any remaining integer argument regs |
| 250 | // to their spots on the stack so that they may be loaded by deferencing the |
| 251 | // result of va_next. |
| 252 | std::vector<SDOperand> MemOps; |
| 253 | for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 254 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 255 | SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 256 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 257 | Val, FIN, DAG.getSrcValue(NULL)); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 258 | MemOps.push_back(Store); |
| 259 | // Increment the address by four for the next argument to store |
| 260 | SDOperand PtrOff = DAG.getConstant(4, getPointerTy()); |
| 261 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff); |
| 262 | } |
| 263 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps)); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 264 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 265 | |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 266 | // Finally, inform the code generator which regs we return values in. |
| 267 | switch (getValueType(F.getReturnType())) { |
| 268 | default: assert(0 && "Unknown type!"); |
| 269 | case MVT::isVoid: break; |
| 270 | case MVT::i1: |
| 271 | case MVT::i8: |
| 272 | case MVT::i16: |
| 273 | case MVT::i32: |
| 274 | MF.addLiveOut(PPC::R3); |
| 275 | break; |
| 276 | case MVT::i64: |
| 277 | MF.addLiveOut(PPC::R3); |
| 278 | MF.addLiveOut(PPC::R4); |
| 279 | break; |
| 280 | case MVT::f32: |
| 281 | case MVT::f64: |
| 282 | MF.addLiveOut(PPC::F1); |
| 283 | break; |
| 284 | } |
| 285 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 286 | return ArgValues; |
| 287 | } |
| 288 | |
| 289 | std::pair<SDOperand, SDOperand> |
| 290 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 291 | const Type *RetTy, bool isVarArg, |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 292 | unsigned CallingConv, bool isTailCall, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 293 | SDOperand Callee, ArgListTy &Args, |
| 294 | SelectionDAG &DAG) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 295 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 296 | // SelectExpr to use to put the arguments in the appropriate registers. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 297 | std::vector<SDOperand> args_to_use; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 298 | |
| 299 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 300 | // area, and parameter passing area. |
| 301 | unsigned NumBytes = 24; |
| 302 | |
| 303 | if (Args.empty()) { |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 304 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | a7e11a4 | 2005-04-01 05:57:17 +0000 | [diff] [blame] | 305 | DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 306 | } else { |
| 307 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 308 | switch (getValueType(Args[i].second)) { |
| 309 | default: assert(0 && "Unknown value type!"); |
| 310 | case MVT::i1: |
| 311 | case MVT::i8: |
| 312 | case MVT::i16: |
| 313 | case MVT::i32: |
| 314 | case MVT::f32: |
| 315 | NumBytes += 4; |
| 316 | break; |
| 317 | case MVT::i64: |
| 318 | case MVT::f64: |
| 319 | NumBytes += 8; |
| 320 | break; |
| 321 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 322 | |
| 323 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 324 | // plus 32 bytes of argument space in case any called code gets funky on us. |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 325 | // (Required by ABI to support var arg) |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 326 | if (NumBytes < 56) NumBytes = 56; |
| 327 | |
| 328 | // Adjust the stack pointer for the new arguments... |
| 329 | // These operations are automatically eliminated by the prolog/epilog pass |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 330 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 331 | DAG.getConstant(NumBytes, getPointerTy())); |
| 332 | |
| 333 | // Set up a copy of the stack pointer for use loading and storing any |
| 334 | // arguments that may not fit in the registers available for argument |
| 335 | // passing. |
| 336 | SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32, |
| 337 | DAG.getEntryNode()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 338 | |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 339 | // Figure out which arguments are going to go in registers, and which in |
| 340 | // memory. Also, if this is a vararg function, floating point operations |
| 341 | // must be stored to our stack, and loaded into integer regs as well, if |
| 342 | // any integer regs are available for argument passing. |
| 343 | unsigned ArgOffset = 24; |
| 344 | unsigned GPR_remaining = 8; |
| 345 | unsigned FPR_remaining = 13; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 346 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 347 | std::vector<SDOperand> MemOps; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 348 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 349 | // PtrOff will be used to store the current argument to the stack if a |
| 350 | // register cannot be found for it. |
| 351 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 352 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 353 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 354 | |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 355 | switch (ArgVT) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 356 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 357 | case MVT::i1: |
| 358 | case MVT::i8: |
| 359 | case MVT::i16: |
| 360 | // Promote the integer to 32 bits. If the input type is signed use a |
| 361 | // sign extend, otherwise use a zero extend. |
| 362 | if (Args[i].second->isSigned()) |
| 363 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 364 | else |
| 365 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 366 | // FALL THROUGH |
| 367 | case MVT::i32: |
| 368 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 369 | args_to_use.push_back(Args[i].first); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 370 | --GPR_remaining; |
| 371 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 372 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 373 | Args[i].first, PtrOff, |
| 374 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 375 | } |
| 376 | ArgOffset += 4; |
| 377 | break; |
| 378 | case MVT::i64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 379 | // If we have one free GPR left, we can place the upper half of the i64 |
| 380 | // in it, and store the other half to the stack. If we have two or more |
| 381 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 382 | if (GPR_remaining > 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 383 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 384 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 385 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 386 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 387 | args_to_use.push_back(Hi); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 388 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 389 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 390 | args_to_use.push_back(Lo); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 391 | --GPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 392 | } else { |
| 393 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 394 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 395 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 396 | Lo, PtrOff, DAG.getSrcValue(NULL))); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 397 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 398 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 399 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 400 | Args[i].first, PtrOff, |
| 401 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 402 | } |
| 403 | ArgOffset += 8; |
| 404 | break; |
| 405 | case MVT::f32: |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 406 | case MVT::f64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 407 | if (FPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 408 | args_to_use.push_back(Args[i].first); |
| 409 | --FPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 410 | if (isVarArg) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 411 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 412 | Args[i].first, PtrOff, |
| 413 | DAG.getSrcValue(NULL)); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 414 | MemOps.push_back(Store); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 415 | // Float varargs are always shadowed in available integer registers |
| 416 | if (GPR_remaining > 0) { |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 417 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 418 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 419 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 420 | args_to_use.push_back(Load); |
| 421 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 422 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 423 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 424 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 425 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 426 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 427 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 428 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 429 | args_to_use.push_back(Load); |
| 430 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 431 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 432 | } else { |
| 433 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 434 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 435 | // GPRs. |
| 436 | if (GPR_remaining > 0) { |
| 437 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 438 | --GPR_remaining; |
| 439 | } |
| 440 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 441 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 442 | --GPR_remaining; |
| 443 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 444 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 445 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 446 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 447 | Args[i].first, PtrOff, |
| 448 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 449 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 450 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 453 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 454 | if (!MemOps.empty()) |
| 455 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 456 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 457 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 458 | std::vector<MVT::ValueType> RetVals; |
| 459 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 460 | if (RetTyVT != MVT::isVoid) |
| 461 | RetVals.push_back(RetTyVT); |
| 462 | RetVals.push_back(MVT::Other); |
| 463 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 464 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 465 | Chain, Callee, args_to_use), 0); |
| 466 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 467 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 468 | DAG.getConstant(NumBytes, getPointerTy())); |
| 469 | return std::make_pair(TheCall, Chain); |
| 470 | } |
| 471 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 472 | SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 473 | Value *VAListV, SelectionDAG &DAG) { |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 474 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 475 | // memory location argument. |
| 476 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 477 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 478 | DAG.getSrcValue(VAListV)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 481 | std::pair<SDOperand,SDOperand> |
| 482 | PPC32TargetLowering::LowerVAArg(SDOperand Chain, |
| 483 | SDOperand VAListP, Value *VAListV, |
| 484 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 485 | MVT::ValueType ArgVT = getValueType(ArgTy); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 486 | |
| 487 | SDOperand VAList = |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 488 | DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV)); |
| 489 | SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL)); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 490 | unsigned Amt; |
| 491 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 492 | Amt = 4; |
| 493 | else { |
| 494 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 495 | "Other types should have been promoted for varargs!"); |
| 496 | Amt = 8; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 498 | VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 499 | DAG.getConstant(Amt, VAList.getValueType())); |
| 500 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 501 | VAList, VAListP, DAG.getSrcValue(VAListV)); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 502 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 503 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 504 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 505 | |
| 506 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 507 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 508 | SelectionDAG &DAG) { |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 509 | assert(0 && "LowerFrameReturnAddress unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 510 | abort(); |
| 511 | } |
| 512 | |
| 513 | namespace { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 514 | Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted"); |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 515 | Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations"); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 516 | Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 517 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 518 | //===--------------------------------------------------------------------===// |
| 519 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 520 | /// SelectionDAG operations. |
| 521 | //===--------------------------------------------------------------------===// |
| 522 | class ISel : public SelectionDAGISel { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 523 | PPC32TargetLowering PPC32Lowering; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 524 | SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform |
| 525 | // for sdiv and udiv until it is put into the future |
| 526 | // dag combiner. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 527 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 528 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 529 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 530 | /// tree. |
| 531 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 532 | |
| 533 | unsigned GlobalBaseReg; |
| 534 | bool GlobalBaseInitialized; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 535 | bool RecordSuccess; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 536 | public: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 537 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM), |
| 538 | ISelDAG(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 539 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 540 | /// runOnFunction - Override this function in order to reset our per-function |
| 541 | /// variables. |
| 542 | virtual bool runOnFunction(Function &Fn) { |
| 543 | // Make sure we re-emit a set of the global base reg if necessary |
| 544 | GlobalBaseInitialized = false; |
| 545 | return SelectionDAGISel::runOnFunction(Fn); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 548 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 549 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 550 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 551 | DEBUG(BB->dump()); |
| 552 | // Codegen the basic block. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 553 | ISelDAG = &DAG; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 554 | Select(DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 555 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 556 | // Clear state used for selection. |
| 557 | ExprMap.clear(); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 558 | ISelDAG = 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 559 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 560 | |
| 561 | // dag -> dag expanders for integer divide by constant |
| 562 | SDOperand BuildSDIVSequence(SDOperand N); |
| 563 | SDOperand BuildUDIVSequence(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 564 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 565 | unsigned getGlobalBaseReg(); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 566 | unsigned getConstDouble(double floatVal, unsigned Result); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 567 | void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 568 | bool SelectBitfieldInsert(SDOperand OR, unsigned Result); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 569 | unsigned FoldIfWideZeroExtend(SDOperand N); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 570 | unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx); |
| 571 | unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx); |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 572 | bool SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 573 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 574 | bool IsArithmetic = false, bool Negate = false); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 575 | unsigned SelectExpr(SDOperand N, bool Recording=false); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 576 | void Select(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 577 | |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 578 | unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 579 | void SelectBranchCC(SDOperand N); |
Chris Lattner | 3f27013 | 2005-08-02 19:07:49 +0000 | [diff] [blame] | 580 | |
| 581 | virtual const char *getPassName() const { |
| 582 | return "PowerPC Pattern Instruction Selection"; |
| 583 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 586 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 587 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 588 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 589 | // not, since all 1s are not contiguous. |
| 590 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 591 | if (isShiftedMask_32(Val)) { |
| 592 | // look for the first non-zero bit |
| 593 | MB = CountLeadingZeros_32(Val); |
| 594 | // look for the first zero bit after the run of ones |
| 595 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 596 | return true; |
| 597 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 598 | // effectively look for the first zero bit |
| 599 | ME = CountLeadingZeros_32(Val) - 1; |
| 600 | // effectively look for the first one bit after the run of zeros |
| 601 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 602 | return true; |
| 603 | } |
| 604 | // no run present |
| 605 | return false; |
| 606 | } |
| 607 | |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 608 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 609 | // and mask opcode and mask operation. |
| 610 | static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask, |
| 611 | bool IsShiftMask, |
| 612 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 613 | if (Shift > 31) return false; |
| 614 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 615 | |
| 616 | if (Opcode == ISD::SHL) { // shift left |
| 617 | // apply shift to mask if it comes first |
| 618 | if (IsShiftMask) Mask = Mask << Shift; |
| 619 | // determine which bits are made indeterminant by shift |
| 620 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 621 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights |
| 622 | // apply shift to mask if it comes first |
| 623 | if (IsShiftMask) Mask = Mask >> Shift; |
| 624 | // determine which bits are made indeterminant by shift |
| 625 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 626 | // adjust for the left rotate |
| 627 | Shift = 32 - Shift; |
| 628 | } |
| 629 | |
| 630 | // if the mask doesn't intersect any Indeterminant bits |
| 631 | if (!(Mask & Indeterminant)) { |
| 632 | SH = Shift; |
| 633 | // make sure the mask is still a mask (wrap arounds may not be) |
| 634 | return isRunOfOnes(Mask, MB, ME); |
| 635 | } |
| 636 | |
| 637 | // can't do it |
| 638 | return false; |
| 639 | } |
| 640 | |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 641 | // isIntImmediate - This method tests to see if a constant operand. |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 642 | // If so Imm will receive the 32 bit value. |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 643 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 644 | // test for constant |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 645 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 646 | // retrieve value |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 647 | Imm = (unsigned)CN->getSignExtended(); |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 648 | // passes muster |
| 649 | return true; |
| 650 | } |
| 651 | // not a constant |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 656 | // a immediate shift count less than 32. |
| 657 | static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) { |
| 658 | Opc = N.getOpcode(); |
| 659 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 660 | isIntImmediate(N.getOperand(1), SH) && SH < 32; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 664 | static bool isOprNot(SDOperand N) { |
| 665 | unsigned Imm; |
| 666 | return N.getOpcode() == ISD::XOR && |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 667 | isIntImmediate(N.getOperand(1), Imm) && (signed)Imm == -1; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // Immediate constant composers. |
| 671 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 672 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 673 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 674 | // arithmethically. |
| 675 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 676 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 677 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 678 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 679 | /// NodeHasRecordingVariant - If SelectExpr can always produce code for |
| 680 | /// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise, |
| 681 | /// return false. |
| 682 | static bool NodeHasRecordingVariant(unsigned NodeOpcode) { |
| 683 | switch(NodeOpcode) { |
| 684 | default: return false; |
| 685 | case ISD::AND: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 686 | case ISD::OR: |
Chris Lattner | 519f40b | 2005-04-13 02:46:17 +0000 | [diff] [blame] | 687 | return true; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 691 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 692 | /// to Condition. If the Condition is unordered or unsigned, the bool argument |
| 693 | /// U is set to true, otherwise it is set to false. |
| 694 | static unsigned getBCCForSetCC(unsigned Condition, bool& U) { |
| 695 | U = false; |
| 696 | switch (Condition) { |
| 697 | default: assert(0 && "Unknown condition!"); abort(); |
| 698 | case ISD::SETEQ: return PPC::BEQ; |
| 699 | case ISD::SETNE: return PPC::BNE; |
| 700 | case ISD::SETULT: U = true; |
| 701 | case ISD::SETLT: return PPC::BLT; |
| 702 | case ISD::SETULE: U = true; |
| 703 | case ISD::SETLE: return PPC::BLE; |
| 704 | case ISD::SETUGT: U = true; |
| 705 | case ISD::SETGT: return PPC::BGT; |
| 706 | case ISD::SETUGE: U = true; |
| 707 | case ISD::SETGE: return PPC::BGE; |
| 708 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 712 | /// getCROpForOp - Return the condition register opcode (or inverted opcode) |
| 713 | /// associated with the SelectionDAG opcode. |
| 714 | static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) { |
| 715 | switch (Opcode) { |
| 716 | default: assert(0 && "Unknown opcode!"); abort(); |
| 717 | case ISD::AND: |
| 718 | if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law |
| 719 | if (!Inv1 && !Inv2) return PPC::CRAND; |
| 720 | if (Inv1 ^ Inv2) return PPC::CRANDC; |
| 721 | case ISD::OR: |
| 722 | if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law |
| 723 | if (!Inv1 && !Inv2) return PPC::CROR; |
| 724 | if (Inv1 ^ Inv2) return PPC::CRORC; |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 730 | /// associated with the SetCC condition, and whether or not the field is |
| 731 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
| 732 | static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) { |
| 733 | switch (Condition) { |
| 734 | default: assert(0 && "Unknown condition!"); abort(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 735 | case ISD::SETULT: |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 736 | case ISD::SETLT: Inv = false; return 0; |
| 737 | case ISD::SETUGE: |
| 738 | case ISD::SETGE: Inv = true; return 0; |
| 739 | case ISD::SETUGT: |
| 740 | case ISD::SETGT: Inv = false; return 1; |
| 741 | case ISD::SETULE: |
| 742 | case ISD::SETLE: Inv = true; return 1; |
| 743 | case ISD::SETEQ: Inv = false; return 2; |
| 744 | case ISD::SETNE: Inv = true; return 2; |
| 745 | } |
| 746 | return 0; |
| 747 | } |
| 748 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 749 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 750 | /// and store immediate instructions. |
| 751 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 752 | switch(Opcode) { |
| 753 | default: assert(0 && "Unknown opcode!"); abort(); |
| 754 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 755 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 756 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 757 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 758 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 759 | case PPC::LFD: return PPC::LFDX; |
| 760 | } |
| 761 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 762 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 763 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 764 | // Structure used to return the necessary information to codegen an SDIV as |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 765 | // a multiply. |
| 766 | struct ms { |
| 767 | int m; // magic number |
| 768 | int s; // shift amount |
| 769 | }; |
| 770 | |
| 771 | struct mu { |
| 772 | unsigned int m; // magic number |
| 773 | int a; // add indicator |
| 774 | int s; // shift amount |
| 775 | }; |
| 776 | |
| 777 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 778 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 779 | /// or -1. |
| 780 | static struct ms magic(int d) { |
| 781 | int p; |
| 782 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 783 | const unsigned int two31 = 0x80000000U; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 784 | struct ms mag; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 785 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 786 | ad = abs(d); |
| 787 | t = two31 + ((unsigned int)d >> 31); |
| 788 | anc = t - 1 - t%ad; // absolute value of nc |
| 789 | p = 31; // initialize p |
| 790 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 791 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 792 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 793 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 794 | do { |
| 795 | p = p + 1; |
| 796 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 797 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 798 | if (r1 >= anc) { // must be unsigned comparison |
| 799 | q1 = q1 + 1; |
| 800 | r1 = r1 - anc; |
| 801 | } |
| 802 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 803 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 804 | if (r2 >= ad) { // must be unsigned comparison |
| 805 | q2 = q2 + 1; |
| 806 | r2 = r2 - ad; |
| 807 | } |
| 808 | delta = ad - r2; |
| 809 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 810 | |
| 811 | mag.m = q2 + 1; |
| 812 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 813 | mag.s = p - 32; // resulting shift |
| 814 | return mag; |
| 815 | } |
| 816 | |
| 817 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 818 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 819 | static struct mu magicu(unsigned d) |
| 820 | { |
| 821 | int p; |
| 822 | unsigned int nc, delta, q1, r1, q2, r2; |
| 823 | struct mu magu; |
| 824 | magu.a = 0; // initialize "add" indicator |
| 825 | nc = - 1 - (-d)%d; |
| 826 | p = 31; // initialize p |
| 827 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 828 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 829 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 830 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 831 | do { |
| 832 | p = p + 1; |
| 833 | if (r1 >= nc - r1 ) { |
| 834 | q1 = 2*q1 + 1; // update q1 |
| 835 | r1 = 2*r1 - nc; // update r1 |
| 836 | } |
| 837 | else { |
| 838 | q1 = 2*q1; // update q1 |
| 839 | r1 = 2*r1; // update r1 |
| 840 | } |
| 841 | if (r2 + 1 >= d - r2) { |
| 842 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 843 | q2 = 2*q2 + 1; // update q2 |
| 844 | r2 = 2*r2 + 1 - d; // update r2 |
| 845 | } |
| 846 | else { |
| 847 | if (q2 >= 0x80000000) magu.a = 1; |
| 848 | q2 = 2*q2; // update q2 |
| 849 | r2 = 2*r2 + 1; // update r2 |
| 850 | } |
| 851 | delta = d - 1 - r2; |
| 852 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 853 | magu.m = q2 + 1; // resulting magic number |
| 854 | magu.s = p - 32; // resulting shift |
| 855 | return magu; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 860 | /// return a DAG expression to select that will generate the same value by |
| 861 | /// multiplying by a magic number. See: |
| 862 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 863 | SDOperand ISel::BuildSDIVSequence(SDOperand N) { |
| 864 | int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 865 | ms magics = magic(d); |
| 866 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 867 | SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 868 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 869 | // If d > 0 and m < 0, add the numerator |
| 870 | if (d > 0 && magics.m < 0) |
| 871 | Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0)); |
| 872 | // If d < 0 and m > 0, subtract the numerator. |
| 873 | if (d < 0 && magics.m > 0) |
| 874 | Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0)); |
| 875 | // Shift right algebraic if shift value is nonzero |
| 876 | if (magics.s > 0) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 877 | Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 878 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 879 | // Extract the sign bit and add it to the quotient |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 880 | SDOperand T = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 881 | ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32)); |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 882 | return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 886 | /// return a DAG expression to select that will generate the same value by |
| 887 | /// multiplying by a magic number. See: |
| 888 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 889 | SDOperand ISel::BuildUDIVSequence(SDOperand N) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 890 | unsigned d = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 891 | (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 892 | mu magics = magicu(d); |
| 893 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 894 | SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 895 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 896 | if (magics.a == 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 897 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 898 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 899 | } else { |
| 900 | SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 901 | NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 902 | ISelDAG->getConstant(1, MVT::i32)); |
| 903 | NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 904 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 905 | ISelDAG->getConstant(magics.s-1, MVT::i32)); |
| 906 | } |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 907 | return Q; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 910 | /// getGlobalBaseReg - Output the instructions required to put the |
| 911 | /// base address to use for accessing globals into a register. |
| 912 | /// |
| 913 | unsigned ISel::getGlobalBaseReg() { |
| 914 | if (!GlobalBaseInitialized) { |
| 915 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 916 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 917 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 918 | GlobalBaseReg = MakeReg(MVT::i32); |
| 919 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 920 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 921 | GlobalBaseInitialized = true; |
| 922 | } |
| 923 | return GlobalBaseReg; |
| 924 | } |
| 925 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 926 | /// getConstDouble - Loads a floating point value into a register, via the |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 927 | /// Constant Pool. Optionally takes a register in which to load the value. |
| 928 | unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) { |
| 929 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 930 | if (0 == Result) Result = MakeReg(MVT::f64); |
| 931 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 932 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal); |
| 933 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 934 | if (PICEnabled) |
| 935 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 936 | .addConstantPoolIndex(CPI); |
| 937 | else |
| 938 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 939 | BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
| 940 | return Result; |
| 941 | } |
| 942 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 943 | /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 944 | /// Inv is true, then invert the result. |
| 945 | void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){ |
| 946 | unsigned IntCR = MakeReg(MVT::i32); |
| 947 | BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 948 | bool GPOpt = |
| 949 | TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor(); |
| 950 | BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 951 | if (Inv) { |
| 952 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 953 | BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx)) |
| 954 | .addImm(31).addImm(31); |
| 955 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1); |
| 956 | } else { |
| 957 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx)) |
| 958 | .addImm(31).addImm(31); |
| 959 | } |
| 960 | } |
| 961 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 962 | /// SelectBitfieldInsert - turn an or of two masked values into |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 963 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 964 | /// Returns true on success, false if the caller still needs to select OR. |
| 965 | /// |
| 966 | /// Patterns matched: |
| 967 | /// 1. or shl, and 5. or and, and |
| 968 | /// 2. or and, shl 6. or shl, shr |
| 969 | /// 3. or shr, and 7. or shr, shl |
| 970 | /// 4. or and, shr |
| 971 | bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 972 | bool IsRotate = false; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 973 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 974 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 975 | SDOperand Op0 = OR.getOperand(0); |
| 976 | SDOperand Op1 = OR.getOperand(1); |
| 977 | |
| 978 | unsigned Op0Opc = Op0.getOpcode(); |
| 979 | unsigned Op1Opc = Op1.getOpcode(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 980 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 981 | // Verify that we have the correct opcodes |
| 982 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 983 | return false; |
| 984 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 985 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 986 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 987 | // Generate Mask value for Target |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 988 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 989 | dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 990 | switch(Op0Opc) { |
| 991 | case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break; |
| 992 | case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break; |
| 993 | case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break; |
| 994 | } |
| 995 | } else { |
| 996 | return false; |
| 997 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 998 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 999 | // Generate Mask value for Insert |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1000 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1001 | dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1002 | switch(Op1Opc) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1003 | case ISD::SHL: |
| 1004 | Amount = CN->getValue(); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1005 | InsMask <<= Amount; |
| 1006 | if (Op0Opc == ISD::SRL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1007 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1008 | case ISD::SRL: |
| 1009 | Amount = CN->getValue(); |
| 1010 | InsMask >>= Amount; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1011 | Amount = 32-Amount; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1012 | if (Op0Opc == ISD::SHL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1013 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1014 | case ISD::AND: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1015 | InsMask &= (unsigned)CN->getValue(); |
| 1016 | break; |
| 1017 | } |
| 1018 | } else { |
| 1019 | return false; |
| 1020 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1021 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1022 | unsigned Tmp3 = 0; |
| 1023 | |
| 1024 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 1025 | // constant as its input, make that the inserted value so that we can combine |
| 1026 | // the shift into the rotate part of the rlwimi instruction |
| 1027 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1028 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1029 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1030 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1031 | dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1032 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1033 | CN->getValue() : 32 - CN->getValue(); |
| 1034 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1035 | } |
| 1036 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 1037 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1038 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1039 | dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) { |
| 1040 | std::swap(Op0, Op1); |
| 1041 | std::swap(TgtMask, InsMask); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1042 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1043 | CN->getValue() : 32 - CN->getValue(); |
| 1044 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1049 | // Verify that the Target mask and Insert mask together form a full word mask |
| 1050 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 1051 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 1052 | // instruction. |
| 1053 | unsigned MB, ME; |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 1054 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1055 | unsigned Tmp1, Tmp2; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1056 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1057 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 1058 | // where both bitfield halves are sourced from the same value. |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1059 | if (IsRotate && fullMask && |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1060 | OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1061 | Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0)); |
| 1062 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount) |
| 1063 | .addImm(0).addImm(31); |
| 1064 | return true; |
| 1065 | } |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1066 | if (Op0Opc == ISD::AND && fullMask) |
| 1067 | Tmp1 = SelectExpr(Op0.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1068 | else |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1069 | Tmp1 = SelectExpr(Op0); |
| 1070 | Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1071 | BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2) |
| 1072 | .addImm(Amount).addImm(MB).addImm(ME); |
| 1073 | return true; |
| 1074 | } |
| 1075 | return false; |
| 1076 | } |
| 1077 | |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1078 | /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the |
| 1079 | /// low six bits. If the shift amount is an ISD::AND node with a mask that is |
| 1080 | /// wider than the implicit mask, then we can get rid of the AND and let the |
| 1081 | /// shift do the mask. |
| 1082 | unsigned ISel::FoldIfWideZeroExtend(SDOperand N) { |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 1083 | unsigned C, MB, ME; |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1084 | if (N.getOpcode() == ISD::AND && |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1085 | isIntImmediate(N.getOperand(1), C) && isRunOfOnes(C, MB, ME) && |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 1086 | MB <= 26 && ME == 31) |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1087 | return SelectExpr(N.getOperand(0)); |
| 1088 | else |
| 1089 | return SelectExpr(N); |
| 1090 | } |
| 1091 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1092 | unsigned ISel::SelectCC(SDOperand Cond, unsigned& Opc, bool &Inv, unsigned& Idx) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1093 | unsigned Result, Tmp1, Tmp2; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1094 | bool AlreadySelected = false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1095 | static const unsigned CompareOpcodes[] = |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1096 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1097 | |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1098 | // Allocate a condition register for this expression |
| 1099 | Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1100 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1101 | // If the first operand to the select is a SETCC node, then we can fold it |
| 1102 | // into the branch that selects which value to return. |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1103 | if (Cond.getOpcode() == ISD::SETCC) { |
| 1104 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1105 | bool U; |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1106 | Opc = getBCCForSetCC(CC, U); |
| 1107 | Idx = getCRIdxForSetCC(CC, Inv); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 1109 | // Use U to determine whether the SETCC immediate range is signed or not. |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1110 | if (isIntImmediate(Cond.getOperand(1), Tmp2) && |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 1111 | ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) { |
| 1112 | Tmp2 = Lo16(Tmp2); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1113 | // For comparisons against zero, we can implicity set CR0 if a recording |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1114 | // variant (e.g. 'or.' instead of 'or') of the instruction that defines |
| 1115 | // operand zero of the SetCC node is available. |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1116 | if (Tmp2 == 0 && |
| 1117 | NodeHasRecordingVariant(Cond.getOperand(0).getOpcode()) && |
| 1118 | Cond.getOperand(0).Val->hasOneUse()) { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1119 | RecordSuccess = false; |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1120 | Tmp1 = SelectExpr(Cond.getOperand(0), true); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1121 | if (RecordSuccess) { |
| 1122 | ++Recorded; |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 1123 | BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0); |
| 1124 | return Result; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1125 | } |
| 1126 | AlreadySelected = true; |
| 1127 | } |
| 1128 | // If we could not implicitly set CR0, then emit a compare immediate |
| 1129 | // instead. |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1130 | if (!AlreadySelected) Tmp1 = SelectExpr(Cond.getOperand(0)); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1131 | if (U) |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1132 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1133 | else |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1134 | BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1135 | } else { |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1136 | bool IsInteger = MVT::isInteger(Cond.getOperand(0).getValueType()); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1137 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1138 | Tmp1 = SelectExpr(Cond.getOperand(0)); |
| 1139 | Tmp2 = SelectExpr(Cond.getOperand(1)); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1140 | BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } else { |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1143 | // If this isn't a SetCC, then select the value and compare it against zero, |
| 1144 | // treating it as if it were a boolean. |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1145 | Opc = PPC::BNE; |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1146 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1147 | Tmp1 = SelectExpr(Cond); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1148 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1149 | } |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1150 | return Result; |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1153 | unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1154 | unsigned &Idx) { |
| 1155 | bool Inv0, Inv1; |
| 1156 | unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2; |
| 1157 | |
| 1158 | // Allocate a condition register for this expression |
| 1159 | unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
| 1160 | |
| 1161 | // Check for the operations we support: |
| 1162 | switch(N.getOpcode()) { |
| 1163 | default: |
| 1164 | Opc = PPC::BNE; |
| 1165 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
| 1166 | Tmp1 = SelectExpr(N); |
| 1167 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
| 1168 | break; |
| 1169 | case ISD::OR: |
| 1170 | case ISD::AND: |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1171 | Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0); |
| 1172 | Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1); |
| 1173 | CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1); |
| 1174 | if (Inv0 && !Inv1) { |
| 1175 | std::swap(Tmp1, Tmp2); |
| 1176 | std::swap(Idx0, Idx1); |
| 1177 | Opc = Opc1; |
| 1178 | } |
| 1179 | if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
| 1180 | BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0) |
| 1181 | .addReg(Tmp2).addImm(Idx1); |
| 1182 | Inv = false; |
| 1183 | Idx = Idx0; |
| 1184 | break; |
| 1185 | case ISD::SETCC: |
| 1186 | Tmp1 = SelectCC(N, Opc, Inv, Idx); |
| 1187 | Result = Tmp1; |
| 1188 | break; |
| 1189 | } |
| 1190 | return Result; |
| 1191 | } |
| 1192 | |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 1193 | /// Check to see if the load is a constant offset from a base register. |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1194 | unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1195 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 1196 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1197 | if (N.getOpcode() == ISD::ADD) { |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1198 | bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex; |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1199 | if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) { |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 1200 | offset = Lo16(imm); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1201 | if (isFrame) { |
| 1202 | ++FrameOff; |
| 1203 | Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex(); |
| 1204 | return 1; |
| 1205 | } else { |
| 1206 | Reg = SelectExpr(N.getOperand(0)); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | } else { |
| 1210 | Reg = SelectExpr(N.getOperand(0)); |
| 1211 | offset = SelectExpr(N.getOperand(1)); |
| 1212 | return 2; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1213 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1214 | } |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 1215 | // Now check if we're dealing with a global, and whether or not we should emit |
| 1216 | // an optimized load or store for statics. |
| 1217 | if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) { |
| 1218 | GlobalValue *GV = GN->getGlobal(); |
| 1219 | if (!GV->hasWeakLinkage() && !GV->isExternal()) { |
| 1220 | unsigned GlobalHi = MakeReg(MVT::i32); |
| 1221 | if (PICEnabled) |
| 1222 | BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg()) |
| 1223 | .addGlobalAddress(GV); |
| 1224 | else |
| 1225 | BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV); |
| 1226 | Reg = GlobalHi; |
| 1227 | offset = 0; |
| 1228 | return 3; |
| 1229 | } |
| 1230 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1231 | Reg = SelectExpr(N); |
| 1232 | offset = 0; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1233 | return 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | void ISel::SelectBranchCC(SDOperand N) |
| 1237 | { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1238 | MachineBasicBlock *Dest = |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1239 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 1240 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1241 | bool Inv; |
| 1242 | unsigned Opc, CCReg, Idx; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1243 | Select(N.getOperand(0)); //chain |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1244 | CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1245 | |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1246 | // Iterate to the next basic block |
| 1247 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1248 | ++It; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1249 | |
| 1250 | // If this is a two way branch, then grab the fallthrough basic block argument |
| 1251 | // and build a PowerPC branch pseudo-op, suitable for long branch conversion |
| 1252 | // if necessary by the branch selection pass. Otherwise, emit a standard |
| 1253 | // conditional branch. |
| 1254 | if (N.getOpcode() == ISD::BRCONDTWOWAY) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1255 | MachineBasicBlock *Fallthrough = |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1256 | cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock(); |
| 1257 | if (Dest != It) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1258 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1259 | .addMBB(Dest).addMBB(Fallthrough); |
| 1260 | if (Fallthrough != It) |
| 1261 | BuildMI(BB, PPC::B, 1).addMBB(Fallthrough); |
| 1262 | } else { |
| 1263 | if (Fallthrough != It) { |
| 1264 | Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1265 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1266 | .addMBB(Fallthrough).addMBB(Dest); |
| 1267 | } |
| 1268 | } |
| 1269 | } else { |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1270 | // If the fallthrough path is off the end of the function, which would be |
| 1271 | // undefined behavior, set it to be the same as the current block because |
| 1272 | // we have nothing better to set it to, and leaving it alone will cause the |
| 1273 | // PowerPC Branch Selection pass to crash. |
| 1274 | if (It == BB->getParent()->end()) It = Dest; |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1275 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 1276 | .addMBB(Dest).addMBB(It); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1277 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
| 1280 | |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1281 | // SelectIntImmediateExpr - Choose code for opcodes with immediate value. |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1282 | bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1283 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1284 | bool IsArithmetic, bool Negate) { |
| 1285 | // check constant |
| 1286 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 1287 | // exit if not a constant |
| 1288 | if (!CN) return false; |
| 1289 | // extract immediate |
| 1290 | unsigned C = (unsigned)CN->getSignExtended(); |
| 1291 | // negate if required (ISD::SUB) |
| 1292 | if (Negate) C = -C; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1293 | // get the hi and lo portions of constant |
| 1294 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 1295 | unsigned Lo = Lo16(C); |
| 1296 | // assume no intermediate result from lo instruction (same as final result) |
| 1297 | unsigned Tmp = Result; |
| 1298 | // check if two instructions are needed |
| 1299 | if (Hi && Lo) { |
| 1300 | // exit if usage indicates it would be better to load immediate into a |
| 1301 | // register |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1302 | if (CN->use_size() > 2) return false; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1303 | // need intermediate result for two instructions |
| 1304 | Tmp = MakeReg(MVT::i32); |
| 1305 | } |
| 1306 | // get first operand |
| 1307 | unsigned Opr0 = SelectExpr(N.getOperand(0)); |
| 1308 | // is a lo instruction needed |
| 1309 | if (Lo) { |
| 1310 | // generate instruction for hi portion |
| 1311 | const MachineInstrBuilder &MIBLo = BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0); |
| 1312 | if (IsArithmetic) MIBLo.addSImm(Lo); else MIBLo.addImm(Lo); |
| 1313 | // need to switch out first operand for hi instruction |
| 1314 | Opr0 = Tmp; |
| 1315 | } |
| 1316 | // is a ho instruction needed |
| 1317 | if (Hi) { |
| 1318 | // generate instruction for hi portion |
| 1319 | const MachineInstrBuilder &MIBHi = BuildMI(BB, OCHi, 2, Result).addReg(Opr0); |
| 1320 | if (IsArithmetic) MIBHi.addSImm(Hi); else MIBHi.addImm(Hi); |
| 1321 | } |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1325 | unsigned ISel::SelectExpr(SDOperand N, bool Recording) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1326 | unsigned Result; |
| 1327 | unsigned Tmp1, Tmp2, Tmp3; |
| 1328 | unsigned Opc = 0; |
| 1329 | unsigned opcode = N.getOpcode(); |
| 1330 | |
| 1331 | SDNode *Node = N.Val; |
| 1332 | MVT::ValueType DestType = N.getValueType(); |
| 1333 | |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 1334 | if (Node->getOpcode() == ISD::CopyFromReg && |
Chris Lattner | 988b1dd | 2005-07-28 05:23:43 +0000 | [diff] [blame] | 1335 | (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) || |
| 1336 | cast<RegSDNode>(Node)->getReg() == PPC::R1)) |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 1337 | // Just use the specified register as our input. |
| 1338 | return cast<RegSDNode>(Node)->getReg(); |
| 1339 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1340 | unsigned &Reg = ExprMap[N]; |
| 1341 | if (Reg) return Reg; |
| 1342 | |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1343 | switch (N.getOpcode()) { |
| 1344 | default: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1345 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1346 | MakeReg(N.getValueType()) : 1; |
| 1347 | break; |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1348 | case ISD::TAILCALL: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1349 | case ISD::CALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1350 | // If this is a call instruction, make sure to prepare ALL of the result |
| 1351 | // values as well as the chain. |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1352 | if (Node->getNumValues() == 1) |
| 1353 | Reg = Result = 1; // Void call, just a chain. |
| 1354 | else { |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1355 | Result = MakeReg(Node->getValueType(0)); |
| 1356 | ExprMap[N.getValue(0)] = Result; |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1357 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1358 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1359 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1360 | } |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | case ISD::ADD_PARTS: |
| 1363 | case ISD::SUB_PARTS: |
| 1364 | case ISD::SHL_PARTS: |
| 1365 | case ISD::SRL_PARTS: |
| 1366 | case ISD::SRA_PARTS: |
| 1367 | Result = MakeReg(Node->getValueType(0)); |
| 1368 | ExprMap[N.getValue(0)] = Result; |
| 1369 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 1370 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 1371 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1374 | switch (opcode) { |
| 1375 | default: |
| 1376 | Node->dump(); |
| 1377 | assert(0 && "Node not handled!\n"); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1378 | case ISD::UNDEF: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1379 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result); |
| 1380 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1381 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1382 | // Generate both result values. FIXME: Need a better commment here? |
| 1383 | if (Result != 1) |
| 1384 | ExprMap[N.getValue(1)] = 1; |
| 1385 | else |
| 1386 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1387 | |
| 1388 | // FIXME: We are currently ignoring the requested alignment for handling |
| 1389 | // greater than the stack alignment. This will need to be revisited at some |
| 1390 | // point. Align = N.getOperand(2); |
| 1391 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 1392 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 1393 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 1394 | << " the stack alignment yet!"; |
| 1395 | abort(); |
| 1396 | } |
| 1397 | Select(N.getOperand(0)); |
| 1398 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1399 | // Subtract size from stack pointer, thereby allocating some space. |
| 1400 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 1401 | // Put a pointer to the space into the result register by copying the SP |
| 1402 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 1403 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1404 | |
| 1405 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1406 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 1407 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1408 | if (PICEnabled) |
| 1409 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 1410 | .addConstantPoolIndex(Tmp1); |
| 1411 | else |
| 1412 | BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1413 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 1414 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1415 | |
| 1416 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1417 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1418 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1419 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1420 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1421 | case ISD::GlobalAddress: { |
| 1422 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1423 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1424 | if (PICEnabled) |
| 1425 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1426 | .addGlobalAddress(GV); |
| 1427 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 1428 | BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1429 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 1430 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 1431 | } else { |
| 1432 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 1433 | } |
| 1434 | return Result; |
| 1435 | } |
| 1436 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1437 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1438 | case ISD::EXTLOAD: |
| 1439 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1440 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1441 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1442 | Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1443 | bool sext = (ISD::SEXTLOAD == opcode); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1444 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1445 | // Make sure we generate both values. |
| 1446 | if (Result != 1) |
| 1447 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 1448 | else |
| 1449 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1450 | |
| 1451 | SDOperand Chain = N.getOperand(0); |
| 1452 | SDOperand Address = N.getOperand(1); |
| 1453 | Select(Chain); |
| 1454 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1455 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1456 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1457 | case MVT::i1: Opc = PPC::LBZ; break; |
| 1458 | case MVT::i8: Opc = PPC::LBZ; break; |
| 1459 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 1460 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1461 | case MVT::f32: Opc = PPC::LFS; break; |
| 1462 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1463 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1464 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1465 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
| 1466 | Tmp1 = MakeReg(MVT::i32); |
| 1467 | int CPI = CP->getIndex(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1468 | if (PICEnabled) |
| 1469 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1470 | .addConstantPoolIndex(CPI); |
| 1471 | else |
| 1472 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1473 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1474 | } else if (Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1475 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1476 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1477 | } else { |
| 1478 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1479 | switch(SelectAddr(Address, Tmp1, offset)) { |
| 1480 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 1481 | case 0: // imm offset, no frame, no index |
| 1482 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 1483 | break; |
| 1484 | case 1: // imm offset + frame index |
| 1485 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset); |
| 1486 | break; |
| 1487 | case 2: // base+index addressing |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1488 | Opc = IndexedOpForOp(Opc); |
| 1489 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1490 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 1491 | case 3: { |
| 1492 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 1493 | GlobalValue *GV = GN->getGlobal(); |
| 1494 | BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 1495 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1496 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1497 | } |
| 1498 | return Result; |
| 1499 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1501 | case ISD::TAILCALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1502 | case ISD::CALL: { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1503 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1504 | static const unsigned GPR[] = { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1505 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1506 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1507 | }; |
| 1508 | static const unsigned FPR[] = { |
| 1509 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1510 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1511 | }; |
| 1512 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1513 | // Lower the chain for this call. |
| 1514 | Select(N.getOperand(0)); |
| 1515 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1516 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1517 | MachineInstr *CallMI; |
| 1518 | // Emit the correct call instruction based on the type of symbol called. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1519 | if (GlobalAddressSDNode *GASD = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1520 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1521 | CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1522 | true); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1523 | } else if (ExternalSymbolSDNode *ESSDN = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1524 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1525 | CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1526 | true); |
| 1527 | } else { |
| 1528 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1529 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 1530 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 1531 | CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0) |
| 1532 | .addReg(PPC::R12); |
| 1533 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1534 | |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1535 | // Load the register args to virtual regs |
| 1536 | std::vector<unsigned> ArgVR; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1537 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1538 | ArgVR.push_back(SelectExpr(N.getOperand(i))); |
| 1539 | |
| 1540 | // Copy the virtual registers into the appropriate argument register |
| 1541 | for(int i = 0, e = ArgVR.size(); i < e; ++i) { |
| 1542 | switch(N.getOperand(i+2).getValueType()) { |
| 1543 | default: Node->dump(); assert(0 && "Unknown value type for call"); |
| 1544 | case MVT::i1: |
| 1545 | case MVT::i8: |
| 1546 | case MVT::i16: |
| 1547 | case MVT::i32: |
| 1548 | assert(GPR_idx < 8 && "Too many int args"); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1549 | if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1550 | BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1551 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1552 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1553 | ++GPR_idx; |
| 1554 | break; |
| 1555 | case MVT::f64: |
| 1556 | case MVT::f32: |
| 1557 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1558 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1559 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1560 | ++FPR_idx; |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1564 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1565 | // Put the call instruction in the correct place in the MachineBasicBlock |
| 1566 | BB->push_back(CallMI); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1567 | |
| 1568 | switch (Node->getValueType(0)) { |
| 1569 | default: assert(0 && "Unknown value type for call result!"); |
| 1570 | case MVT::Other: return 1; |
| 1571 | case MVT::i1: |
| 1572 | case MVT::i8: |
| 1573 | case MVT::i16: |
| 1574 | case MVT::i32: |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 1575 | if (Node->getValueType(1) == MVT::i32) { |
| 1576 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3); |
| 1577 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4); |
| 1578 | } else { |
| 1579 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
| 1580 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1581 | break; |
| 1582 | case MVT::f32: |
| 1583 | case MVT::f64: |
| 1584 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1585 | break; |
| 1586 | } |
| 1587 | return Result+N.ResNo; |
| 1588 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1589 | |
| 1590 | case ISD::SIGN_EXTEND: |
| 1591 | case ISD::SIGN_EXTEND_INREG: |
| 1592 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1593 | switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1594 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1595 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1596 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1597 | break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1598 | case MVT::i8: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1599 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1600 | break; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1601 | case MVT::i1: |
| 1602 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0); |
| 1603 | break; |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1604 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1605 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1606 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1607 | case ISD::CopyFromReg: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1608 | DestType = N.getValue(0).getValueType(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1609 | if (Result == 1) |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1610 | Result = ExprMap[N.getValue(0)] = MakeReg(DestType); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1611 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1612 | if (MVT::isInteger(DestType)) |
| 1613 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1614 | else |
| 1615 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1616 | return Result; |
| 1617 | |
| 1618 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1619 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1620 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1621 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1622 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1623 | .addImm(31-Tmp2); |
| 1624 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1625 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1626 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1627 | } |
| 1628 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1629 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1630 | case ISD::SRL: |
| 1631 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1632 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1633 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1634 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1635 | .addImm(Tmp2).addImm(31); |
| 1636 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1637 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1638 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1639 | } |
| 1640 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1641 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1642 | case ISD::SRA: |
| 1643 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1644 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1645 | Tmp2 = CN->getValue() & 0x1F; |
| 1646 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1647 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1648 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1649 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1650 | } |
| 1651 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1652 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1653 | case ISD::CTLZ: |
| 1654 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1655 | BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1); |
| 1656 | return Result; |
| 1657 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1658 | case ISD::ADD: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1659 | if (!MVT::isInteger(DestType)) { |
| 1660 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1661 | N.getOperand(0).Val->hasOneUse()) { |
| 1662 | ++FusedFP; // Statistic |
| 1663 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1664 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1665 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1666 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1667 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1668 | return Result; |
| 1669 | } |
| 1670 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1671 | N.getOperand(1).Val->hasOneUse()) { |
| 1672 | ++FusedFP; // Statistic |
| 1673 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1674 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1675 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1676 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1677 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1678 | return Result; |
| 1679 | } |
| 1680 | Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; |
| 1681 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1682 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1683 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1684 | return Result; |
| 1685 | } |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1686 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true)) |
| 1687 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1688 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 39c6896 | 2005-08-08 21:21:03 +0000 | [diff] [blame] | 1689 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1690 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1691 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1692 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1693 | case ISD::AND: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1694 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1695 | if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) { |
| 1696 | unsigned SH, MB, ME; |
| 1697 | Opc = Recording ? PPC::RLWINMo : PPC::RLWINM; |
| 1698 | unsigned OprOpc; |
| 1699 | if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) && |
| 1700 | isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) { |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1701 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1702 | } else { |
| 1703 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1704 | isRunOfOnes(Tmp2, MB, ME); |
| 1705 | SH = 0; |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1706 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1707 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH) |
| 1708 | .addImm(MB).addImm(ME); |
| 1709 | RecordSuccess = true; |
| 1710 | return Result; |
| 1711 | } else if (isUInt16(Tmp2)) { |
| 1712 | Tmp2 = Lo16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1713 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1714 | BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1715 | RecordSuccess = true; |
| 1716 | return Result; |
| 1717 | } else if (isUInt16(Tmp2)) { |
| 1718 | Tmp2 = Hi16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1719 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1720 | BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1721 | RecordSuccess = true; |
| 1722 | return Result; |
| 1723 | } |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1724 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1725 | if (isOprNot(N.getOperand(0))) { |
| 1726 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1727 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1728 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1729 | RecordSuccess = false; |
| 1730 | return Result; |
| 1731 | } |
| 1732 | // emit a regular and |
| 1733 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1734 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1735 | Opc = Recording ? PPC::ANDo : PPC::AND; |
| 1736 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1737 | RecordSuccess = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1738 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1739 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1740 | case ISD::OR: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1741 | if (SelectBitfieldInsert(N, Result)) |
| 1742 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1743 | if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI)) |
| 1744 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1745 | // emit regular or |
| 1746 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1747 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1748 | Opc = Recording ? PPC::ORo : PPC::OR; |
| 1749 | RecordSuccess = true; |
| 1750 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1751 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1752 | |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1753 | case ISD::XOR: { |
| 1754 | // Check for EQV: xor, (xor a, -1), b |
Chris Lattner | df706e3 | 2005-08-10 16:35:46 +0000 | [diff] [blame] | 1755 | if (isOprNot(N.getOperand(0))) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1756 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1757 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1758 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1759 | return Result; |
| 1760 | } |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1761 | // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1 |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1762 | if (isOprNot(N)) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1763 | switch(N.getOperand(0).getOpcode()) { |
| 1764 | case ISD::OR: |
| 1765 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1766 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1767 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1768 | break; |
| 1769 | case ISD::AND: |
| 1770 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1771 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1772 | BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1773 | break; |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1774 | case ISD::XOR: |
| 1775 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1776 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1777 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1778 | break; |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1779 | default: |
| 1780 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1781 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1782 | break; |
| 1783 | } |
| 1784 | return Result; |
| 1785 | } |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1786 | if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI)) |
| 1787 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1788 | // emit regular xor |
| 1789 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1790 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1791 | BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1792 | return Result; |
| 1793 | } |
| 1794 | |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1795 | case ISD::SUB: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1796 | if (!MVT::isInteger(DestType)) { |
| 1797 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1798 | N.getOperand(0).Val->hasOneUse()) { |
| 1799 | ++FusedFP; // Statistic |
| 1800 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1801 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1802 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1803 | Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS; |
| 1804 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1805 | return Result; |
| 1806 | } |
| 1807 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1808 | N.getOperand(1).Val->hasOneUse()) { |
| 1809 | ++FusedFP; // Statistic |
| 1810 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1811 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1812 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1813 | Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS; |
| 1814 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1815 | return Result; |
| 1816 | } |
| 1817 | Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; |
| 1818 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1819 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1820 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1821 | return Result; |
| 1822 | } |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1823 | if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) { |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1824 | Tmp1 = Lo16(Tmp1); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1825 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1826 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1); |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1827 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1828 | } |
| 1829 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true)) |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1830 | return Result; |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1831 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1832 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1833 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1834 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1835 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1836 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1837 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1838 | if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1839 | Tmp2 = Lo16(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1840 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1841 | } else { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1842 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1843 | switch (DestType) { |
| 1844 | default: assert(0 && "Unknown type to ISD::MUL"); break; |
| 1845 | case MVT::i32: Opc = PPC::MULLW; break; |
| 1846 | case MVT::f32: Opc = PPC::FMULS; break; |
| 1847 | case MVT::f64: Opc = PPC::FMUL; break; |
| 1848 | } |
| 1849 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1850 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1851 | return Result; |
| 1852 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1853 | case ISD::MULHS: |
| 1854 | case ISD::MULHU: |
| 1855 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1856 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1857 | Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW; |
| 1858 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1859 | return Result; |
| 1860 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1861 | case ISD::SDIV: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1862 | if (isIntImmediate(N.getOperand(1), Tmp3)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1863 | if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) { |
| 1864 | Tmp3 = Log2_32(Tmp3); |
| 1865 | Tmp1 = MakeReg(MVT::i32); |
| 1866 | Tmp2 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1867 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1868 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1869 | return Result; |
| 1870 | } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) { |
| 1871 | Tmp3 = Log2_32(-Tmp3); |
Chris Lattner | 2f46055 | 2005-08-09 18:08:41 +0000 | [diff] [blame] | 1872 | Tmp2 = SelectExpr(N.getOperand(0)); |
| 1873 | Tmp1 = MakeReg(MVT::i32); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1874 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1875 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1876 | BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1); |
| 1877 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4); |
| 1878 | return Result; |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1879 | } |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1880 | } |
| 1881 | // fall thru |
| 1882 | case ISD::UDIV: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1883 | // If this is a divide by constant, we can emit code using some magic |
| 1884 | // constants to implement it as a multiply instead. |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1885 | if (isIntImmediate(N.getOperand(1), Tmp3)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1886 | if (opcode == ISD::SDIV) { |
| 1887 | if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) { |
| 1888 | ExprMap.erase(N); |
| 1889 | return SelectExpr(BuildSDIVSequence(N)); |
| 1890 | } |
| 1891 | } else { |
| 1892 | if ((signed)Tmp3 > 1) { |
| 1893 | ExprMap.erase(N); |
| 1894 | return SelectExpr(BuildUDIVSequence(N)); |
| 1895 | } |
| 1896 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1897 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1898 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1899 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1900 | switch (DestType) { |
| 1901 | default: assert(0 && "Unknown type to ISD::SDIV"); break; |
| 1902 | case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break; |
| 1903 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 1904 | case MVT::f64: Opc = PPC::FDIV; break; |
| 1905 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1906 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1907 | return Result; |
| 1908 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1909 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1910 | case ISD::SUB_PARTS: { |
| 1911 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1912 | "Not an i64 add/sub!"); |
| 1913 | // Emit all of the operands. |
| 1914 | std::vector<unsigned> InVals; |
| 1915 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1916 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1917 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1918 | BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 1919 | BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1920 | } else { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1921 | BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]); |
| 1922 | BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]); |
| 1923 | } |
| 1924 | return Result+N.ResNo; |
| 1925 | } |
| 1926 | |
| 1927 | case ISD::SHL_PARTS: |
| 1928 | case ISD::SRA_PARTS: |
| 1929 | case ISD::SRL_PARTS: { |
| 1930 | assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 && |
| 1931 | "Not an i64 shift!"); |
| 1932 | unsigned ShiftOpLo = SelectExpr(N.getOperand(0)); |
| 1933 | unsigned ShiftOpHi = SelectExpr(N.getOperand(1)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1934 | unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2)); |
| 1935 | Tmp1 = MakeReg(MVT::i32); |
| 1936 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1937 | Tmp3 = MakeReg(MVT::i32); |
| 1938 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1939 | unsigned Tmp5 = MakeReg(MVT::i32); |
| 1940 | unsigned Tmp6 = MakeReg(MVT::i32); |
| 1941 | BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32); |
| 1942 | if (ISD::SHL_PARTS == opcode) { |
| 1943 | BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg); |
| 1944 | BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1); |
| 1945 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1946 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 1947 | BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1948 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6); |
| 1949 | BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg); |
| 1950 | } else if (ISD::SRL_PARTS == opcode) { |
| 1951 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1952 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1953 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1954 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1955 | BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1956 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6); |
| 1957 | BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1958 | } else { |
| 1959 | MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1960 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1961 | MachineBasicBlock *OldMBB = BB; |
| 1962 | MachineFunction *F = BB->getParent(); |
| 1963 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1964 | F->getBasicBlockList().insert(It, TmpMBB); |
| 1965 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1966 | BB->addSuccessor(TmpMBB); |
| 1967 | BB->addSuccessor(PhiMBB); |
| 1968 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1969 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1970 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1971 | BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1972 | BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1973 | BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1974 | BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 1975 | // Select correct least significant half if the shift amount > 32 |
| 1976 | BB = TmpMBB; |
| 1977 | unsigned Tmp7 = MakeReg(MVT::i32); |
| 1978 | BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6); |
| 1979 | TmpMBB->addSuccessor(PhiMBB); |
| 1980 | BB = PhiMBB; |
| 1981 | BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB) |
| 1982 | .addReg(Tmp7).addMBB(TmpMBB); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1983 | } |
| 1984 | return Result+N.ResNo; |
| 1985 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1986 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1987 | case ISD::FP_TO_UINT: |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 1988 | case ISD::FP_TO_SINT: { |
| 1989 | bool U = (ISD::FP_TO_UINT == opcode); |
| 1990 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1991 | if (!U) { |
| 1992 | Tmp2 = MakeReg(MVT::f64); |
| 1993 | BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1); |
| 1994 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 1995 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx); |
| 1996 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4); |
| 1997 | return Result; |
| 1998 | } else { |
| 1999 | unsigned Zero = getConstDouble(0.0); |
| 2000 | unsigned MaxInt = getConstDouble((1LL << 32) - 1); |
| 2001 | unsigned Border = getConstDouble(1LL << 31); |
| 2002 | unsigned UseZero = MakeReg(MVT::f64); |
| 2003 | unsigned UseMaxInt = MakeReg(MVT::f64); |
| 2004 | unsigned UseChoice = MakeReg(MVT::f64); |
| 2005 | unsigned TmpReg = MakeReg(MVT::f64); |
| 2006 | unsigned TmpReg2 = MakeReg(MVT::f64); |
| 2007 | unsigned ConvReg = MakeReg(MVT::f64); |
| 2008 | unsigned IntTmp = MakeReg(MVT::i32); |
| 2009 | unsigned XorReg = MakeReg(MVT::i32); |
| 2010 | MachineFunction *F = BB->getParent(); |
| 2011 | int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8); |
| 2012 | // Update machine-CFG edges |
| 2013 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 2014 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 2015 | MachineBasicBlock *OldMBB = BB; |
| 2016 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 2017 | F->getBasicBlockList().insert(It, XorMBB); |
| 2018 | F->getBasicBlockList().insert(It, PhiMBB); |
| 2019 | BB->addSuccessor(XorMBB); |
| 2020 | BB->addSuccessor(PhiMBB); |
| 2021 | // Convert from floating point to unsigned 32-bit value |
| 2022 | // Use 0 if incoming value is < 0.0 |
| 2023 | BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero); |
| 2024 | // Use 2**32 - 1 if incoming value is >= 2**32 |
| 2025 | BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1); |
| 2026 | BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero) |
| 2027 | .addReg(MaxInt); |
| 2028 | // Subtract 2**31 |
| 2029 | BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
| 2030 | // Use difference if >= 2**31 |
| 2031 | BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border); |
| 2032 | BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
| 2033 | .addReg(UseChoice); |
| 2034 | // Convert to integer |
| 2035 | BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 2036 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx); |
| 2037 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4); |
| 2038 | BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 2039 | BuildMI(BB, PPC::B, 1).addMBB(XorMBB); |
| 2040 | |
| 2041 | // XorMBB: |
| 2042 | // add 2**31 if input was >= 2**31 |
| 2043 | BB = XorMBB; |
| 2044 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
| 2045 | XorMBB->addSuccessor(PhiMBB); |
| 2046 | |
| 2047 | // PhiMBB: |
| 2048 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 2049 | BB = PhiMBB; |
| 2050 | BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB) |
| 2051 | .addReg(XorReg).addMBB(XorMBB); |
| 2052 | return Result; |
| 2053 | } |
| 2054 | assert(0 && "Should never get here"); |
| 2055 | return 0; |
| 2056 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2057 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2058 | case ISD::SETCC: { |
| 2059 | ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get(); |
| 2060 | if (isIntImmediate(Node->getOperand(1), Tmp3)) { |
| 2061 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 2062 | // Check for those cases here. |
| 2063 | // setcc op, 0 |
| 2064 | if (Tmp3 == 0) { |
| 2065 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 2066 | switch (CC) { |
| 2067 | default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort(); |
| 2068 | case ISD::SETEQ: |
| 2069 | Tmp2 = MakeReg(MVT::i32); |
| 2070 | BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1); |
| 2071 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27) |
| 2072 | .addImm(5).addImm(31); |
| 2073 | break; |
| 2074 | case ISD::SETNE: |
| 2075 | Tmp2 = MakeReg(MVT::i32); |
| 2076 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 2077 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 2078 | break; |
| 2079 | case ISD::SETLT: |
| 2080 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1) |
| 2081 | .addImm(31).addImm(31); |
| 2082 | break; |
| 2083 | case ISD::SETGT: |
| 2084 | Tmp2 = MakeReg(MVT::i32); |
| 2085 | Tmp3 = MakeReg(MVT::i32); |
| 2086 | BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1); |
| 2087 | BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2088 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2089 | .addImm(31).addImm(31); |
| 2090 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2091 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2092 | return Result; |
| 2093 | } else if (Tmp3 == ~0U) { // setcc op, -1 |
| 2094 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 2095 | switch (CC) { |
| 2096 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 2097 | case ISD::SETEQ: |
| 2098 | Tmp2 = MakeReg(MVT::i32); |
| 2099 | Tmp3 = MakeReg(MVT::i32); |
| 2100 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2101 | BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0); |
| 2102 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3); |
| 2103 | break; |
| 2104 | case ISD::SETNE: |
| 2105 | Tmp2 = MakeReg(MVT::i32); |
| 2106 | Tmp3 = MakeReg(MVT::i32); |
| 2107 | BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2108 | BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1); |
| 2109 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2); |
| 2110 | break; |
| 2111 | case ISD::SETLT: |
| 2112 | Tmp2 = MakeReg(MVT::i32); |
| 2113 | Tmp3 = MakeReg(MVT::i32); |
| 2114 | BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2115 | BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2116 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2117 | .addImm(31).addImm(31); |
| 2118 | break; |
| 2119 | case ISD::SETGT: |
| 2120 | Tmp2 = MakeReg(MVT::i32); |
| 2121 | BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1) |
| 2122 | .addImm(31).addImm(31); |
| 2123 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1); |
| 2124 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2125 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2126 | return Result; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2127 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 2128 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2130 | bool Inv; |
| 2131 | unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2); |
| 2132 | MoveCRtoGPR(CCReg, Inv, Tmp2, Result); |
| 2133 | return Result; |
| 2134 | } |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2135 | case ISD::SELECT: { |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2136 | SDNode *Cond = N.getOperand(0).Val; |
| 2137 | ISD::CondCode CC; |
| 2138 | if (Cond->getOpcode() == ISD::SETCC && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2139 | !MVT::isInteger(N.getOperand(1).getValueType()) && |
Chris Lattner | 979a21e | 2005-08-10 03:40:09 +0000 | [diff] [blame] | 2140 | !MVT::isInteger(Cond->getOperand(1).getValueType()) && |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2141 | cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETEQ && |
| 2142 | cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETNE) { |
| 2143 | MVT::ValueType VT = Cond->getOperand(0).getValueType(); |
| 2144 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond->getOperand(2))->get(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2145 | unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE |
| 2146 | unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE |
| 2147 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2148 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Cond->getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2149 | if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) { |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2150 | switch(CC) { |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2151 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2152 | case ISD::SETULT: |
| 2153 | case ISD::SETLT: |
| 2154 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2155 | case ISD::SETUGE: |
| 2156 | case ISD::SETGE: |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2157 | Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2158 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV); |
| 2159 | return Result; |
| 2160 | case ISD::SETUGT: |
| 2161 | case ISD::SETGT: |
| 2162 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2163 | case ISD::SETULE: |
| 2164 | case ISD::SETLE: { |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2165 | if (Cond->getOperand(0).getOpcode() == ISD::FNEG) { |
| 2166 | Tmp2 = SelectExpr(Cond->getOperand(0).getOperand(0)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2167 | } else { |
| 2168 | Tmp2 = MakeReg(VT); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2169 | Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2170 | BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1); |
| 2171 | } |
| 2172 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV); |
| 2173 | return Result; |
| 2174 | } |
| 2175 | } |
| 2176 | } else { |
| 2177 | Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS; |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2178 | Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against |
| 2179 | Tmp2 = SelectExpr(Cond->getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2180 | Tmp3 = MakeReg(VT); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 2181 | switch(CC) { |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2182 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2183 | case ISD::SETULT: |
| 2184 | case ISD::SETLT: |
| 2185 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2186 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2187 | return Result; |
| 2188 | case ISD::SETUGE: |
| 2189 | case ISD::SETGE: |
| 2190 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2191 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2192 | return Result; |
| 2193 | case ISD::SETUGT: |
| 2194 | case ISD::SETGT: |
| 2195 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2196 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2197 | return Result; |
| 2198 | case ISD::SETULE: |
| 2199 | case ISD::SETLE: |
| 2200 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2201 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2202 | return Result; |
| 2203 | } |
| 2204 | } |
| 2205 | assert(0 && "Should never get here"); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2208 | bool Inv; |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2209 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
| 2210 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2211 | unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3); |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2212 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2213 | // Create an iterator with which to insert the MBB for copying the false |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2214 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 2215 | MachineBasicBlock *thisMBB = BB; |
| 2216 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 2217 | ilist<MachineBasicBlock>::iterator It = BB; |
| 2218 | ++It; |
| 2219 | |
| 2220 | // thisMBB: |
| 2221 | // ... |
| 2222 | // TrueVal = ... |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2223 | // cmpTY ccX, r1, r2 |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2224 | // bCC copy1MBB |
| 2225 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2226 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 2227 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2228 | BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2229 | MachineFunction *F = BB->getParent(); |
| 2230 | F->getBasicBlockList().insert(It, copy0MBB); |
| 2231 | F->getBasicBlockList().insert(It, sinkMBB); |
| 2232 | // Update machine-CFG edges |
| 2233 | BB->addSuccessor(copy0MBB); |
| 2234 | BB->addSuccessor(sinkMBB); |
| 2235 | |
| 2236 | // copy0MBB: |
| 2237 | // %FalseValue = ... |
| 2238 | // # fallthrough to sinkMBB |
| 2239 | BB = copy0MBB; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2240 | // Update machine-CFG edges |
| 2241 | BB->addSuccessor(sinkMBB); |
| 2242 | |
| 2243 | // sinkMBB: |
| 2244 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 2245 | // ... |
| 2246 | BB = sinkMBB; |
| 2247 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 2248 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2249 | return Result; |
| 2250 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2251 | |
| 2252 | case ISD::Constant: |
| 2253 | switch (N.getValueType()) { |
| 2254 | default: assert(0 && "Cannot use constants of this type!"); |
| 2255 | case MVT::i1: |
| 2256 | BuildMI(BB, PPC::LI, 1, Result) |
| 2257 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 2258 | break; |
| 2259 | case MVT::i32: |
| 2260 | { |
| 2261 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 2262 | if (v < 32768 && v >= -32768) { |
| 2263 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 2264 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 2265 | Tmp1 = MakeReg(MVT::i32); |
| 2266 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 2267 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | return Result; |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2272 | |
| 2273 | case ISD::ConstantFP: { |
| 2274 | ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N); |
| 2275 | Result = getConstDouble(CN->getValue(), Result); |
| 2276 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2279 | case ISD::FNEG: |
| 2280 | if (!NoExcessFPPrecision && |
| 2281 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2282 | N.getOperand(0).Val->hasOneUse() && |
| 2283 | ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() && |
| 2284 | N.getOperand(0).getOperand(0).Val->hasOneUse()) { |
| 2285 | ++FusedFP; // Statistic |
| 2286 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0)); |
| 2287 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1)); |
| 2288 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 2289 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2290 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2291 | } else if (!NoExcessFPPrecision && |
| 2292 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2293 | N.getOperand(0).Val->hasOneUse() && |
| 2294 | ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() && |
| 2295 | N.getOperand(0).getOperand(1).Val->hasOneUse()) { |
| 2296 | ++FusedFP; // Statistic |
| 2297 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0)); |
| 2298 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1)); |
| 2299 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2300 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2301 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2302 | } else if (ISD::FABS == N.getOperand(0).getOpcode()) { |
| 2303 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2304 | BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1); |
| 2305 | } else { |
| 2306 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2307 | BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1); |
| 2308 | } |
| 2309 | return Result; |
| 2310 | |
| 2311 | case ISD::FABS: |
| 2312 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2313 | BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1); |
| 2314 | return Result; |
| 2315 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 2316 | case ISD::FSQRT: |
| 2317 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2318 | Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS; |
| 2319 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2320 | return Result; |
| 2321 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2322 | case ISD::FP_ROUND: |
| 2323 | assert (DestType == MVT::f32 && |
| 2324 | N.getOperand(0).getValueType() == MVT::f64 && |
| 2325 | "only f64 to f32 conversion supported here"); |
| 2326 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2327 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 2328 | return Result; |
| 2329 | |
| 2330 | case ISD::FP_EXTEND: |
| 2331 | assert (DestType == MVT::f64 && |
| 2332 | N.getOperand(0).getValueType() == MVT::f32 && |
| 2333 | "only f32 to f64 conversion supported here"); |
| 2334 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2335 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 2336 | return Result; |
| 2337 | |
| 2338 | case ISD::UINT_TO_FP: |
| 2339 | case ISD::SINT_TO_FP: { |
| 2340 | assert (N.getOperand(0).getValueType() == MVT::i32 |
| 2341 | && "int to float must operate on i32"); |
| 2342 | bool IsUnsigned = (ISD::UINT_TO_FP == opcode); |
| 2343 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2344 | Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into |
| 2345 | Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant |
| 2346 | |
| 2347 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 2348 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 2349 | |
| 2350 | if (IsUnsigned) { |
| 2351 | unsigned ConstF = getConstDouble(0x1.000000p52); |
| 2352 | // Store the hi & low halves of the fp value, currently in int regs |
| 2353 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2354 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2355 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4); |
| 2356 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2357 | // Generate the return value with a subtract |
| 2358 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2359 | } else { |
| 2360 | unsigned ConstF = getConstDouble(0x1.000008p52); |
| 2361 | unsigned TmpL = MakeReg(MVT::i32); |
| 2362 | // Store the hi & low halves of the fp value, currently in int regs |
| 2363 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2364 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2365 | BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000); |
| 2366 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4); |
| 2367 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2368 | // Generate the return value with a subtract |
| 2369 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2370 | } |
| 2371 | return Result; |
| 2372 | } |
| 2373 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
| 2376 | |
| 2377 | void ISel::Select(SDOperand N) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2378 | unsigned Tmp1, Tmp2, Tmp3, Opc; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2379 | unsigned opcode = N.getOpcode(); |
| 2380 | |
| 2381 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 2382 | return; // Already selected. |
| 2383 | |
| 2384 | SDNode *Node = N.Val; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2385 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2386 | switch (Node->getOpcode()) { |
| 2387 | default: |
| 2388 | Node->dump(); std::cerr << "\n"; |
| 2389 | assert(0 && "Node not handled yet!"); |
| 2390 | case ISD::EntryToken: return; // Noop |
| 2391 | case ISD::TokenFactor: |
| 2392 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 2393 | Select(Node->getOperand(i)); |
| 2394 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2395 | case ISD::CALLSEQ_START: |
| 2396 | case ISD::CALLSEQ_END: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2397 | Select(N.getOperand(0)); |
| 2398 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2399 | Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN : |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2400 | PPC::ADJCALLSTACKUP; |
| 2401 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 2402 | return; |
| 2403 | case ISD::BR: { |
| 2404 | MachineBasicBlock *Dest = |
| 2405 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2406 | Select(N.getOperand(0)); |
| 2407 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 2408 | return; |
| 2409 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2410 | case ISD::BRCOND: |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 2411 | case ISD::BRCONDTWOWAY: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2412 | SelectBranchCC(N); |
| 2413 | return; |
| 2414 | case ISD::CopyToReg: |
| 2415 | Select(N.getOperand(0)); |
| 2416 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2417 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2418 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2419 | if (Tmp1 != Tmp2) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2420 | if (N.getOperand(1).getValueType() == MVT::f64 || |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2421 | N.getOperand(1).getValueType() == MVT::f32) |
| 2422 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 2423 | else |
| 2424 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2425 | } |
| 2426 | return; |
| 2427 | case ISD::ImplicitDef: |
| 2428 | Select(N.getOperand(0)); |
| 2429 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 2430 | return; |
| 2431 | case ISD::RET: |
| 2432 | switch (N.getNumOperands()) { |
| 2433 | default: |
| 2434 | assert(0 && "Unknown return instruction!"); |
| 2435 | case 3: |
| 2436 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 2437 | N.getOperand(2).getValueType() == MVT::i32 && |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 2438 | "Unknown two-register value!"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2439 | Select(N.getOperand(0)); |
| 2440 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2441 | Tmp2 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 2442 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2); |
| 2443 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2444 | break; |
| 2445 | case 2: |
| 2446 | Select(N.getOperand(0)); |
| 2447 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2448 | switch (N.getOperand(1).getValueType()) { |
| 2449 | default: |
| 2450 | assert(0 && "Unknown return type!"); |
| 2451 | case MVT::f64: |
| 2452 | case MVT::f32: |
| 2453 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 2454 | break; |
| 2455 | case MVT::i32: |
| 2456 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 2457 | break; |
| 2458 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 2459 | case 1: |
| 2460 | Select(N.getOperand(0)); |
| 2461 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2462 | } |
| 2463 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 2464 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2465 | case ISD::TRUNCSTORE: |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2466 | case ISD::STORE: { |
| 2467 | SDOperand Chain = N.getOperand(0); |
| 2468 | SDOperand Value = N.getOperand(1); |
| 2469 | SDOperand Address = N.getOperand(2); |
| 2470 | Select(Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2471 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2472 | Tmp1 = SelectExpr(Value); //value |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2473 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2474 | if (opcode == ISD::STORE) { |
| 2475 | switch(Value.getValueType()) { |
| 2476 | default: assert(0 && "unknown Type in store"); |
| 2477 | case MVT::i32: Opc = PPC::STW; break; |
| 2478 | case MVT::f64: Opc = PPC::STFD; break; |
| 2479 | case MVT::f32: Opc = PPC::STFS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2480 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2481 | } else { //ISD::TRUNCSTORE |
| 2482 | switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) { |
| 2483 | default: assert(0 && "unknown Type in store"); |
| 2484 | case MVT::i1: |
| 2485 | case MVT::i8: Opc = PPC::STB; break; |
| 2486 | case MVT::i16: Opc = PPC::STH; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2487 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2488 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2489 | |
| 2490 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 2491 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 2492 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2493 | } else { |
| 2494 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 2495 | switch(SelectAddr(Address, Tmp2, offset)) { |
| 2496 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 2497 | case 0: // imm offset, no frame, no index |
| 2498 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2); |
| 2499 | break; |
| 2500 | case 1: // imm offset + frame index |
| 2501 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset); |
| 2502 | break; |
| 2503 | case 2: // base+index addressing |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2504 | Opc = IndexedOpForOp(Opc); |
| 2505 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 2506 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 2507 | case 3: { |
| 2508 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 2509 | GlobalValue *GV = GN->getGlobal(); |
| 2510 | BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2); |
| 2511 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
| 2514 | return; |
| 2515 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2516 | case ISD::EXTLOAD: |
| 2517 | case ISD::SEXTLOAD: |
| 2518 | case ISD::ZEXTLOAD: |
| 2519 | case ISD::LOAD: |
| 2520 | case ISD::CopyFromReg: |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 2521 | case ISD::TAILCALL: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2522 | case ISD::CALL: |
| 2523 | case ISD::DYNAMIC_STACKALLOC: |
| 2524 | ExprMap.erase(N); |
| 2525 | SelectExpr(N); |
| 2526 | return; |
| 2527 | } |
| 2528 | assert(0 && "Should not be reached!"); |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 2533 | /// into a machine code representation using pattern matching and a machine |
| 2534 | /// description file. |
| 2535 | /// |
| 2536 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2537 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |