Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/DwarfWriter.h" |
| 20 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetSubtarget.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 28 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 29 | #include "llvm/Constants.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 30 | #include "llvm/DerivedTypes.h" |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 31 | #include "llvm/Function.h" |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 32 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 39 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 44 | /// hacks on it until the target machine can handle it. This involves |
| 45 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 46 | /// large sizes or splitting up large values into small values) as well as |
| 47 | /// eliminating operations the machine cannot handle. |
| 48 | /// |
| 49 | /// This code also does a small amount of optimization and recognition of idioms |
| 50 | /// as part of its processing. For example, if a target does not support a |
| 51 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 52 | /// will attempt merge setcc and brc instructions into brcc's. |
| 53 | /// |
| 54 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 55 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 56 | TargetLowering &TLI; |
| 57 | SelectionDAG &DAG; |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 58 | CodeGenOpt::Level OptLevel; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 60 | // Libcall insertion helpers. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 62 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 63 | /// legalized. We use this to ensure that calls are properly serialized |
| 64 | /// against each other, including inserted libcalls. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 65 | SDValue LastCALLSEQ_END; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 67 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 68 | /// helpful assertions that a libcall isn't created while another call is |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 69 | /// being legalized (which could lead to non-serialized call sequences). |
| 70 | bool IsLegalizingCall; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 72 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 73 | Legal, // The target natively supports this operation. |
| 74 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 75 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 76 | }; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 78 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 79 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 80 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 81 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 83 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 84 | /// than one use, this map indicates what regularized operand to use. This |
| 85 | /// allows us to avoid legalizing the same thing more than once. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 86 | DenseMap<SDValue, SDValue> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 88 | void AddLegalizedOperand(SDValue From, SDValue To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 89 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 90 | // If someone requests legalization of the new node, return itself. |
| 91 | if (From != To) |
| 92 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 95 | public: |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 96 | SelectionDAGLegalize(SelectionDAG &DAG, CodeGenOpt::Level ol); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 98 | /// getTypeAction - Return how we should legalize values of this type, either |
| 99 | /// it is already legal or we need to expand it into multiple registers of |
| 100 | /// smaller integer type, or we need to promote it to a larger type. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 101 | LegalizeAction getTypeAction(MVT VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 102 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// isTypeLegal - Return true if this type is legal on this target. |
| 106 | /// |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 107 | bool isTypeLegal(MVT VT) const { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 108 | return getTypeAction(VT) == Legal; |
| 109 | } |
| 110 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 111 | void LegalizeDAG(); |
| 112 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 113 | private: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 114 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 115 | /// appropriate for its type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 116 | void HandleOp(SDValue Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 117 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 118 | /// LegalizeOp - We know that the specified value has a legal type. |
| 119 | /// Recursively ensure that the operands have legal types, then return the |
| 120 | /// result. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 121 | SDValue LegalizeOp(SDValue O); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 122 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 123 | /// PerformInsertVectorEltInMemory - Some target cannot handle a variable |
| 124 | /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it |
| 125 | /// is necessary to spill the vector being inserted into to memory, perform |
| 126 | /// the insert there, and then read the result back. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 127 | SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame^] | 128 | SDValue Idx, DebugLoc dl); |
| 129 | SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, |
| 130 | SDValue Idx, DebugLoc dl); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 131 | |
Mon P Wang | f007a8b | 2008-11-06 05:31:54 +0000 | [diff] [blame] | 132 | /// Useful 16 element vector type that is used to pass operands for widening. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 133 | typedef SmallVector<SDValue, 16> SDValueVector; |
| 134 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 135 | /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which |
| 136 | /// performs the same shuffe in terms of order or result bytes, but on a type |
| 137 | /// whose vector element type is narrower than the original shuffle type. |
| 138 | /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> |
| 139 | SDValue ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl, |
| 140 | SDValue N1, SDValue N2, |
| 141 | SmallVectorImpl<int> &Mask) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 142 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 143 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 144 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 145 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 146 | void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC, |
| 147 | DebugLoc dl); |
| 148 | void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, |
| 149 | DebugLoc dl); |
| 150 | void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, |
| 151 | DebugLoc dl) { |
| 152 | LegalizeSetCCOperands(LHS, RHS, CC, dl); |
| 153 | LegalizeSetCCCondCode(VT, LHS, RHS, CC, dl); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 154 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 155 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 156 | SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned, |
| 157 | SDValue &Hi); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 158 | |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 159 | SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT, DebugLoc dl); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 160 | SDValue ExpandBUILD_VECTOR(SDNode *Node); |
| 161 | SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 162 | SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT, |
| 163 | DebugLoc dl); |
| 164 | SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned, |
| 165 | DebugLoc dl); |
| 166 | SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned, |
| 167 | DebugLoc dl); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 168 | |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 169 | SDValue ExpandBSWAP(SDValue Op, DebugLoc dl); |
| 170 | SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 171 | |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 172 | SDValue ExpandExtractFromVectorThroughStack(SDValue Op); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 173 | |
| 174 | void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); |
| 175 | void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 179 | /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which |
| 180 | /// performs the same shuffe in terms of order or result bytes, but on a type |
| 181 | /// whose vector element type is narrower than the original shuffle type. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 182 | /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 183 | SDValue |
| 184 | SelectionDAGLegalize::ShuffleWithNarrowerEltType(MVT NVT, MVT VT, DebugLoc dl, |
| 185 | SDValue N1, SDValue N2, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 186 | SmallVectorImpl<int> &Mask) const { |
| 187 | MVT EltVT = NVT.getVectorElementType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 188 | unsigned NumMaskElts = VT.getVectorNumElements(); |
| 189 | unsigned NumDestElts = NVT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 190 | unsigned NumEltsGrowth = NumDestElts / NumMaskElts; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 191 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 192 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 193 | |
| 194 | if (NumEltsGrowth == 1) |
| 195 | return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]); |
| 196 | |
| 197 | SmallVector<int, 8> NewMask; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 198 | for (unsigned i = 0; i != NumMaskElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 199 | int Idx = Mask[i]; |
| 200 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 201 | if (Idx < 0) |
| 202 | NewMask.push_back(-1); |
| 203 | else |
| 204 | NewMask.push_back(Idx * NumEltsGrowth + j); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 206 | } |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 207 | assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 208 | assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); |
| 209 | return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Bill Wendling | 5aa4977 | 2009-02-24 02:35:30 +0000 | [diff] [blame] | 212 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 213 | CodeGenOpt::Level ol) |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 214 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), OptLevel(ol), |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 215 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 216 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 217 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 220 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 221 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 222 | IsLegalizingCall = false; |
Mon P Wang | e5ab34e | 2009-02-04 19:38:14 +0000 | [diff] [blame] | 223 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 224 | // The legalize process is inherently a bottom-up recursive process (users |
| 225 | // legalize their uses before themselves). Given infinite stack space, we |
| 226 | // could just start legalizing on the root and traverse the whole graph. In |
| 227 | // practice however, this causes us to run out of stack space on large basic |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 228 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 229 | // node is only legalized after all of its operands are legalized. |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 230 | DAG.AssignTopologicalOrder(); |
| 231 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 232 | E = prior(DAG.allnodes_end()); I != next(E); ++I) |
| 233 | HandleOp(SDValue(I, 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 234 | |
| 235 | // Finally, it's possible the root changed. Get the new root. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 236 | SDValue OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 237 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 238 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 240 | LegalizedNodes.clear(); |
| 241 | |
| 242 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 243 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 246 | |
| 247 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 248 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 249 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 250 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 251 | return Node; |
| 252 | if (Node->use_empty()) |
| 253 | return 0; // No CallSeqEnd |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 255 | // The chain is usually at the end. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 256 | SDValue TheChain(Node, Node->getNumValues()-1); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 257 | if (TheChain.getValueType() != MVT::Other) { |
| 258 | // Sometimes it's at the beginning. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 259 | TheChain = SDValue(Node, 0); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 260 | if (TheChain.getValueType() != MVT::Other) { |
| 261 | // Otherwise, hunt for it. |
| 262 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 263 | if (Node->getValueType(i) == MVT::Other) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 264 | TheChain = SDValue(Node, i); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 267 | |
| 268 | // Otherwise, we walked into a node without a chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 269 | if (TheChain.getValueType() != MVT::Other) |
| 270 | return 0; |
| 271 | } |
| 272 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 274 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 275 | E = Node->use_end(); UI != E; ++UI) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 277 | // Make sure to only follow users of our token chain. |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 278 | SDNode *User = *UI; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 279 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 280 | if (User->getOperand(i) == TheChain) |
| 281 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 282 | return Result; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 287 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 288 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 289 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 290 | assert(Node && "Didn't find callseq_start for a call??"); |
| 291 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 293 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 294 | "Node doesn't have a token chain argument!"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 295 | return FindCallStartFromCallEnd(Node->getOperand(0).getNode()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 299 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 300 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 301 | /// |
| 302 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 303 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 304 | /// |
| 305 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 306 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 307 | if (N == Dest) return true; // N certainly leads to Dest :) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 308 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 309 | // If we've already processed this node and it does lead to Dest, there is no |
| 310 | // need to reprocess it. |
| 311 | if (NodesLeadingTo.count(N)) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 313 | // If the first result of this node has been already legalized, then it cannot |
| 314 | // reach N. |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 315 | if (LegalizedNodes.count(SDValue(N, 0))) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 316 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 317 | // Okay, this node has not already been legalized. Check and legalize all |
| 318 | // operands. If none lead to Dest, then we can legalize this node. |
| 319 | bool OperandsLeadToDest = false; |
| 320 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 321 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 322 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 323 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 324 | if (OperandsLeadToDest) { |
| 325 | NodesLeadingTo.insert(N); |
| 326 | return true; |
| 327 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 328 | |
| 329 | // Okay, this node looks safe, legalize it and return false. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 330 | HandleOp(SDValue(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 334 | /// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 335 | /// appropriate for its type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 336 | void SelectionDAGLegalize::HandleOp(SDValue Op) { |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 337 | // Don't touch TargetConstants |
| 338 | if (Op.getOpcode() == ISD::TargetConstant) |
| 339 | return; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 340 | MVT VT = Op.getValueType(); |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 341 | // We should never see any illegal result types here. |
| 342 | assert(isTypeLegal(VT) && "Illegal type introduced after type legalization?"); |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 343 | (void)LegalizeOp(Op); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 345 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 346 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 347 | /// a load from the constant pool. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 348 | static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Dan Gohman | 0d137d7 | 2009-01-15 16:43:02 +0000 | [diff] [blame] | 349 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 350 | bool Extend = false; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 351 | DebugLoc dl = CFP->getDebugLoc(); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 352 | |
| 353 | // If a FP immediate is precise when represented as a float and if the |
| 354 | // target can do an extending load from float to double, we put it into |
| 355 | // the constant pool as a float, even if it's is statically typed as a |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 356 | // double. This shrinks FP constants and canonicalizes them for targets where |
| 357 | // an FP extending load is the same cost as a normal load (such as on the x87 |
| 358 | // fp stack or PPC FP unit). |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 359 | MVT VT = CFP->getValueType(0); |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 360 | ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 361 | if (!UseCP) { |
Chris Lattner | d2e936a | 2009-03-08 01:47:41 +0000 | [diff] [blame] | 362 | assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 363 | return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 364 | (VT == MVT::f64) ? MVT::i64 : MVT::i32); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 367 | MVT OrigVT = VT; |
| 368 | MVT SVT = VT; |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 369 | while (SVT != MVT::f32) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 370 | SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 371 | if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) && |
| 372 | // Only do this if the target has a native EXTLOAD instruction from |
| 373 | // smaller type. |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 374 | TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) && |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 375 | TLI.ShouldShrinkFPConstant(OrigVT)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 376 | const Type *SType = SVT.getTypeForMVT(); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 377 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); |
| 378 | VT = SVT; |
| 379 | Extend = true; |
| 380 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 383 | SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 384 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 385 | if (Extend) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 386 | return DAG.getExtLoad(ISD::EXTLOAD, dl, |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 387 | OrigVT, DAG.getEntryNode(), |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 388 | CPIdx, PseudoSourceValue::getConstantPool(), |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 389 | 0, VT, false, Alignment); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 390 | return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 391 | PseudoSourceValue::getConstantPool(), 0, false, Alignment); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 394 | /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. |
| 395 | static |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 396 | SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, |
Dan Gohman | 0d137d7 | 2009-01-15 16:43:02 +0000 | [diff] [blame] | 397 | const TargetLowering &TLI) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 398 | SDValue Chain = ST->getChain(); |
| 399 | SDValue Ptr = ST->getBasePtr(); |
| 400 | SDValue Val = ST->getValue(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 401 | MVT VT = Val.getValueType(); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 402 | int Alignment = ST->getAlignment(); |
| 403 | int SVOffset = ST->getSrcValueOffset(); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 404 | DebugLoc dl = ST->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 405 | if (ST->getMemoryVT().isFloatingPoint() || |
| 406 | ST->getMemoryVT().isVector()) { |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 407 | MVT intVT = MVT::getIntegerVT(VT.getSizeInBits()); |
| 408 | if (TLI.isTypeLegal(intVT)) { |
| 409 | // Expand to a bitconvert of the value to the integer type of the |
| 410 | // same size, then a (misaligned) int store. |
| 411 | // FIXME: Does not handle truncating floating point stores! |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 412 | SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, intVT, Val); |
| 413 | return DAG.getStore(Chain, dl, Result, Ptr, ST->getSrcValue(), |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 414 | SVOffset, ST->isVolatile(), Alignment); |
| 415 | } else { |
| 416 | // Do a (aligned) store to a stack slot, then copy from the stack slot |
| 417 | // to the final destination using (unaligned) integer loads and stores. |
| 418 | MVT StoredVT = ST->getMemoryVT(); |
| 419 | MVT RegVT = |
| 420 | TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits())); |
| 421 | unsigned StoredBytes = StoredVT.getSizeInBits() / 8; |
| 422 | unsigned RegBytes = RegVT.getSizeInBits() / 8; |
| 423 | unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 424 | |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 425 | // Make sure the stack slot is also aligned for the register type. |
| 426 | SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT); |
| 427 | |
| 428 | // Perform the original store, only redirected to the stack slot. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 429 | SDValue Store = DAG.getTruncStore(Chain, dl, |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 430 | Val, StackPtr, NULL, 0, StoredVT); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 431 | SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); |
| 432 | SmallVector<SDValue, 8> Stores; |
| 433 | unsigned Offset = 0; |
| 434 | |
| 435 | // Do all but one copies using the full register width. |
| 436 | for (unsigned i = 1; i < NumRegs; i++) { |
| 437 | // Load one integer register's worth from the stack slot. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 438 | SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, NULL, 0); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 439 | // Store it to the final location. Remember the store. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 440 | Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 441 | ST->getSrcValue(), SVOffset + Offset, |
| 442 | ST->isVolatile(), |
| 443 | MinAlign(ST->getAlignment(), Offset))); |
| 444 | // Increment the pointers. |
| 445 | Offset += RegBytes; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 446 | StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 447 | Increment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 448 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 451 | // The last store may be partial. Do a truncating store. On big-endian |
| 452 | // machines this requires an extending load from the stack slot to ensure |
| 453 | // that the bits are in the right place. |
| 454 | MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset)); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 455 | |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 456 | // Load from the stack slot. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 457 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr, |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 458 | NULL, 0, MemVT); |
| 459 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 460 | Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 461 | ST->getSrcValue(), SVOffset + Offset, |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 462 | MemVT, ST->isVolatile(), |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 463 | MinAlign(ST->getAlignment(), Offset))); |
| 464 | // The order of the stores doesn't matter - say it with a TokenFactor. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 465 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 466 | Stores.size()); |
| 467 | } |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 468 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 469 | assert(ST->getMemoryVT().isInteger() && |
| 470 | !ST->getMemoryVT().isVector() && |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 471 | "Unaligned store of unknown type."); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 472 | // Get the half-size VT |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 473 | MVT NewStoredVT = |
| 474 | (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1); |
| 475 | int NumBits = NewStoredVT.getSizeInBits(); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 476 | int IncrementSize = NumBits / 8; |
| 477 | |
| 478 | // Divide the stored value in two parts. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 479 | SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 480 | SDValue Lo = Val; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 481 | SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 482 | |
| 483 | // Store the two parts |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 484 | SDValue Store1, Store2; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 485 | Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 486 | ST->getSrcValue(), SVOffset, NewStoredVT, |
| 487 | ST->isVolatile(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 488 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 489 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 490 | Alignment = MinAlign(Alignment, IncrementSize); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 491 | Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 492 | ST->getSrcValue(), SVOffset + IncrementSize, |
| 493 | NewStoredVT, ST->isVolatile(), Alignment); |
| 494 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 495 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. |
| 499 | static |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 500 | SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 501 | const TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 502 | int SVOffset = LD->getSrcValueOffset(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 503 | SDValue Chain = LD->getChain(); |
| 504 | SDValue Ptr = LD->getBasePtr(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 505 | MVT VT = LD->getValueType(0); |
| 506 | MVT LoadedVT = LD->getMemoryVT(); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 507 | DebugLoc dl = LD->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 508 | if (VT.isFloatingPoint() || VT.isVector()) { |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 509 | MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits()); |
| 510 | if (TLI.isTypeLegal(intVT)) { |
| 511 | // Expand to a (misaligned) integer load of the same size, |
| 512 | // then bitconvert to floating point or vector. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 513 | SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getSrcValue(), |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 514 | SVOffset, LD->isVolatile(), |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 515 | LD->getAlignment()); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 516 | SDValue Result = DAG.getNode(ISD::BIT_CONVERT, dl, LoadedVT, newLoad); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 517 | if (VT.isFloatingPoint() && LoadedVT != VT) |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 518 | Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 519 | |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 520 | SDValue Ops[] = { Result, Chain }; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 521 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 522 | } else { |
| 523 | // Copy the value to a (aligned) stack slot using (unaligned) integer |
| 524 | // loads and stores, then do a (aligned) load from the stack slot. |
| 525 | MVT RegVT = TLI.getRegisterType(intVT); |
| 526 | unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8; |
| 527 | unsigned RegBytes = RegVT.getSizeInBits() / 8; |
| 528 | unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; |
| 529 | |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 530 | // Make sure the stack slot is also aligned for the register type. |
| 531 | SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); |
| 532 | |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 533 | SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); |
| 534 | SmallVector<SDValue, 8> Stores; |
| 535 | SDValue StackPtr = StackBase; |
| 536 | unsigned Offset = 0; |
| 537 | |
| 538 | // Do all but one copies using the full register width. |
| 539 | for (unsigned i = 1; i < NumRegs; i++) { |
| 540 | // Load one integer register's worth from the original location. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 541 | SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, LD->getSrcValue(), |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 542 | SVOffset + Offset, LD->isVolatile(), |
| 543 | MinAlign(LD->getAlignment(), Offset)); |
| 544 | // Follow the load with a store to the stack slot. Remember the store. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 545 | Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 546 | NULL, 0)); |
| 547 | // Increment the pointers. |
| 548 | Offset += RegBytes; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 549 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 550 | StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 551 | Increment); |
| 552 | } |
| 553 | |
| 554 | // The last copy may be partial. Do an extending load. |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 555 | MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset)); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 556 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 557 | LD->getSrcValue(), SVOffset + Offset, |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 558 | MemVT, LD->isVolatile(), |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 559 | MinAlign(LD->getAlignment(), Offset)); |
| 560 | // Follow the load with a store to the stack slot. Remember the store. |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 561 | // On big-endian machines this requires a truncating store to ensure |
| 562 | // that the bits end up in the right place. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 563 | Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr, |
Duncan Sands | fd6673c | 2008-12-13 07:18:38 +0000 | [diff] [blame] | 564 | NULL, 0, MemVT)); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 565 | |
| 566 | // The order of the stores doesn't matter - say it with a TokenFactor. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 567 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 568 | Stores.size()); |
| 569 | |
| 570 | // Finally, perform the original load only redirected to the stack slot. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 571 | Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 572 | NULL, 0, LoadedVT); |
| 573 | |
| 574 | // Callers expect a MERGE_VALUES node. |
| 575 | SDValue Ops[] = { Load, TF }; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 576 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 577 | } |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 578 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 579 | assert(LoadedVT.isInteger() && !LoadedVT.isVector() && |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 580 | "Unaligned load of unsupported type."); |
| 581 | |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 582 | // Compute the new VT that is half the size of the old one. This is an |
| 583 | // integer MVT. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 584 | unsigned NumBits = LoadedVT.getSizeInBits(); |
| 585 | MVT NewLoadedVT; |
| 586 | NewLoadedVT = MVT::getIntegerVT(NumBits/2); |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 587 | NumBits >>= 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 588 | |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 589 | unsigned Alignment = LD->getAlignment(); |
| 590 | unsigned IncrementSize = NumBits / 8; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 591 | ISD::LoadExtType HiExtType = LD->getExtensionType(); |
| 592 | |
| 593 | // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. |
| 594 | if (HiExtType == ISD::NON_EXTLOAD) |
| 595 | HiExtType = ISD::ZEXTLOAD; |
| 596 | |
| 597 | // Load the value in two parts |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 598 | SDValue Lo, Hi; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 599 | if (TLI.isLittleEndian()) { |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 600 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(), |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 601 | SVOffset, NewLoadedVT, LD->isVolatile(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 602 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 603 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 604 | Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(), |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 605 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 606 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 607 | } else { |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 608 | Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getSrcValue(), |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 609 | SVOffset, NewLoadedVT, LD->isVolatile(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 610 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 611 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 612 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getSrcValue(), |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 613 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 614 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // aggregate the two parts |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 618 | SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 619 | SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); |
| 620 | Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 621 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 622 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 623 | Hi.getValue(1)); |
| 624 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 625 | SDValue Ops[] = { Result, TF }; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 626 | return DAG.getMergeValues(Ops, 2, dl); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 627 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 628 | |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 629 | /// GetFPLibCall - Return the right libcall for the given floating point type. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 630 | static RTLIB::Libcall GetFPLibCall(MVT VT, |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 631 | RTLIB::Libcall Call_F32, |
| 632 | RTLIB::Libcall Call_F64, |
| 633 | RTLIB::Libcall Call_F80, |
| 634 | RTLIB::Libcall Call_PPCF128) { |
| 635 | return |
| 636 | VT == MVT::f32 ? Call_F32 : |
| 637 | VT == MVT::f64 ? Call_F64 : |
| 638 | VT == MVT::f80 ? Call_F80 : |
| 639 | VT == MVT::ppcf128 ? Call_PPCF128 : |
| 640 | RTLIB::UNKNOWN_LIBCALL; |
| 641 | } |
| 642 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 643 | /// PerformInsertVectorEltInMemory - Some target cannot handle a variable |
| 644 | /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it |
| 645 | /// is necessary to spill the vector being inserted into to memory, perform |
| 646 | /// the insert there, and then read the result back. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 647 | SDValue SelectionDAGLegalize:: |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 648 | PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, |
| 649 | DebugLoc dl) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 650 | SDValue Tmp1 = Vec; |
| 651 | SDValue Tmp2 = Val; |
| 652 | SDValue Tmp3 = Idx; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 653 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 654 | // If the target doesn't support this, we have to spill the input vector |
| 655 | // to a temporary stack slot, update the element, then reload it. This is |
| 656 | // badness. We could also load the value into a vector register (either |
| 657 | // with a "move to register" or "extload into register" instruction, then |
| 658 | // permute it into place, if the idx is a constant and if the idx is |
| 659 | // supported by the target. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 660 | MVT VT = Tmp1.getValueType(); |
| 661 | MVT EltVT = VT.getVectorElementType(); |
| 662 | MVT IdxVT = Tmp3.getValueType(); |
| 663 | MVT PtrVT = TLI.getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 664 | SDValue StackPtr = DAG.CreateStackTemporary(VT); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 665 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 666 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 667 | |
| 668 | // Store the vector. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 669 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr, |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 670 | PseudoSourceValue::getFixedStack(SPFI), 0); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 671 | |
| 672 | // Truncate or zero extend offset to target pointer type. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 673 | unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 674 | Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 675 | // Add the offset to the index. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 676 | unsigned EltSize = EltVT.getSizeInBits()/8; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 677 | Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 678 | SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 679 | // Store the scalar value. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 680 | Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 681 | PseudoSourceValue::getFixedStack(SPFI), 0, EltVT); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 682 | // Load the updated vector. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 683 | return DAG.getLoad(VT, dl, Ch, StackPtr, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 684 | PseudoSourceValue::getFixedStack(SPFI), 0); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Mon P Wang | e9f1015 | 2008-12-09 05:46:39 +0000 | [diff] [blame] | 687 | |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame^] | 688 | SDValue SelectionDAGLegalize:: |
| 689 | ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) { |
| 690 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { |
| 691 | // SCALAR_TO_VECTOR requires that the type of the value being inserted |
| 692 | // match the element type of the vector being created, except for |
| 693 | // integers in which case the inserted value can be over width. |
| 694 | MVT EltVT = Vec.getValueType().getVectorElementType(); |
| 695 | if (Val.getValueType() == EltVT || |
| 696 | (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { |
| 697 | SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| 698 | Vec.getValueType(), Val); |
| 699 | |
| 700 | unsigned NumElts = Vec.getValueType().getVectorNumElements(); |
| 701 | // We generate a shuffle of InVec and ScVec, so the shuffle mask |
| 702 | // should be 0,1,2,3,4,5... with the appropriate element replaced with |
| 703 | // elt 0 of the RHS. |
| 704 | SmallVector<int, 8> ShufOps; |
| 705 | for (unsigned i = 0; i != NumElts; ++i) |
| 706 | ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); |
| 707 | |
| 708 | return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, |
| 709 | &ShufOps[0]); |
| 710 | } |
| 711 | } |
| 712 | return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); |
| 713 | } |
| 714 | |
Dan Gohman | a346615 | 2007-07-13 20:14:11 +0000 | [diff] [blame] | 715 | /// LegalizeOp - We know that the specified value has a legal type, and |
| 716 | /// that its operands are legal. Now ensure that the operation itself |
| 717 | /// is legal, recursively ensuring that the operands' operations remain |
| 718 | /// legal. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 719 | SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) { |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 720 | if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. |
| 721 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 722 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 723 | SDNode *Node = Op.getNode(); |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 724 | DebugLoc dl = Node->getDebugLoc(); |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 725 | |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 726 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 727 | assert(getTypeAction(Node->getValueType(i)) == Legal && |
| 728 | "Unexpected illegal type!"); |
| 729 | |
| 730 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 731 | assert((isTypeLegal(Node->getOperand(i).getValueType()) || |
| 732 | Node->getOperand(i).getOpcode() == ISD::TargetConstant) && |
| 733 | "Unexpected illegal type!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 735 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 736 | // means that we always must cache transformed nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 737 | DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 738 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 739 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 740 | SDValue Tmp1, Tmp2, Tmp3, Tmp4; |
| 741 | SDValue Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 742 | bool isCustom = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 743 | |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 744 | // Figure out the correct action; the way to query this varies by opcode |
| 745 | TargetLowering::LegalizeAction Action; |
| 746 | bool SimpleFinishLegalizing = true; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 747 | switch (Node->getOpcode()) { |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 748 | case ISD::INTRINSIC_W_CHAIN: |
| 749 | case ISD::INTRINSIC_WO_CHAIN: |
| 750 | case ISD::INTRINSIC_VOID: |
| 751 | case ISD::VAARG: |
| 752 | case ISD::STACKSAVE: |
| 753 | Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); |
| 754 | break; |
| 755 | case ISD::SINT_TO_FP: |
| 756 | case ISD::UINT_TO_FP: |
| 757 | case ISD::EXTRACT_VECTOR_ELT: |
| 758 | Action = TLI.getOperationAction(Node->getOpcode(), |
| 759 | Node->getOperand(0).getValueType()); |
| 760 | break; |
| 761 | case ISD::FP_ROUND_INREG: |
| 762 | case ISD::SIGN_EXTEND_INREG: { |
| 763 | MVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
| 764 | Action = TLI.getOperationAction(Node->getOpcode(), InnerType); |
| 765 | break; |
| 766 | } |
| 767 | case ISD::LOAD: |
| 768 | case ISD::STORE: |
| 769 | case ISD::BR_CC: |
| 770 | case ISD::FORMAL_ARGUMENTS: |
| 771 | case ISD::CALL: |
| 772 | case ISD::CALLSEQ_START: |
| 773 | case ISD::CALLSEQ_END: |
| 774 | case ISD::SELECT_CC: |
| 775 | case ISD::SETCC: |
| 776 | case ISD::EXCEPTIONADDR: |
| 777 | case ISD::EHSELECTION: |
| 778 | // These instructions have properties that aren't modeled in the |
| 779 | // generic codepath |
| 780 | SimpleFinishLegalizing = false; |
| 781 | break; |
| 782 | case ISD::EXTRACT_ELEMENT: |
| 783 | case ISD::FLT_ROUNDS_: |
| 784 | case ISD::SADDO: |
| 785 | case ISD::SSUBO: |
| 786 | case ISD::UADDO: |
| 787 | case ISD::USUBO: |
| 788 | case ISD::SMULO: |
| 789 | case ISD::UMULO: |
| 790 | case ISD::FPOWI: |
| 791 | case ISD::MERGE_VALUES: |
| 792 | case ISD::EH_RETURN: |
| 793 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 794 | Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); |
| 795 | if (Action == TargetLowering::Legal) |
| 796 | Action = TargetLowering::Expand; |
| 797 | break; |
| 798 | case ISD::TRAMPOLINE: |
| 799 | case ISD::FRAMEADDR: |
| 800 | case ISD::RETURNADDR: |
| 801 | Action = TargetLowering::Custom; |
| 802 | break; |
| 803 | case ISD::BUILD_VECTOR: |
| 804 | // A weird case: when a BUILD_VECTOR is custom-lowered, it doesn't legalize |
| 805 | // its operands first! |
| 806 | SimpleFinishLegalizing = false; |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 807 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 808 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 809 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 810 | Action = TargetLowering::Legal; |
| 811 | } else { |
| 812 | Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 813 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | |
| 817 | if (SimpleFinishLegalizing) { |
| 818 | SmallVector<SDValue, 8> Ops, ResultVals; |
| 819 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 820 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 821 | switch (Node->getOpcode()) { |
| 822 | default: break; |
| 823 | case ISD::BR: |
| 824 | case ISD::BRIND: |
| 825 | case ISD::BR_JT: |
| 826 | case ISD::BR_CC: |
| 827 | case ISD::BRCOND: |
| 828 | case ISD::RET: |
| 829 | // Branches tweak the chain to include LastCALLSEQ_END |
| 830 | Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0], |
| 831 | LastCALLSEQ_END); |
| 832 | Ops[0] = LegalizeOp(Ops[0]); |
| 833 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 834 | break; |
| 835 | case ISD::SHL: |
| 836 | case ISD::SRL: |
| 837 | case ISD::SRA: |
| 838 | case ISD::ROTL: |
| 839 | case ISD::ROTR: |
| 840 | // Legalizing shifts/rotates requires adjusting the shift amount |
| 841 | // to the appropriate width. |
| 842 | if (!Ops[1].getValueType().isVector()) |
| 843 | Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[1])); |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | Result = DAG.UpdateNodeOperands(Result.getValue(0), Ops.data(), |
| 848 | Ops.size()); |
| 849 | switch (Action) { |
| 850 | case TargetLowering::Legal: |
| 851 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 852 | ResultVals.push_back(Result.getValue(i)); |
| 853 | break; |
| 854 | case TargetLowering::Custom: |
| 855 | // FIXME: The handling for custom lowering with multiple results is |
| 856 | // a complete mess. |
| 857 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 858 | if (Tmp1.getNode()) { |
| 859 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
| 860 | if (e == 1) |
| 861 | ResultVals.push_back(Tmp1); |
| 862 | else |
| 863 | ResultVals.push_back(Tmp1.getValue(i)); |
| 864 | } |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | // FALL THROUGH |
| 869 | case TargetLowering::Expand: |
| 870 | ExpandNode(Result.getNode(), ResultVals); |
| 871 | break; |
| 872 | case TargetLowering::Promote: |
| 873 | PromoteNode(Result.getNode(), ResultVals); |
| 874 | break; |
| 875 | } |
| 876 | if (!ResultVals.empty()) { |
| 877 | for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) { |
| 878 | if (ResultVals[i] != SDValue(Node, i)) |
| 879 | ResultVals[i] = LegalizeOp(ResultVals[i]); |
| 880 | AddLegalizedOperand(SDValue(Node, i), ResultVals[i]); |
| 881 | } |
| 882 | return ResultVals[Op.getResNo()]; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | switch (Node->getOpcode()) { |
| 887 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 888 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 889 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 890 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 891 | assert(0 && "Do not know how to legalize this operator!"); |
| 892 | abort(); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 893 | case ISD::EXCEPTIONADDR: { |
| 894 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 895 | MVT VT = Node->getValueType(0); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 896 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 897 | default: assert(0 && "This action is not supported yet!"); |
| 898 | case TargetLowering::Expand: { |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 899 | unsigned Reg = TLI.getExceptionAddressRegister(); |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 900 | Result = DAG.getCopyFromReg(Tmp1, dl, Reg, VT); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 901 | } |
| 902 | break; |
| 903 | case TargetLowering::Custom: |
| 904 | Result = TLI.LowerOperation(Op, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 905 | if (Result.getNode()) break; |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 906 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 907 | case TargetLowering::Legal: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 908 | SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 }; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 909 | Result = DAG.getMergeValues(Ops, 2, dl); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
| 912 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 913 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 914 | if (Result.getNode()->getNumValues() == 1) break; |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 915 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 916 | assert(Result.getNode()->getNumValues() == 2 && |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 917 | "Cannot return more than two values!"); |
| 918 | |
| 919 | // Since we produced two values, make sure to remember that we |
| 920 | // legalized both of them. |
| 921 | Tmp1 = LegalizeOp(Result); |
| 922 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 923 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 924 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 925 | return Op.getResNo() ? Tmp2 : Tmp1; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 926 | case ISD::EHSELECTION: { |
| 927 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 928 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 929 | MVT VT = Node->getValueType(0); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 930 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 931 | default: assert(0 && "This action is not supported yet!"); |
| 932 | case TargetLowering::Expand: { |
| 933 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 934 | Result = DAG.getCopyFromReg(Tmp2, dl, Reg, VT); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 935 | } |
| 936 | break; |
| 937 | case TargetLowering::Custom: |
| 938 | Result = TLI.LowerOperation(Op, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 939 | if (Result.getNode()) break; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 940 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 941 | case TargetLowering::Legal: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 942 | SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 }; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 943 | Result = DAG.getMergeValues(Ops, 2, dl); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 947 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 948 | if (Result.getNode()->getNumValues() == 1) break; |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 949 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 950 | assert(Result.getNode()->getNumValues() == 2 && |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 951 | "Cannot return more than two values!"); |
| 952 | |
| 953 | // Since we produced two values, make sure to remember that we |
| 954 | // legalized both of them. |
| 955 | Tmp1 = LegalizeOp(Result); |
| 956 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 957 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 958 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 959 | return Op.getResNo() ? Tmp2 : Tmp1; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 960 | |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 961 | case ISD::DBG_STOPPOINT: |
| 962 | assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!"); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 963 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 964 | |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 965 | switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) { |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 966 | case TargetLowering::Promote: |
| 967 | default: assert(0 && "This action is not supported yet!"); |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 968 | case TargetLowering::Expand: { |
| 969 | DwarfWriter *DW = DAG.getDwarfWriter(); |
| 970 | bool useDEBUG_LOC = TLI.isOperationLegalOrCustom(ISD::DEBUG_LOC, |
| 971 | MVT::Other); |
| 972 | bool useLABEL = TLI.isOperationLegalOrCustom(ISD::DBG_LABEL, MVT::Other); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 973 | |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 974 | const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node); |
| 975 | GlobalVariable *CU_GV = cast<GlobalVariable>(DSP->getCompileUnit()); |
| 976 | if (DW && (useDEBUG_LOC || useLABEL) && !CU_GV->isDeclaration()) { |
| 977 | DICompileUnit CU(cast<GlobalVariable>(DSP->getCompileUnit())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 978 | |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 979 | unsigned Line = DSP->getLine(); |
| 980 | unsigned Col = DSP->getColumn(); |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 981 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 982 | if (OptLevel == CodeGenOpt::None) { |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 983 | // A bit self-referential to have DebugLoc on Debug_Loc nodes, but it |
| 984 | // won't hurt anything. |
| 985 | if (useDEBUG_LOC) { |
| 986 | SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32), |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 987 | DAG.getConstant(Col, MVT::i32), |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 988 | DAG.getSrcValue(CU.getGV()) }; |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 989 | Result = DAG.getNode(ISD::DEBUG_LOC, dl, MVT::Other, Ops, 4); |
| 990 | } else { |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 991 | unsigned ID = DW->RecordSourceLine(Line, Col, CU); |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 992 | Result = DAG.getLabel(ISD::DBG_LABEL, dl, Tmp1, ID); |
| 993 | } |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 994 | } else { |
Bill Wendling | 86e6cb9 | 2009-02-17 01:04:54 +0000 | [diff] [blame] | 995 | Result = Tmp1; // chain |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 996 | } |
| 997 | } else { |
| 998 | Result = Tmp1; // chain |
| 999 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1000 | break; |
Bill Wendling | 92c1e12 | 2009-02-13 02:16:35 +0000 | [diff] [blame] | 1001 | } |
Sanjiv Gupta | 5274a4a | 2009-04-02 18:03:10 +0000 | [diff] [blame] | 1002 | case TargetLowering::Custom: |
| 1003 | Result = TLI.LowerOperation(Op, DAG); |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 1004 | if (Result.getNode()) |
Sanjiv Gupta | 5274a4a | 2009-04-02 18:03:10 +0000 | [diff] [blame] | 1005 | break; |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 1006 | case TargetLowering::Legal: { |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 1007 | if (Tmp1 == Node->getOperand(0)) |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 1008 | break; |
| 1009 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1010 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 1011 | Ops.push_back(Tmp1); |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 1012 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 1013 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 1014 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 1015 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
| 1016 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1017 | break; |
| 1018 | } |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 1019 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1020 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1021 | case ISD::ConstantFP: { |
| 1022 | // Spill FP immediates to the constant pool if the target cannot directly |
| 1023 | // codegen them. Targets often have some immediate values that can be |
| 1024 | // efficiently generated into an FP register without a load. We explicitly |
| 1025 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1026 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 1027 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1028 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 1029 | default: assert(0 && "This action is not supported yet!"); |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1030 | case TargetLowering::Legal: |
| 1031 | break; |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1032 | case TargetLowering::Custom: |
| 1033 | Tmp3 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1034 | if (Tmp3.getNode()) { |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1035 | Result = Tmp3; |
| 1036 | break; |
| 1037 | } |
| 1038 | // FALLTHROUGH |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1039 | case TargetLowering::Expand: { |
| 1040 | // Check to see if this FP immediate is already legal. |
| 1041 | bool isLegal = false; |
| 1042 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 1043 | E = TLI.legal_fpimm_end(); I != E; ++I) { |
| 1044 | if (CFP->isExactlyValue(*I)) { |
| 1045 | isLegal = true; |
| 1046 | break; |
| 1047 | } |
| 1048 | } |
| 1049 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 1050 | if (isLegal) |
| 1051 | break; |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 1052 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1053 | } |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1054 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | } |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1057 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1058 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1059 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1060 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1061 | assert(Tmp3.getNode() && "Target didn't custom lower this node!"); |
Dale Johannesen | 0ea0356 | 2008-03-05 19:14:03 +0000 | [diff] [blame] | 1062 | // A call within a calling sequence must be legalized to something |
| 1063 | // other than the normal CALLSEQ_END. Violating this gets Legalize |
| 1064 | // into an infinite loop. |
| 1065 | assert ((!IsLegalizingCall || |
| 1066 | Node->getOpcode() != ISD::CALL || |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1067 | Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) && |
Dale Johannesen | 0ea0356 | 2008-03-05 19:14:03 +0000 | [diff] [blame] | 1068 | "Nested CALLSEQ_START..CALLSEQ_END not supported."); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1069 | |
| 1070 | // The number of incoming and outgoing values should match; unless the final |
| 1071 | // outgoing value is a flag. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1072 | assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() || |
| 1073 | (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 && |
| 1074 | Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) == |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1075 | MVT::Flag)) && |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1076 | "Lowering call/formal_arguments produced unexpected # results!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1078 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1079 | // remember that we legalized all of them, so it doesn't get relegalized. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1080 | for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) { |
| 1081 | if (Tmp3.getNode()->getValueType(i) == MVT::Flag) |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1082 | continue; |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1083 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1084 | if (Op.getResNo() == i) |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1085 | Tmp2 = Tmp1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1086 | AddLegalizedOperand(SDValue(Node, i), Tmp1); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1087 | } |
| 1088 | return Tmp2; |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1089 | case ISD::BUILD_VECTOR: |
| 1090 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1091 | default: assert(0 && "This action is not supported yet!"); |
| 1092 | case TargetLowering::Custom: |
| 1093 | Tmp3 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1094 | if (Tmp3.getNode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1095 | Result = Tmp3; |
| 1096 | break; |
| 1097 | } |
| 1098 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1099 | case TargetLowering::Expand: |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1100 | Result = ExpandBUILD_VECTOR(Result.getNode()); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1101 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1102 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1103 | break; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1104 | case ISD::VECTOR_SHUFFLE: { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1105 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1106 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1107 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1108 | MVT VT = Result.getValueType(); |
| 1109 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1110 | // Copy the Mask to a local SmallVector for use with isShuffleMaskLegal. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1111 | SmallVector<int, 8> Mask; |
| 1112 | cast<ShuffleVectorSDNode>(Result)->getMask(Mask); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1113 | |
| 1114 | // Allow targets to custom lower the SHUFFLEs they support. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1115 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1116 | default: assert(0 && "Unknown operation action!"); |
| 1117 | case TargetLowering::Legal: |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1118 | assert(TLI.isShuffleMaskLegal(Mask, VT) && |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1119 | "vector shuffle should not be created if not legal!"); |
| 1120 | break; |
| 1121 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1122 | Tmp3 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1123 | if (Tmp3.getNode()) { |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1124 | Result = Tmp3; |
| 1125 | break; |
| 1126 | } |
| 1127 | // FALLTHROUGH |
| 1128 | case TargetLowering::Expand: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1129 | MVT EltVT = VT.getVectorElementType(); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1130 | unsigned NumElems = VT.getVectorNumElements(); |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 1131 | SmallVector<SDValue, 8> Ops; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1132 | for (unsigned i = 0; i != NumElems; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1133 | if (Mask[i] < 0) { |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1134 | Ops.push_back(DAG.getUNDEF(EltVT)); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1135 | continue; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1136 | } |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1137 | unsigned Idx = Mask[i]; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1138 | if (Idx < NumElems) |
| 1139 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp1, |
| 1140 | DAG.getIntPtrConstant(Idx))); |
| 1141 | else |
| 1142 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Tmp2, |
| 1143 | DAG.getIntPtrConstant(Idx - NumElems))); |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1144 | } |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 1145 | Result = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1146 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1147 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1148 | case TargetLowering::Promote: { |
| 1149 | // Change base type to a different vector type. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1150 | MVT OVT = Node->getValueType(0); |
| 1151 | MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1152 | |
| 1153 | // Cast the two input vectors. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1154 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1); |
| 1155 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1157 | // Convert the shuffle mask to the right # elements. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1158 | Result = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1159 | Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1162 | } |
| 1163 | break; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1164 | } |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 1165 | case ISD::CONCAT_VECTORS: { |
Bob Wilson | 5ee24e5 | 2009-05-01 17:55:32 +0000 | [diff] [blame] | 1166 | // Legalize the operands. |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 1167 | SmallVector<SDValue, 8> Ops; |
Bob Wilson | 5ee24e5 | 2009-05-01 17:55:32 +0000 | [diff] [blame] | 1168 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1169 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1170 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
| 1171 | |
| 1172 | switch (TLI.getOperationAction(ISD::CONCAT_VECTORS, |
| 1173 | Node->getValueType(0))) { |
| 1174 | default: assert(0 && "Unknown operation action!"); |
| 1175 | case TargetLowering::Legal: |
| 1176 | break; |
| 1177 | case TargetLowering::Custom: |
| 1178 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1179 | if (Tmp3.getNode()) { |
| 1180 | Result = Tmp3; |
| 1181 | break; |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 1182 | } |
Bob Wilson | 5ee24e5 | 2009-05-01 17:55:32 +0000 | [diff] [blame] | 1183 | // FALLTHROUGH |
| 1184 | case TargetLowering::Expand: { |
| 1185 | // Use extract/insert/build vector for now. We might try to be |
| 1186 | // more clever later. |
| 1187 | MVT PtrVT = TLI.getPointerTy(); |
| 1188 | SmallVector<SDValue, 8> Ops; |
| 1189 | unsigned NumOperands = Node->getNumOperands(); |
| 1190 | for (unsigned i=0; i < NumOperands; ++i) { |
| 1191 | SDValue SubOp = Node->getOperand(i); |
| 1192 | MVT VVT = SubOp.getNode()->getValueType(0); |
| 1193 | MVT EltVT = VVT.getVectorElementType(); |
| 1194 | unsigned NumSubElem = VVT.getVectorNumElements(); |
| 1195 | for (unsigned j=0; j < NumSubElem; ++j) { |
| 1196 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, SubOp, |
| 1197 | DAG.getConstant(j, PtrVT))); |
| 1198 | } |
| 1199 | } |
| 1200 | return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 1201 | Node->getValueType(0), |
| 1202 | &Ops[0], Ops.size())); |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 1203 | } |
Bob Wilson | 5ee24e5 | 2009-05-01 17:55:32 +0000 | [diff] [blame] | 1204 | } |
| 1205 | break; |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1208 | case ISD::CALLSEQ_START: { |
| 1209 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1211 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1212 | // to this call start. This ensures that any libcalls that need be inserted |
| 1213 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1214 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1215 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1216 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node, |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1217 | NodesLeadingTo); |
| 1218 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Now that we legalized all of the inputs (which may have inserted |
| 1221 | // libcalls) create the new CALLSEQ_START node. |
| 1222 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1223 | |
| 1224 | // Merge in the last call, to ensure that this call start after the last |
| 1225 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1226 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1227 | Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1228 | Tmp1, LastCALLSEQ_END); |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1229 | Tmp1 = LegalizeOp(Tmp1); |
| 1230 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1232 | // Do not try to legalize the target-specific arguments (#1+). |
| 1233 | if (Tmp1 != Node->getOperand(0)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1234 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1235 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1236 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1237 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1239 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1240 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1241 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1242 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1244 | // Now that the callseq_start and all of the non-call nodes above this call |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1245 | // sequence have been legalized, legalize the call itself. During this |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1246 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1247 | // can overlap. |
| 1248 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1249 | // Note that we are selecting this call! |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1250 | LastCALLSEQ_END = SDValue(CallEnd, 0); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1251 | IsLegalizingCall = true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1253 | // Legalize the call, starting from the CALLSEQ_END. |
| 1254 | LegalizeOp(LastCALLSEQ_END); |
| 1255 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1256 | return Result; |
| 1257 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1258 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1259 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1260 | // will cause this node to be legalized as well as handling libcalls right. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1261 | if (LastCALLSEQ_END.getNode() != Node) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1262 | LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0)); |
| 1263 | DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1264 | assert(I != LegalizedNodes.end() && |
| 1265 | "Legalizing the call start should have legalized this node!"); |
| 1266 | return I->second; |
| 1267 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1268 | |
| 1269 | // Otherwise, the call start has been legalized and everything is going |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1270 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1271 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1272 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1273 | // an optional flag input. |
| 1274 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1275 | if (Tmp1 != Node->getOperand(0)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1276 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1277 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1278 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } else { |
| 1281 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1282 | if (Tmp1 != Node->getOperand(0) || |
| 1283 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1284 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1285 | Ops[0] = Tmp1; |
| 1286 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1287 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1288 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1289 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1290 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1291 | // This finishes up call legalization. |
| 1292 | IsLegalizingCall = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1294 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1295 | AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0)); |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1296 | if (Node->getNumValues() == 2) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1297 | AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1)); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1298 | return Result.getValue(Op.getResNo()); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1299 | case ISD::DYNAMIC_STACKALLOC: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1300 | MVT VT = Node->getValueType(0); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1301 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1302 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1303 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1304 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1306 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1307 | Tmp2 = Result.getValue(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1308 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1309 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1310 | case TargetLowering::Expand: { |
| 1311 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1312 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1313 | " not tell us which reg is the stack pointer!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1314 | SDValue Chain = Tmp1.getOperand(0); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1315 | |
| 1316 | // Chain the dynamic stack allocation so that it doesn't modify the stack |
| 1317 | // pointer when other instructions are using the stack. |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 1318 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1319 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1320 | SDValue Size = Tmp2.getOperand(1); |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 1321 | SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1322 | Chain = SP.getValue(1); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1323 | unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue(); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1324 | unsigned StackAlign = |
| 1325 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1326 | if (Align > StackAlign) |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1327 | SP = DAG.getNode(ISD::AND, dl, VT, SP, |
Evan Cheng | 3e20bba | 2007-08-17 18:02:22 +0000 | [diff] [blame] | 1328 | DAG.getConstant(-(uint64_t)Align, VT)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1329 | Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 1330 | Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1331 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1332 | Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true), |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 1333 | DAG.getIntPtrConstant(0, true), SDValue()); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1335 | Tmp1 = LegalizeOp(Tmp1); |
| 1336 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1340 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1341 | if (Tmp3.getNode()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1342 | Tmp1 = LegalizeOp(Tmp3); |
| 1343 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1344 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1345 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1346 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1347 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1348 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1349 | // Since this op produce two values, make sure to remember that we |
| 1350 | // legalized both of them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1351 | AddLegalizedOperand(SDValue(Node, 0), Tmp1); |
| 1352 | AddLegalizedOperand(SDValue(Node, 1), Tmp2); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1353 | return Op.getResNo() ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1354 | } |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1355 | case ISD::BR_JT: |
| 1356 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1357 | // Ensure that libcalls are emitted before a branch. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1358 | Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1359 | Tmp1 = LegalizeOp(Tmp1); |
| 1360 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1361 | |
| 1362 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1363 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1364 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1365 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1366 | default: assert(0 && "This action is not supported yet!"); |
| 1367 | case TargetLowering::Legal: break; |
| 1368 | case TargetLowering::Custom: |
| 1369 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1370 | if (Tmp1.getNode()) Result = Tmp1; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1371 | break; |
| 1372 | case TargetLowering::Expand: { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1373 | SDValue Chain = Result.getOperand(0); |
| 1374 | SDValue Table = Result.getOperand(1); |
| 1375 | SDValue Index = Result.getOperand(2); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1376 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1377 | MVT PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1378 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1379 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1380 | Index= DAG.getNode(ISD::MUL, dl, PTy, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1381 | Index, DAG.getConstant(EntrySize, PTy)); |
| 1382 | SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1383 | |
Duncan Sands | 712f7b3 | 2008-12-12 08:13:38 +0000 | [diff] [blame] | 1384 | MVT MemVT = MVT::getIntegerVT(EntrySize * 8); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1385 | SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr, |
Duncan Sands | 712f7b3 | 2008-12-12 08:13:38 +0000 | [diff] [blame] | 1386 | PseudoSourceValue::getJumpTable(), 0, MemVT); |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1387 | Addr = LD; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1388 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1389 | // For PIC, the sequence is: |
| 1390 | // BRIND(load(Jumptable + index) + RelocBase) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1391 | // RelocBase can be JumpTable, GOT or some sort of global base. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1392 | Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1393 | TLI.getPICJumpTableRelocBase(Table, DAG)); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1394 | } |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1395 | Result = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1399 | case ISD::BRCOND: |
| 1400 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1401 | // Ensure that libcalls are emitted before a return. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1402 | Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1403 | Tmp1 = LegalizeOp(Tmp1); |
| 1404 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1405 | |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1406 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1409 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1410 | |
| 1411 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1412 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1413 | case TargetLowering::Legal: break; |
| 1414 | case TargetLowering::Custom: |
| 1415 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1416 | if (Tmp1.getNode()) Result = Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1417 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1418 | case TargetLowering::Expand: |
| 1419 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1420 | // Node. |
| 1421 | if (Tmp2.getOpcode() == ISD::SETCC) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1422 | Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1423 | Tmp1, Tmp2.getOperand(2), |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1424 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1425 | Node->getOperand(2)); |
| 1426 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1427 | Result = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1428 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1429 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1430 | Node->getOperand(2)); |
| 1431 | } |
| 1432 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1433 | } |
| 1434 | break; |
| 1435 | case ISD::BR_CC: |
| 1436 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1437 | // Ensure that libcalls are emitted before a branch. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1438 | Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1, LastCALLSEQ_END); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1439 | Tmp1 = LegalizeOp(Tmp1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1440 | Tmp2 = Node->getOperand(2); // LHS |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1441 | Tmp3 = Node->getOperand(3); // RHS |
| 1442 | Tmp4 = Node->getOperand(1); // CC |
| 1443 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1444 | LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 1445 | Tmp2, Tmp3, Tmp4, dl); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1446 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1447 | |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1448 | // If we didn't get both a LHS and RHS back from LegalizeSetCC, |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1449 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1450 | // the result against zero to select between true and false values. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1451 | if (Tmp3.getNode() == 0) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1452 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1453 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1454 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1455 | |
| 1456 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1457 | Node->getOperand(4)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1459 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1460 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1461 | case TargetLowering::Legal: break; |
| 1462 | case TargetLowering::Custom: |
| 1463 | Tmp4 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1464 | if (Tmp4.getNode()) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1465 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1466 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1467 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1468 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1469 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1470 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1471 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1472 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1473 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1474 | if (ExtType == ISD::NON_EXTLOAD) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1475 | MVT VT = Node->getValueType(0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1476 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1477 | Tmp3 = Result.getValue(0); |
| 1478 | Tmp4 = Result.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1479 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1480 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1481 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1482 | case TargetLowering::Legal: |
| 1483 | // If this is an unaligned load and the target doesn't support it, |
| 1484 | // expand it. |
| 1485 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1486 | unsigned ABIAlignment = TLI.getTargetData()-> |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1487 | getABITypeAlignment(LD->getMemoryVT().getTypeForMVT()); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1488 | if (LD->getAlignment() < ABIAlignment){ |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1489 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1490 | TLI); |
| 1491 | Tmp3 = Result.getOperand(0); |
| 1492 | Tmp4 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1493 | Tmp3 = LegalizeOp(Tmp3); |
| 1494 | Tmp4 = LegalizeOp(Tmp4); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1498 | case TargetLowering::Custom: |
| 1499 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1500 | if (Tmp1.getNode()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1501 | Tmp3 = LegalizeOp(Tmp1); |
| 1502 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1503 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | case TargetLowering::Promote: { |
| 1506 | // Only promote a load of vector type to another. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1507 | assert(VT.isVector() && "Cannot promote this load!"); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1508 | // Change base type to a different vector type. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1509 | MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1510 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1511 | Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1512 | LD->getSrcValueOffset(), |
| 1513 | LD->isVolatile(), LD->getAlignment()); |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1514 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, dl, VT, Tmp1)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1515 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1516 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1517 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1518 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1519 | // Since loads produce two values, make sure to remember that we |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1520 | // legalized both of them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1521 | AddLegalizedOperand(SDValue(Node, 0), Tmp3); |
| 1522 | AddLegalizedOperand(SDValue(Node, 1), Tmp4); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1523 | return Op.getResNo() ? Tmp4 : Tmp3; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1524 | } else { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1525 | MVT SrcVT = LD->getMemoryVT(); |
| 1526 | unsigned SrcWidth = SrcVT.getSizeInBits(); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1527 | int SVOffset = LD->getSrcValueOffset(); |
| 1528 | unsigned Alignment = LD->getAlignment(); |
| 1529 | bool isVolatile = LD->isVolatile(); |
| 1530 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1531 | if (SrcWidth != SrcVT.getStoreSizeInBits() && |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1532 | // Some targets pretend to have an i1 loading operation, and actually |
| 1533 | // load an i8. This trick is correct for ZEXTLOAD because the top 7 |
| 1534 | // bits are guaranteed to be zero; it helps the optimizers understand |
| 1535 | // that these bits are zero. It is also useful for EXTLOAD, since it |
| 1536 | // tells the optimizers that those bits are undefined. It would be |
| 1537 | // nice to have an effective generic way of getting these benefits... |
| 1538 | // Until such a way is found, don't insist on promoting i1 here. |
| 1539 | (SrcVT != MVT::i1 || |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 1540 | TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1541 | // Promote to a byte-sized load if not loading an integral number of |
| 1542 | // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1543 | unsigned NewWidth = SrcVT.getStoreSizeInBits(); |
| 1544 | MVT NVT = MVT::getIntegerVT(NewWidth); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1545 | SDValue Ch; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1546 | |
| 1547 | // The extra bits are guaranteed to be zero, since we stored them that |
| 1548 | // way. A zext load from NVT thus automatically gives zext from SrcVT. |
| 1549 | |
| 1550 | ISD::LoadExtType NewExtType = |
| 1551 | ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| 1552 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1553 | Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1554 | Tmp1, Tmp2, LD->getSrcValue(), SVOffset, |
| 1555 | NVT, isVolatile, Alignment); |
| 1556 | |
| 1557 | Ch = Result.getValue(1); // The chain. |
| 1558 | |
| 1559 | if (ExtType == ISD::SEXTLOAD) |
| 1560 | // Having the top bits zero doesn't help when sign extending. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1561 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1562 | Result.getValueType(), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1563 | Result, DAG.getValueType(SrcVT)); |
| 1564 | else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) |
| 1565 | // All the top bits are guaranteed to be zero - inform the optimizers. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1566 | Result = DAG.getNode(ISD::AssertZext, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1567 | Result.getValueType(), Result, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1568 | DAG.getValueType(SrcVT)); |
| 1569 | |
| 1570 | Tmp1 = LegalizeOp(Result); |
| 1571 | Tmp2 = LegalizeOp(Ch); |
| 1572 | } else if (SrcWidth & (SrcWidth - 1)) { |
| 1573 | // If not loading a power-of-2 number of bits, expand as two loads. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1574 | assert(SrcVT.isExtended() && !SrcVT.isVector() && |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1575 | "Unsupported extload!"); |
| 1576 | unsigned RoundWidth = 1 << Log2_32(SrcWidth); |
| 1577 | assert(RoundWidth < SrcWidth); |
| 1578 | unsigned ExtraWidth = SrcWidth - RoundWidth; |
| 1579 | assert(ExtraWidth < RoundWidth); |
| 1580 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 1581 | "Load size not an integral number of bytes!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1582 | MVT RoundVT = MVT::getIntegerVT(RoundWidth); |
| 1583 | MVT ExtraVT = MVT::getIntegerVT(ExtraWidth); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1584 | SDValue Lo, Hi, Ch; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1585 | unsigned IncrementSize; |
| 1586 | |
| 1587 | if (TLI.isLittleEndian()) { |
| 1588 | // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) |
| 1589 | // Load the bottom RoundWidth bits. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1590 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, |
| 1591 | Node->getValueType(0), Tmp1, Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1592 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 1593 | Alignment); |
| 1594 | |
| 1595 | // Load the remaining ExtraWidth bits. |
| 1596 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1597 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1598 | DAG.getIntPtrConstant(IncrementSize)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1599 | Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1600 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 1601 | ExtraVT, isVolatile, |
| 1602 | MinAlign(Alignment, IncrementSize)); |
| 1603 | |
| 1604 | // Build a factor node to remember that this load is independent of the |
| 1605 | // other one. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1606 | Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1607 | Hi.getValue(1)); |
| 1608 | |
| 1609 | // Move the top bits to the right place. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1610 | Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1611 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
| 1612 | |
| 1613 | // Join the hi and lo parts. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1614 | Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1615 | } else { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1616 | // Big endian - avoid unaligned loads. |
| 1617 | // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 |
| 1618 | // Load the top RoundWidth bits. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1619 | Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1620 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 1621 | Alignment); |
| 1622 | |
| 1623 | // Load the remaining ExtraWidth bits. |
| 1624 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1625 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1626 | DAG.getIntPtrConstant(IncrementSize)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1627 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1628 | Node->getValueType(0), Tmp1, Tmp2, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1629 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 1630 | ExtraVT, isVolatile, |
| 1631 | MinAlign(Alignment, IncrementSize)); |
| 1632 | |
| 1633 | // Build a factor node to remember that this load is independent of the |
| 1634 | // other one. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1635 | Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1636 | Hi.getValue(1)); |
| 1637 | |
| 1638 | // Move the top bits to the right place. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1639 | Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1640 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
| 1641 | |
| 1642 | // Join the hi and lo parts. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1643 | Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | Tmp1 = LegalizeOp(Result); |
| 1647 | Tmp2 = LegalizeOp(Ch); |
| 1648 | } else { |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 1649 | switch (TLI.getLoadExtAction(ExtType, SrcVT)) { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1650 | default: assert(0 && "This action is not supported yet!"); |
| 1651 | case TargetLowering::Custom: |
| 1652 | isCustom = true; |
| 1653 | // FALLTHROUGH |
| 1654 | case TargetLowering::Legal: |
| 1655 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1656 | Tmp1 = Result.getValue(0); |
| 1657 | Tmp2 = Result.getValue(1); |
| 1658 | |
| 1659 | if (isCustom) { |
| 1660 | Tmp3 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1661 | if (Tmp3.getNode()) { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1662 | Tmp1 = LegalizeOp(Tmp3); |
| 1663 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1664 | } |
| 1665 | } else { |
| 1666 | // If this is an unaligned load and the target doesn't support it, |
| 1667 | // expand it. |
| 1668 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1669 | unsigned ABIAlignment = TLI.getTargetData()-> |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1670 | getABITypeAlignment(LD->getMemoryVT().getTypeForMVT()); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1671 | if (LD->getAlignment() < ABIAlignment){ |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1672 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG, |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1673 | TLI); |
| 1674 | Tmp1 = Result.getOperand(0); |
| 1675 | Tmp2 = Result.getOperand(1); |
| 1676 | Tmp1 = LegalizeOp(Tmp1); |
| 1677 | Tmp2 = LegalizeOp(Tmp2); |
| 1678 | } |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1681 | break; |
| 1682 | case TargetLowering::Expand: |
| 1683 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 1684 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1685 | SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, LD->getSrcValue(), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1686 | LD->getSrcValueOffset(), |
| 1687 | LD->isVolatile(), LD->getAlignment()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1688 | Result = DAG.getNode(ISD::FP_EXTEND, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1689 | Node->getValueType(0), Load); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1690 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1691 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1692 | break; |
| 1693 | } |
| 1694 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
| 1695 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1696 | // zero/sign extend inreg. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1697 | Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1698 | Tmp1, Tmp2, LD->getSrcValue(), |
| 1699 | LD->getSrcValueOffset(), SrcVT, |
| 1700 | LD->isVolatile(), LD->getAlignment()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1701 | SDValue ValRes; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1702 | if (ExtType == ISD::SEXTLOAD) |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1703 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, |
| 1704 | Result.getValueType(), |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1705 | Result, DAG.getValueType(SrcVT)); |
| 1706 | else |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1707 | ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1708 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1709 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1712 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1713 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1714 | // Since loads produce two values, make sure to remember that we legalized |
| 1715 | // both of them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1716 | AddLegalizedOperand(SDValue(Node, 0), Tmp1); |
| 1717 | AddLegalizedOperand(SDValue(Node, 1), Tmp2); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1718 | return Op.getResNo() ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1719 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1720 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1721 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1722 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1723 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1724 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1725 | int SVOffset = ST->getSrcValueOffset(); |
| 1726 | unsigned Alignment = ST->getAlignment(); |
| 1727 | bool isVolatile = ST->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1728 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1729 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1730 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 1731 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 1732 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 1733 | // to phase ordering between legalized code and the dag combiner. This |
| 1734 | // probably means that we need to integrate dag combiner and legalizer |
| 1735 | // together. |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1736 | // We generally can't do this one for long doubles. |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 1737 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1738 | if (CFP->getValueType(0) == MVT::f32 && |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1739 | getTypeAction(MVT::i32) == Legal) { |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 1740 | Tmp3 = DAG.getConstant(CFP->getValueAPF(). |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 1741 | bitcastToAPInt().zextOrTrunc(32), |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 1742 | MVT::i32); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1743 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1744 | SVOffset, isVolatile, Alignment); |
| 1745 | break; |
| 1746 | } else if (CFP->getValueType(0) == MVT::f64) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1747 | // If this target supports 64-bit registers, do a single 64-bit store. |
| 1748 | if (getTypeAction(MVT::i64) == Legal) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 1749 | Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 1750 | zextOrTrunc(64), MVT::i64); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1751 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1752 | SVOffset, isVolatile, Alignment); |
| 1753 | break; |
Duncan Sands | d4b9c17 | 2008-06-13 19:07:40 +0000 | [diff] [blame] | 1754 | } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1755 | // Otherwise, if the target supports 32-bit registers, use 2 32-bit |
| 1756 | // stores. If the target supports neither 32- nor 64-bits, this |
| 1757 | // xform is certainly not worth it. |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 1758 | const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1759 | SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32); |
| 1760 | SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 1761 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1762 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1763 | Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getSrcValue(), |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1764 | SVOffset, isVolatile, Alignment); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1765 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1766 | DAG.getIntPtrConstant(4)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1767 | Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), SVOffset+4, |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 1768 | isVolatile, MinAlign(Alignment, 4U)); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1769 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1770 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1773 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1774 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1775 | |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1776 | { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1777 | Tmp3 = LegalizeOp(ST->getValue()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1778 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1779 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1780 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1781 | MVT VT = Tmp3.getValueType(); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1782 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1783 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1784 | case TargetLowering::Legal: |
| 1785 | // If this is an unaligned store and the target doesn't support it, |
| 1786 | // expand it. |
| 1787 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1788 | unsigned ABIAlignment = TLI.getTargetData()-> |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1789 | getABITypeAlignment(ST->getMemoryVT().getTypeForMVT()); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1790 | if (ST->getAlignment() < ABIAlignment) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1791 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1792 | TLI); |
| 1793 | } |
| 1794 | break; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1795 | case TargetLowering::Custom: |
| 1796 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1797 | if (Tmp1.getNode()) Result = Tmp1; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1798 | break; |
| 1799 | case TargetLowering::Promote: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1800 | assert(VT.isVector() && "Unknown legal promote case!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1801 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, dl, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1802 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1803 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1804 | ST->getSrcValue(), SVOffset, isVolatile, |
| 1805 | Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1806 | break; |
| 1807 | } |
| 1808 | break; |
| 1809 | } |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1810 | } else { |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1811 | Tmp3 = LegalizeOp(ST->getValue()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1812 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1813 | MVT StVT = ST->getMemoryVT(); |
| 1814 | unsigned StWidth = StVT.getSizeInBits(); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1815 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1816 | if (StWidth != StVT.getStoreSizeInBits()) { |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1817 | // Promote to a byte-sized store with upper bits zero if not |
| 1818 | // storing an integral number of bytes. For example, promote |
| 1819 | // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1820 | MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits()); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1821 | Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT); |
| 1822 | Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1823 | SVOffset, NVT, isVolatile, Alignment); |
| 1824 | } else if (StWidth & (StWidth - 1)) { |
| 1825 | // If not storing a power-of-2 number of bits, expand as two stores. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1826 | assert(StVT.isExtended() && !StVT.isVector() && |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1827 | "Unsupported truncstore!"); |
| 1828 | unsigned RoundWidth = 1 << Log2_32(StWidth); |
| 1829 | assert(RoundWidth < StWidth); |
| 1830 | unsigned ExtraWidth = StWidth - RoundWidth; |
| 1831 | assert(ExtraWidth < RoundWidth); |
| 1832 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 1833 | "Store size not an integral number of bytes!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1834 | MVT RoundVT = MVT::getIntegerVT(RoundWidth); |
| 1835 | MVT ExtraVT = MVT::getIntegerVT(ExtraWidth); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1836 | SDValue Lo, Hi; |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1837 | unsigned IncrementSize; |
| 1838 | |
| 1839 | if (TLI.isLittleEndian()) { |
| 1840 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) |
| 1841 | // Store the bottom RoundWidth bits. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1842 | Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1843 | SVOffset, RoundVT, |
| 1844 | isVolatile, Alignment); |
| 1845 | |
| 1846 | // Store the remaining ExtraWidth bits. |
| 1847 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1848 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1849 | DAG.getIntPtrConstant(IncrementSize)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1850 | Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1851 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1852 | Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1853 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 1854 | MinAlign(Alignment, IncrementSize)); |
| 1855 | } else { |
| 1856 | // Big endian - avoid unaligned stores. |
| 1857 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X |
| 1858 | // Store the top RoundWidth bits. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1859 | Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1860 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1861 | Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getSrcValue(), |
| 1862 | SVOffset, RoundVT, isVolatile, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Store the remaining ExtraWidth bits. |
| 1865 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1866 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1867 | DAG.getIntPtrConstant(IncrementSize)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1868 | Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1869 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 1870 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1871 | } |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1872 | |
| 1873 | // The order of the stores doesn't matter. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1874 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1875 | } else { |
| 1876 | if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 1877 | Tmp2 != ST->getBasePtr()) |
| 1878 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1879 | ST->getOffset()); |
| 1880 | |
| 1881 | switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { |
| 1882 | default: assert(0 && "This action is not supported yet!"); |
| 1883 | case TargetLowering::Legal: |
| 1884 | // If this is an unaligned store and the target doesn't support it, |
| 1885 | // expand it. |
| 1886 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1887 | unsigned ABIAlignment = TLI.getTargetData()-> |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1888 | getABITypeAlignment(ST->getMemoryVT().getTypeForMVT()); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1889 | if (ST->getAlignment() < ABIAlignment) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1890 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1891 | TLI); |
| 1892 | } |
| 1893 | break; |
| 1894 | case TargetLowering::Custom: |
| 1895 | Result = TLI.LowerOperation(Result, DAG); |
| 1896 | break; |
| 1897 | case Expand: |
| 1898 | // TRUNCSTORE:i16 i32 -> STORE i16 |
| 1899 | assert(isTypeLegal(StVT) && "Do not know how to expand this store!"); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1900 | Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3); |
| 1901 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getSrcValue(), |
| 1902 | SVOffset, isVolatile, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1903 | break; |
| 1904 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1905 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1906 | } |
| 1907 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1908 | } |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1909 | case ISD::SELECT: |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1910 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1911 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1912 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1914 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1915 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1916 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1917 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1918 | case TargetLowering::Legal: break; |
| 1919 | case TargetLowering::Custom: { |
| 1920 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1921 | if (Tmp1.getNode()) Result = Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1924 | case TargetLowering::Expand: |
| 1925 | if (Tmp1.getOpcode() == ISD::SETCC) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1926 | Result = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1927 | Tmp2, Tmp3, |
| 1928 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1929 | } else { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1930 | Result = DAG.getSelectCC(dl, Tmp1, |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1931 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1932 | Tmp2, Tmp3, ISD::SETNE); |
| 1933 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | case TargetLowering::Promote: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1936 | MVT NVT = |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1937 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1938 | unsigned ExtOp, TruncOp; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1939 | if (Tmp2.getValueType().isVector()) { |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1940 | ExtOp = ISD::BIT_CONVERT; |
| 1941 | TruncOp = ISD::BIT_CONVERT; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1942 | } else if (Tmp2.getValueType().isInteger()) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1943 | ExtOp = ISD::ANY_EXTEND; |
| 1944 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1945 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1946 | ExtOp = ISD::FP_EXTEND; |
| 1947 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1948 | } |
| 1949 | // Promote each of the values to the new type. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1950 | Tmp2 = DAG.getNode(ExtOp, dl, NVT, Tmp2); |
| 1951 | Tmp3 = DAG.getNode(ExtOp, dl, NVT, Tmp3); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1952 | // Perform the larger operation, then round down. |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 1953 | Result = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1954 | if (TruncOp != ISD::FP_ROUND) |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1955 | Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1956 | else |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1957 | Result = DAG.getNode(TruncOp, dl, Node->getValueType(0), Result, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1958 | DAG.getIntPtrConstant(0)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
| 1961 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1962 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1963 | case ISD::SELECT_CC: { |
| 1964 | Tmp1 = Node->getOperand(0); // LHS |
| 1965 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1966 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 1967 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1968 | SDValue CC = Node->getOperand(4); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1969 | |
| 1970 | LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 1971 | Tmp1, Tmp2, CC, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1972 | |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1973 | // If we didn't get both a LHS and RHS back from LegalizeSetCC, |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1974 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1975 | // the result against zero to select between true and false values. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1976 | if (Tmp2.getNode() == 0) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1977 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 1978 | CC = DAG.getCondCode(ISD::SETNE); |
| 1979 | } |
| 1980 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 1981 | |
| 1982 | // Everything is legal, see if we should expand this op or something. |
| 1983 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 1984 | default: assert(0 && "This action is not supported yet!"); |
| 1985 | case TargetLowering::Legal: break; |
| 1986 | case TargetLowering::Custom: |
| 1987 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1988 | if (Tmp1.getNode()) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1989 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1990 | } |
| 1991 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1992 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1993 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1994 | Tmp1 = Node->getOperand(0); |
| 1995 | Tmp2 = Node->getOperand(1); |
| 1996 | Tmp3 = Node->getOperand(2); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 1997 | LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1998 | |
| 1999 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2000 | // not always be possible to return a true LHS & RHS. In this case, just |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2001 | // return the value we legalized, returned in the LHS |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2002 | if (Tmp2.getNode() == 0) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2003 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2004 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2005 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2007 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2008 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2009 | case TargetLowering::Custom: |
| 2010 | isCustom = true; |
| 2011 | // FALLTHROUGH. |
| 2012 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2013 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2014 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2015 | Tmp4 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2016 | if (Tmp4.getNode()) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2017 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2018 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2019 | case TargetLowering::Promote: { |
| 2020 | // First step, figure out the appropriate operation to use. |
| 2021 | // Allow SETCC to not be supported for all legal data types |
| 2022 | // Mostly this targets FP |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2023 | MVT NewInTy = Node->getOperand(0).getValueType(); |
| 2024 | MVT OldVT = NewInTy; OldVT = OldVT; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2025 | |
| 2026 | // Scan for the appropriate larger type to use. |
| 2027 | while (1) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2028 | NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1); |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2029 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2030 | assert(NewInTy.isInteger() == OldVT.isInteger() && |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2031 | "Fell off of the edge of the integer world"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2032 | assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() && |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2033 | "Fell off of the edge of the floating point world"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2034 | |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2035 | // If the target supports SETCC of this type, use it. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2036 | if (TLI.isOperationLegalOrCustom(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2039 | if (NewInTy.isInteger()) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2040 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2041 | else { |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2042 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp1); |
| 2043 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NewInTy, Tmp2); |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2044 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2045 | Tmp1 = LegalizeOp(Tmp1); |
| 2046 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2047 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2048 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2049 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2050 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2051 | case TargetLowering::Expand: |
| 2052 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2053 | // rhs that selects between const 1 (true) and const 0 (false). |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2054 | MVT VT = Node->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2055 | Result = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2056 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2057 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2058 | break; |
| 2059 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2060 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2063 | case ISD::ADD: |
| 2064 | case ISD::SUB: |
| 2065 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2066 | case ISD::MULHS: |
| 2067 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2068 | case ISD::UDIV: |
| 2069 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2070 | case ISD::AND: |
| 2071 | case ISD::OR: |
| 2072 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2073 | case ISD::SHL: |
| 2074 | case ISD::SRL: |
| 2075 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2076 | case ISD::FADD: |
| 2077 | case ISD::FSUB: |
| 2078 | case ISD::FMUL: |
| 2079 | case ISD::FDIV: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2080 | case ISD::FPOW: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2081 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2082 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2083 | |
| 2084 | if ((Node->getOpcode() == ISD::SHL || |
| 2085 | Node->getOpcode() == ISD::SRL || |
| 2086 | Node->getOpcode() == ISD::SRA) && |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 2087 | !Node->getValueType(0).isVector()) |
| 2088 | Tmp2 = DAG.getShiftAmountOperand(Tmp2); |
| 2089 | |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 2090 | Tmp2 = LegalizeOp(Tmp2); // Legalize the RHS. |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2091 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2092 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 2093 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2094 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2095 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2096 | case TargetLowering::Legal: break; |
| 2097 | case TargetLowering::Custom: |
| 2098 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2099 | if (Tmp1.getNode()) { |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2100 | Result = Tmp1; |
| 2101 | break; |
Nate Begeman | 24dc346 | 2008-07-29 19:07:27 +0000 | [diff] [blame] | 2102 | } |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2103 | // Fall through if the custom lower can't deal with the operation |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2104 | case TargetLowering::Expand: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2105 | MVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2106 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2107 | // See if multiply or divide can be lowered using two-result operations. |
| 2108 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 2109 | if (Node->getOpcode() == ISD::MUL) { |
| 2110 | // We just need the low half of the multiply; try both the signed |
| 2111 | // and unsigned forms. If the target supports both SMUL_LOHI and |
| 2112 | // UMUL_LOHI, form a preference by checking which forms of plain |
| 2113 | // MULH it supports. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2114 | bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); |
| 2115 | bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); |
| 2116 | bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); |
| 2117 | bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2118 | unsigned OpToUse = 0; |
| 2119 | if (HasSMUL_LOHI && !HasMULHS) { |
| 2120 | OpToUse = ISD::SMUL_LOHI; |
| 2121 | } else if (HasUMUL_LOHI && !HasMULHU) { |
| 2122 | OpToUse = ISD::UMUL_LOHI; |
| 2123 | } else if (HasSMUL_LOHI) { |
| 2124 | OpToUse = ISD::SMUL_LOHI; |
| 2125 | } else if (HasUMUL_LOHI) { |
| 2126 | OpToUse = ISD::UMUL_LOHI; |
| 2127 | } |
| 2128 | if (OpToUse) { |
Duncan Sands | a9cad0e | 2009-05-06 11:29:50 +0000 | [diff] [blame] | 2129 | Result = DAG.getNode(OpToUse, dl, VTs, Tmp1, Tmp2); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | if (Node->getOpcode() == ISD::MULHS && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2134 | TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2135 | Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2136 | VTs, Tmp1, Tmp2).getNode(), |
Chris Lattner | 31d7161 | 2008-10-04 21:27:46 +0000 | [diff] [blame] | 2137 | 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2138 | break; |
| 2139 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2140 | if (Node->getOpcode() == ISD::MULHU && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2141 | TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT)) { |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2142 | Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, |
| 2143 | VTs, Tmp1, Tmp2).getNode(), |
Chris Lattner | 31d7161 | 2008-10-04 21:27:46 +0000 | [diff] [blame] | 2144 | 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2145 | break; |
| 2146 | } |
| 2147 | if (Node->getOpcode() == ISD::SDIV && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2148 | TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) { |
Duncan Sands | a9cad0e | 2009-05-06 11:29:50 +0000 | [diff] [blame] | 2149 | Result = DAG.getNode(ISD::SDIVREM, dl, VTs, Tmp1, Tmp2); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2150 | break; |
| 2151 | } |
| 2152 | if (Node->getOpcode() == ISD::UDIV && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2153 | TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) { |
Duncan Sands | a9cad0e | 2009-05-06 11:29:50 +0000 | [diff] [blame] | 2154 | Result = DAG.getNode(ISD::UDIVREM, dl, VTs, Tmp1, Tmp2); |
| 2155 | break; |
| 2156 | } |
| 2157 | if (Node->getOpcode() == ISD::SUB && |
| 2158 | TLI.isOperationLegalOrCustom(ISD::ADD, VT) && |
| 2159 | TLI.isOperationLegalOrCustom(ISD::XOR, VT)) { |
| 2160 | Tmp2 = DAG.getNode(ISD::XOR, dl, VT, Tmp2, |
| 2161 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT)); |
| 2162 | Tmp2 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT)); |
| 2163 | Result = DAG.getNode(ISD::ADD, dl, VT, Tmp1, Tmp2); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2164 | break; |
| 2165 | } |
Mon P Wang | 87c8a8f | 2008-12-18 20:03:17 +0000 | [diff] [blame] | 2166 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2167 | // Check to see if we have a libcall for this operator. |
| 2168 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 2169 | bool isSigned = false; |
| 2170 | switch (Node->getOpcode()) { |
| 2171 | case ISD::UDIV: |
| 2172 | case ISD::SDIV: |
Anton Korobeynikov | 813090c | 2009-05-03 13:18:16 +0000 | [diff] [blame] | 2173 | isSigned = Node->getOpcode() == ISD::SDIV; |
| 2174 | if (VT == MVT::i16) |
| 2175 | LC = (isSigned ? RTLIB::SDIV_I16 : RTLIB::UDIV_I16); |
| 2176 | else if (VT == MVT::i32) |
| 2177 | LC = (isSigned ? RTLIB::SDIV_I32 : RTLIB::UDIV_I32); |
| 2178 | else if (VT == MVT::i64) |
| 2179 | LC = (isSigned ? RTLIB::SDIV_I64 : RTLIB::UDIV_I64); |
| 2180 | else if (VT == MVT::i128) |
| 2181 | LC = (isSigned ? RTLIB::SDIV_I128 : RTLIB::UDIV_I128); |
| 2182 | break; |
Chris Lattner | 31d7161 | 2008-10-04 21:27:46 +0000 | [diff] [blame] | 2183 | case ISD::MUL: |
Anton Korobeynikov | 2f25c2c | 2009-05-03 13:13:51 +0000 | [diff] [blame] | 2184 | if (VT == MVT::i16) |
| 2185 | LC = RTLIB::MUL_I16; |
Anton Korobeynikov | 813090c | 2009-05-03 13:18:16 +0000 | [diff] [blame] | 2186 | else if (VT == MVT::i32) |
Chris Lattner | 31d7161 | 2008-10-04 21:27:46 +0000 | [diff] [blame] | 2187 | LC = RTLIB::MUL_I32; |
Nate Begeman | 9b99485 | 2009-01-24 22:12:48 +0000 | [diff] [blame] | 2188 | else if (VT == MVT::i64) |
Scott Michel | 845145f | 2008-12-29 03:21:37 +0000 | [diff] [blame] | 2189 | LC = RTLIB::MUL_I64; |
Anton Korobeynikov | 2f25c2c | 2009-05-03 13:13:51 +0000 | [diff] [blame] | 2190 | else if (VT == MVT::i128) |
| 2191 | LC = RTLIB::MUL_I128; |
Chris Lattner | 31d7161 | 2008-10-04 21:27:46 +0000 | [diff] [blame] | 2192 | break; |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2193 | case ISD::FPOW: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2194 | LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, |
| 2195 | RTLIB::POW_PPCF128); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2196 | break; |
Scott Michel | d1e8d9c | 2009-01-21 04:58:48 +0000 | [diff] [blame] | 2197 | case ISD::FDIV: |
| 2198 | LC = GetFPLibCall(VT, RTLIB::DIV_F32, RTLIB::DIV_F64, RTLIB::DIV_F80, |
| 2199 | RTLIB::DIV_PPCF128); |
| 2200 | break; |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2201 | default: break; |
| 2202 | } |
| 2203 | if (LC != RTLIB::UNKNOWN_LIBCALL) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2204 | SDValue Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 2205 | Result = ExpandLibCall(LC, Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2208 | |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2209 | assert(0 && "Cannot expand this binary operator!"); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2210 | break; |
| 2211 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2212 | case TargetLowering::Promote: { |
| 2213 | switch (Node->getOpcode()) { |
| 2214 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2215 | case ISD::AND: |
| 2216 | case ISD::OR: |
| 2217 | case ISD::XOR: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2218 | MVT OVT = Node->getValueType(0); |
| 2219 | MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2220 | assert(OVT.isVector() && "Cannot promote this BinOp!"); |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2221 | // Bit convert each of the values to the new type. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2222 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp1); |
| 2223 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, dl, NVT, Tmp2); |
| 2224 | Result = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2225 | // Bit convert the result back the original type. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2226 | Result = DAG.getNode(ISD::BIT_CONVERT, dl, OVT, Result); |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2231 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2232 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2233 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2234 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 2235 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2236 | |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2237 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2239 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2240 | default: assert(0 && "Operation not supported"); |
| 2241 | case TargetLowering::Custom: |
| 2242 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2243 | if (Tmp1.getNode()) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2244 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2245 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2246 | case TargetLowering::Expand: { |
Eli Friedman | ed2f8c5 | 2009-05-24 10:21:20 +0000 | [diff] [blame] | 2247 | assert((Tmp2.getValueType() == MVT::f32 || |
Eli Friedman | 57f1a4b | 2009-05-24 20:29:11 +0000 | [diff] [blame] | 2248 | Tmp2.getValueType() == MVT::f64) && |
Eli Friedman | ed2f8c5 | 2009-05-24 10:21:20 +0000 | [diff] [blame] | 2249 | "Ugly special-cased code!"); |
| 2250 | // Get the sign bit of the RHS. |
Eli Friedman | 57f1a4b | 2009-05-24 20:29:11 +0000 | [diff] [blame] | 2251 | SDValue SignBit; |
| 2252 | MVT IVT = Tmp2.getValueType() == MVT::f64 ? MVT::i64 : MVT::i32; |
| 2253 | if (isTypeLegal(IVT)) { |
| 2254 | SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, IVT, Tmp2); |
| 2255 | } else { |
| 2256 | assert(isTypeLegal(TLI.getPointerTy()) && |
| 2257 | (TLI.getPointerTy() == MVT::i32 || |
| 2258 | TLI.getPointerTy() == MVT::i64) && |
| 2259 | "Legal type for load?!"); |
| 2260 | SDValue StackPtr = DAG.CreateStackTemporary(Tmp2.getValueType()); |
| 2261 | SDValue StorePtr = StackPtr, LoadPtr = StackPtr; |
| 2262 | SDValue Ch = |
| 2263 | DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StorePtr, NULL, 0); |
| 2264 | if (Tmp2.getValueType() == MVT::f64 && TLI.isLittleEndian()) |
| 2265 | LoadPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), |
| 2266 | LoadPtr, DAG.getIntPtrConstant(4)); |
| 2267 | SignBit = DAG.getExtLoad(ISD::SEXTLOAD, dl, TLI.getPointerTy(), |
| 2268 | Ch, LoadPtr, NULL, 0, MVT::i32); |
| 2269 | } |
| 2270 | SignBit = |
| 2271 | DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()), |
| 2272 | SignBit, DAG.getConstant(0, SignBit.getValueType()), |
| 2273 | ISD::SETLT); |
Eli Friedman | ed2f8c5 | 2009-05-24 10:21:20 +0000 | [diff] [blame] | 2274 | // Get the absolute value of the result. |
| 2275 | SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1); |
| 2276 | // Select between the nabs and abs value based on the sign bit of |
| 2277 | // the input. |
| 2278 | Result = DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit, |
| 2279 | DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), |
| 2280 | AbsVal), |
| 2281 | AbsVal); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2282 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2283 | break; |
| 2284 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2285 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2286 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2287 | case ISD::BUILD_PAIR: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2288 | MVT PairTy = Node->getValueType(0); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2289 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2290 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2291 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2292 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2293 | case TargetLowering::Promote: |
| 2294 | case TargetLowering::Custom: |
| 2295 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2296 | case TargetLowering::Legal: |
| 2297 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2298 | Result = DAG.getNode(ISD::BUILD_PAIR, dl, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2299 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2300 | case TargetLowering::Expand: |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2301 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Tmp1); |
| 2302 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Tmp2); |
| 2303 | Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2304 | DAG.getConstant(PairTy.getSizeInBits()/2, |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2305 | TLI.getShiftAmountTy())); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2306 | Result = DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2307 | break; |
| 2308 | } |
| 2309 | break; |
| 2310 | } |
| 2311 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2312 | case ISD::UREM: |
| 2313 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2314 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2315 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2316 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2317 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2318 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2319 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2320 | case TargetLowering::Custom: |
| 2321 | isCustom = true; |
| 2322 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2323 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2324 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2325 | if (isCustom) { |
| 2326 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2327 | if (Tmp1.getNode()) Result = Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2328 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2329 | break; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2330 | case TargetLowering::Expand: { |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2331 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2332 | bool isSigned = DivOpc == ISD::SDIV; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2333 | MVT VT = Node->getValueType(0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2334 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2335 | // See if remainder can be lowered using two-result operations. |
| 2336 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 2337 | if (Node->getOpcode() == ISD::SREM && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2338 | TLI.isOperationLegalOrCustom(ISD::SDIVREM, VT)) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2339 | Result = SDValue(DAG.getNode(ISD::SDIVREM, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2340 | VTs, Tmp1, Tmp2).getNode(), 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | if (Node->getOpcode() == ISD::UREM && |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2344 | TLI.isOperationLegalOrCustom(ISD::UDIVREM, VT)) { |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2345 | Result = SDValue(DAG.getNode(ISD::UDIVREM, dl, |
| 2346 | VTs, Tmp1, Tmp2).getNode(), 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2347 | break; |
| 2348 | } |
| 2349 | |
Anton Korobeynikov | 58c04e1 | 2009-05-08 18:51:08 +0000 | [diff] [blame] | 2350 | if (VT.isInteger() && |
| 2351 | TLI.getOperationAction(DivOpc, VT) == TargetLowering::Legal) { |
| 2352 | // X % Y -> X-X/Y*Y |
| 2353 | Result = DAG.getNode(DivOpc, dl, VT, Tmp1, Tmp2); |
| 2354 | Result = DAG.getNode(ISD::MUL, dl, VT, Result, Tmp2); |
| 2355 | Result = DAG.getNode(ISD::SUB, dl, VT, Tmp1, Result); |
| 2356 | break; |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2357 | } |
Anton Korobeynikov | 58c04e1 | 2009-05-08 18:51:08 +0000 | [diff] [blame] | 2358 | |
| 2359 | // Check to see if we have a libcall for this operator. |
| 2360 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 2361 | switch (Node->getOpcode()) { |
| 2362 | default: break; |
| 2363 | case ISD::UREM: |
| 2364 | case ISD::SREM: |
| 2365 | if (VT == MVT::i16) |
| 2366 | LC = (isSigned ? RTLIB::SREM_I16 : RTLIB::UREM_I16); |
| 2367 | else if (VT == MVT::i32) |
| 2368 | LC = (isSigned ? RTLIB::SREM_I32 : RTLIB::UREM_I32); |
| 2369 | else if (VT == MVT::i64) |
| 2370 | LC = (isSigned ? RTLIB::SREM_I64 : RTLIB::UREM_I64); |
| 2371 | else if (VT == MVT::i128) |
| 2372 | LC = (isSigned ? RTLIB::SREM_I128 : RTLIB::UREM_I128); |
| 2373 | break; |
| 2374 | case ISD::FREM: |
| 2375 | // Floating point mod -> fmod libcall. |
| 2376 | LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64, |
| 2377 | RTLIB::REM_F80, RTLIB::REM_PPCF128); |
| 2378 | break; |
| 2379 | } |
| 2380 | |
| 2381 | if (LC != RTLIB::UNKNOWN_LIBCALL) { |
| 2382 | SDValue Dummy; |
| 2383 | Result = ExpandLibCall(LC, Node, isSigned, Dummy); |
| 2384 | break; |
| 2385 | } |
| 2386 | |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2387 | assert(0 && "Cannot expand this binary operator!"); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2390 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2391 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2392 | case ISD::VAARG: { |
| 2393 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2394 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2395 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2396 | MVT VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2397 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2398 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2399 | case TargetLowering::Custom: |
| 2400 | isCustom = true; |
| 2401 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2402 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2403 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2404 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2405 | Tmp1 = Result.getValue(1); |
| 2406 | |
| 2407 | if (isCustom) { |
| 2408 | Tmp2 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2409 | if (Tmp2.getNode()) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2410 | Result = LegalizeOp(Tmp2); |
| 2411 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2412 | } |
| 2413 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2414 | break; |
| 2415 | case TargetLowering::Expand: { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2416 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2417 | SDValue VAList = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2418 | // Increment the pointer, VAList, to the next vaarg |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2419 | Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList, |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2420 | DAG.getConstant(TLI.getTargetData()-> |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2421 | getTypeAllocSize(VT.getTypeForMVT()), |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 2422 | TLI.getPointerTy())); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2423 | // Store the incremented VAList to the legalized pointer |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2424 | Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2425 | // Load the actual argument out of the pointer VAList |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2426 | Result = DAG.getLoad(VT, dl, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2427 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2428 | Result = LegalizeOp(Result); |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2432 | // Since VAARG produces two values, make sure to remember that we |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2433 | // legalized both of them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2434 | AddLegalizedOperand(SDValue(Node, 0), Result); |
| 2435 | AddLegalizedOperand(SDValue(Node, 1), Tmp1); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 2436 | return Op.getResNo() ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2437 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2438 | // Unary operators |
| 2439 | case ISD::FABS: |
| 2440 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2441 | case ISD::FSQRT: |
| 2442 | case ISD::FSIN: |
| 2443 | case ISD::FCOS: |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 2444 | case ISD::FLOG: |
| 2445 | case ISD::FLOG2: |
| 2446 | case ISD::FLOG10: |
| 2447 | case ISD::FEXP: |
| 2448 | case ISD::FEXP2: |
Dan Gohman | 509e84f | 2008-08-21 17:55:02 +0000 | [diff] [blame] | 2449 | case ISD::FTRUNC: |
| 2450 | case ISD::FFLOOR: |
| 2451 | case ISD::FCEIL: |
| 2452 | case ISD::FRINT: |
| 2453 | case ISD::FNEARBYINT: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2454 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2455 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2456 | case TargetLowering::Promote: |
| 2457 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2458 | isCustom = true; |
| 2459 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2460 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2461 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2462 | if (isCustom) { |
| 2463 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2464 | if (Tmp1.getNode()) Result = Tmp1; |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2465 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2466 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2467 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2468 | switch (Node->getOpcode()) { |
| 2469 | default: assert(0 && "Unreachable!"); |
| 2470 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2471 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2472 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2473 | Result = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2474 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2475 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2476 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2477 | MVT VT = Node->getValueType(0); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2478 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2479 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 2480 | Tmp1, Tmp2, ISD::SETUGT); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2481 | Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1); |
| 2482 | Result = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2483 | break; |
| 2484 | } |
Evan Cheng | 9d24ac5 | 2008-09-09 23:35:53 +0000 | [diff] [blame] | 2485 | case ISD::FSQRT: |
| 2486 | case ISD::FSIN: |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2487 | case ISD::FCOS: |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 2488 | case ISD::FLOG: |
| 2489 | case ISD::FLOG2: |
| 2490 | case ISD::FLOG10: |
| 2491 | case ISD::FEXP: |
| 2492 | case ISD::FEXP2: |
Dan Gohman | 2bb1e3e | 2008-08-21 18:38:14 +0000 | [diff] [blame] | 2493 | case ISD::FTRUNC: |
| 2494 | case ISD::FFLOOR: |
| 2495 | case ISD::FCEIL: |
| 2496 | case ISD::FRINT: |
Evan Cheng | 9d24ac5 | 2008-09-09 23:35:53 +0000 | [diff] [blame] | 2497 | case ISD::FNEARBYINT: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2498 | MVT VT = Node->getValueType(0); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2499 | |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2500 | assert(!VT.isVector() && "Vector shouldn't get here!"); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2501 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2502 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2503 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2504 | case ISD::FSQRT: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2505 | LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 2506 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2507 | break; |
| 2508 | case ISD::FSIN: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2509 | LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 2510 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | case ISD::FCOS: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2513 | LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64, |
| 2514 | RTLIB::COS_F80, RTLIB::COS_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2515 | break; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 2516 | case ISD::FLOG: |
| 2517 | LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64, |
| 2518 | RTLIB::LOG_F80, RTLIB::LOG_PPCF128); |
| 2519 | break; |
| 2520 | case ISD::FLOG2: |
| 2521 | LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64, |
| 2522 | RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128); |
| 2523 | break; |
| 2524 | case ISD::FLOG10: |
| 2525 | LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64, |
| 2526 | RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128); |
| 2527 | break; |
| 2528 | case ISD::FEXP: |
| 2529 | LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64, |
| 2530 | RTLIB::EXP_F80, RTLIB::EXP_PPCF128); |
| 2531 | break; |
| 2532 | case ISD::FEXP2: |
| 2533 | LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64, |
| 2534 | RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128); |
| 2535 | break; |
Dan Gohman | 2bb1e3e | 2008-08-21 18:38:14 +0000 | [diff] [blame] | 2536 | case ISD::FTRUNC: |
| 2537 | LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, |
| 2538 | RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128); |
| 2539 | break; |
| 2540 | case ISD::FFLOOR: |
| 2541 | LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, |
| 2542 | RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128); |
| 2543 | break; |
| 2544 | case ISD::FCEIL: |
| 2545 | LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64, |
| 2546 | RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128); |
| 2547 | break; |
| 2548 | case ISD::FRINT: |
| 2549 | LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64, |
| 2550 | RTLIB::RINT_F80, RTLIB::RINT_PPCF128); |
| 2551 | break; |
| 2552 | case ISD::FNEARBYINT: |
| 2553 | LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64, |
| 2554 | RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128); |
| 2555 | break; |
Evan Cheng | 9d24ac5 | 2008-09-09 23:35:53 +0000 | [diff] [blame] | 2556 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2557 | default: assert(0 && "Unreachable!"); |
| 2558 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2559 | SDValue Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 2560 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2561 | break; |
| 2562 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2563 | } |
| 2564 | break; |
| 2565 | } |
| 2566 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2567 | case ISD::FPOWI: { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2568 | MVT VT = Node->getValueType(0); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2569 | |
| 2570 | // Expand unsupported unary vector operators by unrolling them. |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2571 | assert(!VT.isVector() && "Vector shouldn't get here!"); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2572 | |
| 2573 | // We always lower FPOWI into a libcall. No target support for it yet. |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2574 | RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, |
| 2575 | RTLIB::POWI_F80, RTLIB::POWI_PPCF128); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2576 | SDValue Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 2577 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2580 | case ISD::SADDO: |
| 2581 | case ISD::SSUBO: { |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2582 | MVT VT = Node->getValueType(0); |
| 2583 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 2584 | default: assert(0 && "This action not supported for this op yet!"); |
| 2585 | case TargetLowering::Custom: |
| 2586 | Result = TLI.LowerOperation(Op, DAG); |
| 2587 | if (Result.getNode()) break; |
| 2588 | // FALLTHROUGH |
| 2589 | case TargetLowering::Legal: { |
| 2590 | SDValue LHS = LegalizeOp(Node->getOperand(0)); |
| 2591 | SDValue RHS = LegalizeOp(Node->getOperand(1)); |
| 2592 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2593 | SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2594 | ISD::ADD : ISD::SUB, dl, LHS.getValueType(), |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2595 | LHS, RHS); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2596 | MVT OType = Node->getValueType(1); |
| 2597 | |
Bill Wendling | a6af91a | 2008-11-25 08:19:22 +0000 | [diff] [blame] | 2598 | SDValue Zero = DAG.getConstant(0, LHS.getValueType()); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2599 | |
Bill Wendling | 740464e | 2008-11-25 19:40:17 +0000 | [diff] [blame] | 2600 | // LHSSign -> LHS >= 0 |
| 2601 | // RHSSign -> RHS >= 0 |
| 2602 | // SumSign -> Sum >= 0 |
| 2603 | // |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2604 | // Add: |
Bill Wendling | 740464e | 2008-11-25 19:40:17 +0000 | [diff] [blame] | 2605 | // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign) |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2606 | // Sub: |
| 2607 | // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign) |
Bill Wendling | 740464e | 2008-11-25 19:40:17 +0000 | [diff] [blame] | 2608 | // |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2609 | SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE); |
| 2610 | SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2611 | SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, |
| 2612 | Node->getOpcode() == ISD::SADDO ? |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2613 | ISD::SETEQ : ISD::SETNE); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2614 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2615 | SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE); |
| 2616 | SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2617 | |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2618 | SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2619 | |
| 2620 | MVT ValueVTs[] = { LHS.getValueType(), OType }; |
| 2621 | SDValue Ops[] = { Sum, Cmp }; |
| 2622 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2623 | Result = DAG.getNode(ISD::MERGE_VALUES, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2624 | DAG.getVTList(&ValueVTs[0], 2), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2625 | &Ops[0], 2); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2626 | SDNode *RNode = Result.getNode(); |
Dan Gohman | c23e496 | 2009-04-15 20:06:30 +0000 | [diff] [blame] | 2627 | DAG.ReplaceAllUsesWith(Node, RNode); |
Bill Wendling | c0062fb | 2008-11-25 08:12:19 +0000 | [diff] [blame] | 2628 | break; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | break; |
| 2633 | } |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2634 | case ISD::UADDO: |
| 2635 | case ISD::USUBO: { |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 2636 | MVT VT = Node->getValueType(0); |
| 2637 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 2638 | default: assert(0 && "This action not supported for this op yet!"); |
| 2639 | case TargetLowering::Custom: |
| 2640 | Result = TLI.LowerOperation(Op, DAG); |
| 2641 | if (Result.getNode()) break; |
| 2642 | // FALLTHROUGH |
| 2643 | case TargetLowering::Legal: { |
| 2644 | SDValue LHS = LegalizeOp(Node->getOperand(0)); |
| 2645 | SDValue RHS = LegalizeOp(Node->getOperand(1)); |
Bill Wendling | 8ac0d4b | 2008-11-22 00:22:52 +0000 | [diff] [blame] | 2646 | |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2647 | SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ? |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2648 | ISD::ADD : ISD::SUB, dl, LHS.getValueType(), |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2649 | LHS, RHS); |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 2650 | MVT OType = Node->getValueType(1); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2651 | SDValue Cmp = DAG.getSetCC(dl, OType, Sum, LHS, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2652 | Node->getOpcode () == ISD::UADDO ? |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2653 | ISD::SETULT : ISD::SETUGT); |
Bill Wendling | 8ac0d4b | 2008-11-22 00:22:52 +0000 | [diff] [blame] | 2654 | |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 2655 | MVT ValueVTs[] = { LHS.getValueType(), OType }; |
| 2656 | SDValue Ops[] = { Sum, Cmp }; |
Bill Wendling | 8ac0d4b | 2008-11-22 00:22:52 +0000 | [diff] [blame] | 2657 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2658 | Result = DAG.getNode(ISD::MERGE_VALUES, dl, |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 2659 | DAG.getVTList(&ValueVTs[0], 2), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2660 | &Ops[0], 2); |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 2661 | SDNode *RNode = Result.getNode(); |
Dan Gohman | c23e496 | 2009-04-15 20:06:30 +0000 | [diff] [blame] | 2662 | DAG.ReplaceAllUsesWith(Node, RNode); |
Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 2663 | break; |
| 2664 | } |
| 2665 | } |
| 2666 | |
Bill Wendling | 8ac0d4b | 2008-11-22 00:22:52 +0000 | [diff] [blame] | 2667 | break; |
| 2668 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2669 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 2671 | assert(Result.getValueType() == Op.getValueType() && |
| 2672 | "Bad legalization!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2674 | // Make sure that the generated code is itself legal. |
| 2675 | if (Result != Op) |
| 2676 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2677 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2678 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 2679 | // means that we always must cache transformed nodes. |
| 2680 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2681 | return Result; |
| 2682 | } |
| 2683 | |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 2684 | SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { |
| 2685 | SDValue Vec = Op.getOperand(0); |
| 2686 | SDValue Idx = Op.getOperand(1); |
| 2687 | DebugLoc dl = Op.getDebugLoc(); |
| 2688 | // Store the value to a temporary stack slot, then LOAD the returned part. |
| 2689 | SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); |
| 2690 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, NULL, 0); |
| 2691 | |
| 2692 | // Add the offset to the index. |
Eli Friedman | 2a35b1c | 2009-05-23 23:03:28 +0000 | [diff] [blame] | 2693 | unsigned EltSize = |
| 2694 | Vec.getValueType().getVectorElementType().getSizeInBits()/8; |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 2695 | Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, |
| 2696 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 2697 | |
| 2698 | if (Idx.getValueType().bitsGT(TLI.getPointerTy())) |
| 2699 | Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx); |
| 2700 | else |
| 2701 | Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx); |
| 2702 | |
| 2703 | StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr); |
| 2704 | |
| 2705 | return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, NULL, 0); |
| 2706 | } |
| 2707 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2708 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 2709 | /// with condition CC on the current target. This usually involves legalizing |
| 2710 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 2711 | /// there may be no choice but to create a new SetCC node to represent the |
| 2712 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2713 | /// LHS, and the SDValue returned in RHS has a nil SDNode value. |
| 2714 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS, |
| 2715 | SDValue &RHS, |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 2716 | SDValue &CC, |
| 2717 | DebugLoc dl) { |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2718 | LHS = LegalizeOp(LHS); |
| 2719 | RHS = LegalizeOp(RHS); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 2722 | /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and |
| 2723 | /// condition code CC on the current target. This routine assumes LHS and rHS |
| 2724 | /// have already been legalized by LegalizeSetCCOperands. It expands SETCC with |
| 2725 | /// illegal condition code into AND / OR of multiple SETCC values. |
| 2726 | void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT, |
| 2727 | SDValue &LHS, SDValue &RHS, |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 2728 | SDValue &CC, |
| 2729 | DebugLoc dl) { |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 2730 | MVT OpVT = LHS.getValueType(); |
| 2731 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 2732 | switch (TLI.getCondCodeAction(CCCode, OpVT)) { |
| 2733 | default: assert(0 && "Unknown condition code action!"); |
| 2734 | case TargetLowering::Legal: |
| 2735 | // Nothing to do. |
| 2736 | break; |
| 2737 | case TargetLowering::Expand: { |
| 2738 | ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID; |
| 2739 | unsigned Opc = 0; |
| 2740 | switch (CCCode) { |
| 2741 | default: assert(0 && "Don't know how to expand this condition!"); abort(); |
Dan Gohman | e7d238e | 2008-10-21 03:12:54 +0000 | [diff] [blame] | 2742 | case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2743 | case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2744 | case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2745 | case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2746 | case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2747 | case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 2748 | case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 2749 | case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 2750 | case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 2751 | case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 2752 | case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 2753 | case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 2754 | // FIXME: Implement more expansions. |
| 2755 | } |
| 2756 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 2757 | SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1); |
| 2758 | SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2); |
| 2759 | LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 2760 | RHS = SDValue(); |
| 2761 | CC = SDValue(); |
| 2762 | break; |
| 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2767 | /// EmitStackConvert - Emit a store/load combination to the stack. This stores |
| 2768 | /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does |
| 2769 | /// a load from the stack slot to DestVT, extending it if needed. |
| 2770 | /// The resultant code need not be legal. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2771 | SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, |
| 2772 | MVT SlotVT, |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2773 | MVT DestVT, |
| 2774 | DebugLoc dl) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2775 | // Create the stack frame object. |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2776 | unsigned SrcAlign = |
| 2777 | TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType(). |
| 2778 | getTypeForMVT()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2779 | SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2780 | |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 2781 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2782 | int SPFI = StackPtrFI->getIndex(); |
Dan Gohman | 15b3830 | 2009-01-29 21:02:43 +0000 | [diff] [blame] | 2783 | const Value *SV = PseudoSourceValue::getFixedStack(SPFI); |
| 2784 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2785 | unsigned SrcSize = SrcOp.getValueType().getSizeInBits(); |
| 2786 | unsigned SlotSize = SlotVT.getSizeInBits(); |
| 2787 | unsigned DestSize = DestVT.getSizeInBits(); |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2788 | unsigned DestAlign = |
| 2789 | TLI.getTargetData()->getPrefTypeAlignment(DestVT.getTypeForMVT()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2790 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2791 | // Emit a store to the stack slot. Use a truncstore if the input value is |
| 2792 | // later than DestVT. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2793 | SDValue Store; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2794 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2795 | if (SrcSize > SlotSize) |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2796 | Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, |
Dan Gohman | 15b3830 | 2009-01-29 21:02:43 +0000 | [diff] [blame] | 2797 | SV, 0, SlotVT, false, SrcAlign); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2798 | else { |
| 2799 | assert(SrcSize == SlotSize && "Invalid store"); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2800 | Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, |
Dan Gohman | 15b3830 | 2009-01-29 21:02:43 +0000 | [diff] [blame] | 2801 | SV, 0, false, SrcAlign); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2802 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2803 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2804 | // Result is a load from the stack slot. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2805 | if (SlotSize == DestSize) |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2806 | return DAG.getLoad(DestVT, dl, Store, FIPtr, SV, 0, false, DestAlign); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2807 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2808 | assert(SlotSize < DestSize && "Unknown extension!"); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2809 | return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, SV, 0, SlotVT, |
Mon P Wang | 364d73d | 2008-07-05 20:40:31 +0000 | [diff] [blame] | 2810 | false, DestAlign); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2813 | SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2814 | DebugLoc dl = Node->getDebugLoc(); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 2815 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 2816 | // then load the whole vector back out. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2817 | SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2818 | |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 2819 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2820 | int SPFI = StackPtrFI->getIndex(); |
| 2821 | |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 2822 | SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0), |
| 2823 | StackPtr, |
| 2824 | PseudoSourceValue::getFixedStack(SPFI), 0, |
| 2825 | Node->getValueType(0).getVectorElementType()); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2826 | return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 2827 | PseudoSourceValue::getFixedStack(SPFI), 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2831 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 2832 | /// support the operation, but do support the resultant vector type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2833 | SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2834 | unsigned NumElems = Node->getNumOperands(); |
| 2835 | SDValue SplatValue = Node->getOperand(0); |
| 2836 | DebugLoc dl = Node->getDebugLoc(); |
| 2837 | MVT VT = Node->getValueType(0); |
| 2838 | MVT OpVT = SplatValue.getValueType(); |
| 2839 | MVT EltVT = VT.getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2840 | |
| 2841 | // If the only non-undef value is the low element, turn this into a |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2842 | // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2843 | bool isOnlyLowElement = true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2844 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2845 | // FIXME: it would be far nicer to change this into map<SDValue,uint64_t> |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2846 | // and use a bitmask instead of a list of elements. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2847 | // FIXME: this doesn't treat <0, u, 0, u> for example, as a splat. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2848 | std::map<SDValue, std::vector<unsigned> > Values; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2849 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2850 | bool isConstant = true; |
| 2851 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 2852 | SplatValue.getOpcode() != ISD::UNDEF) |
| 2853 | isConstant = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2854 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2855 | for (unsigned i = 1; i < NumElems; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2856 | SDValue V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 2857 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2858 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2859 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2860 | if (SplatValue != V) |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2861 | SplatValue = SDValue(0, 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2862 | |
| 2863 | // If this isn't a constant element or an undef, we can't use a constant |
| 2864 | // pool load. |
| 2865 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 2866 | V.getOpcode() != ISD::UNDEF) |
| 2867 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2868 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2869 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2870 | if (isOnlyLowElement) { |
| 2871 | // If the low element is an undef too, then this whole things is an undef. |
| 2872 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2873 | return DAG.getUNDEF(VT); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2874 | // Otherwise, turn this into a scalar_to_vector node. |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2875 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2876 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2877 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2878 | // If all elements are constants, create a load from the constant pool. |
| 2879 | if (isConstant) { |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2880 | std::vector<Constant*> CV; |
| 2881 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2882 | if (ConstantFPSDNode *V = |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2883 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 2884 | CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2885 | } else if (ConstantSDNode *V = |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2886 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 2887 | CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2888 | } else { |
| 2889 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2890 | const Type *OpNTy = OpVT.getTypeForMVT(); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2891 | CV.push_back(UndefValue::get(OpNTy)); |
| 2892 | } |
| 2893 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2894 | Constant *CP = ConstantVector::get(CV); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2895 | SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 2896 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2897 | return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 2898 | PseudoSourceValue::getConstantPool(), 0, |
| 2899 | false, Alignment); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2900 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2901 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2902 | if (SplatValue.getNode()) { // Splat of one value? |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2903 | // Build the shuffle constant vector: <0, 0, 0, 0> |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2904 | SmallVector<int, 8> ZeroVec(NumElems, 0); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2905 | |
| 2906 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2907 | if (TLI.isShuffleMaskLegal(ZeroVec, Node->getValueType(0))) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2908 | // Get the splatted value into the low element of a vector register. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2909 | SDValue LowValVec = |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2910 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, SplatValue); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2911 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2912 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2913 | return DAG.getVectorShuffle(VT, dl, LowValVec, DAG.getUNDEF(VT), |
| 2914 | &ZeroVec[0]); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2917 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2918 | // If there are only two unique elements, we may be able to turn this into a |
| 2919 | // vector shuffle. |
| 2920 | if (Values.size() == 2) { |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2921 | // Get the two values in deterministic order. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2922 | SDValue Val1 = Node->getOperand(1); |
| 2923 | SDValue Val2; |
| 2924 | std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin(); |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2925 | if (MI->first != Val1) |
| 2926 | Val2 = MI->first; |
| 2927 | else |
| 2928 | Val2 = (++MI)->first; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2929 | |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2930 | // If Val1 is an undef, make sure it ends up as Val2, to ensure that our |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2931 | // vector shuffle has the undef vector on the RHS. |
| 2932 | if (Val1.getOpcode() == ISD::UNDEF) |
| 2933 | std::swap(Val1, Val2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2934 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2935 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2936 | SmallVector<int, 8> ShuffleMask(NumElems, -1); |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2937 | |
| 2938 | // Set elements of the shuffle mask for Val1. |
| 2939 | std::vector<unsigned> &Val1Elts = Values[Val1]; |
| 2940 | for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2941 | ShuffleMask[Val1Elts[i]] = 0; |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2942 | |
| 2943 | // Set elements of the shuffle mask for Val2. |
| 2944 | std::vector<unsigned> &Val2Elts = Values[Val2]; |
| 2945 | for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i) |
| 2946 | if (Val2.getOpcode() != ISD::UNDEF) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2947 | ShuffleMask[Val2Elts[i]] = NumElems; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2948 | |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 2949 | // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it. |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2950 | if (TLI.isOperationLegalOrCustom(ISD::SCALAR_TO_VECTOR, VT) && |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2951 | TLI.isShuffleMaskLegal(ShuffleMask, VT)) { |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2952 | Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val1); |
| 2953 | Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Val2); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2954 | return DAG.getVectorShuffle(VT, dl, Val1, Val2, &ShuffleMask[0]); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2955 | } |
| 2956 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2957 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2958 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 2959 | // aligned object on the stack, store each element into it, then load |
| 2960 | // the result as a vector. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2961 | // Create the stack frame object. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2962 | SDValue FIPtr = DAG.CreateStackTemporary(VT); |
Dan Gohman | 15b3830 | 2009-01-29 21:02:43 +0000 | [diff] [blame] | 2963 | int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); |
| 2964 | const Value *SV = PseudoSourceValue::getFixedStack(FI); |
| 2965 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2966 | // Emit a store of each element to the stack slot. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2967 | SmallVector<SDValue, 8> Stores; |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2968 | unsigned TypeByteSize = OpVT.getSizeInBits() / 8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2969 | // Store (in the right endianness) the elements to memory. |
| 2970 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2971 | // Ignore undef elements. |
| 2972 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2973 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 2974 | unsigned Offset = TypeByteSize*i; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2975 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2976 | SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2977 | Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2978 | |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2979 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i), |
| 2980 | Idx, SV, Offset)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2981 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2982 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2983 | SDValue StoreChain; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2984 | if (!Stores.empty()) // Not all undef elements? |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2985 | StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2986 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2987 | else |
| 2988 | StoreChain = DAG.getEntryNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2989 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2990 | // Result is a load from the stack slot. |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2991 | return DAG.getLoad(VT, dl, StoreChain, FIPtr, SV, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2994 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 2995 | // does not fit into a register, return the lo part and set the hi part to the |
| 2996 | // by-reg argument. If it does fit into a single register, return the result |
| 2997 | // and leave the Hi part unset. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2998 | SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, |
| 2999 | bool isSigned, SDValue &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3000 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3001 | // The input chain to this libcall is the entry node of the function. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3002 | // Legalizing the call will automatically add the previous call to the |
| 3003 | // dependence. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3004 | SDValue InChain = DAG.getEntryNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3005 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3006 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3007 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3008 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3009 | MVT ArgVT = Node->getOperand(i).getValueType(); |
| 3010 | const Type *ArgTy = ArgVT.getTypeForMVT(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3011 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 3012 | Entry.isSExt = isSigned; |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 3013 | Entry.isZExt = !isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3014 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3015 | } |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 3016 | SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 3017 | TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3019 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3020 | const Type *RetTy = Node->getValueType(0).getTypeForMVT(); |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 3021 | std::pair<SDValue, SDValue> CallInfo = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 3022 | TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 3023 | CallingConv::C, false, Callee, Args, DAG, |
| 3024 | Node->getDebugLoc()); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 3025 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3026 | // Legalize the call sequence, starting with the chain. This will advance |
| 3027 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 3028 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 3029 | LegalizeOp(CallInfo.second); |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 3030 | return CallInfo.first; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3033 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 3034 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 3035 | /// we expand it. At this point, we know that the result and operand types are |
| 3036 | /// legal for the target. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3037 | SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 3038 | SDValue Op0, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3039 | MVT DestVT, |
| 3040 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3041 | if (Op0.getValueType() == MVT::i32) { |
| 3042 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3043 | |
Chris Lattner | 23594d4 | 2008-01-16 07:03:22 +0000 | [diff] [blame] | 3044 | // Get the stack frame index of a 8 byte buffer. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3045 | SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3046 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3047 | // word offset constant for Hi/Lo address computation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3048 | SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3049 | // set up Hi and Lo (into buffer) address based on endian |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3050 | SDValue Hi = StackSlot; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3051 | SDValue Lo = DAG.getNode(ISD::ADD, dl, |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 3052 | TLI.getPointerTy(), StackSlot, WordOff); |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 3053 | if (TLI.isLittleEndian()) |
| 3054 | std::swap(Hi, Lo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3056 | // if signed map to unsigned space |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3057 | SDValue Op0Mapped; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3058 | if (isSigned) { |
| 3059 | // constant used to invert sign bit (signed to unsigned mapping) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3060 | SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3061 | Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3062 | } else { |
| 3063 | Op0Mapped = Op0; |
| 3064 | } |
| 3065 | // store the lo of the constructed double - based on integer input |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3066 | SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 3067 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3068 | // initial hi portion of constructed double |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3069 | SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3070 | // store the hi of the constructed double - biased exponent |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3071 | SDValue Store2=DAG.getStore(Store1, dl, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3072 | // load the constructed double |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3073 | SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3074 | // FP constant to bias correct the final result |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3075 | SDValue Bias = DAG.getConstantFP(isSigned ? |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 3076 | BitsToDouble(0x4330000080000000ULL) : |
| 3077 | BitsToDouble(0x4330000000000000ULL), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3078 | MVT::f64); |
| 3079 | // subtract the bias |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3080 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3081 | // final result |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3082 | SDValue Result; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3083 | // handle final rounding |
| 3084 | if (DestVT == MVT::f64) { |
| 3085 | // do nothing |
| 3086 | Result = Sub; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3087 | } else if (DestVT.bitsLT(MVT::f64)) { |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3088 | Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3089 | DAG.getIntPtrConstant(0)); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3090 | } else if (DestVT.bitsGT(MVT::f64)) { |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3091 | Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3092 | } |
| 3093 | return Result; |
| 3094 | } |
| 3095 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3096 | SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3097 | |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3098 | SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()), |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 3099 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 3100 | ISD::SETLT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3101 | SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3102 | SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3103 | SignSet, Four, Zero); |
| 3104 | |
| 3105 | // If the sign bit of the integer is set, the large number will be treated |
| 3106 | // as a negative number. To counteract this, the dynamic code adds an |
| 3107 | // offset depending on the data type. |
| 3108 | uint64_t FF; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3109 | switch (Op0.getValueType().getSimpleVT()) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3110 | default: assert(0 && "Unsupported integer type!"); |
| 3111 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 3112 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 3113 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 3114 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 3115 | } |
| 3116 | if (TLI.isLittleEndian()) FF <<= 32; |
Chris Lattner | d2e936a | 2009-03-08 01:47:41 +0000 | [diff] [blame] | 3117 | Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3118 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3119 | SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 3120 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3121 | CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset); |
Dan Gohman | 87a0f10 | 2008-09-22 22:40:08 +0000 | [diff] [blame] | 3122 | Alignment = std::min(Alignment, 4u); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3123 | SDValue FudgeInReg; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3124 | if (DestVT == MVT::f32) |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3125 | FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 3126 | PseudoSourceValue::getConstantPool(), 0, |
| 3127 | false, Alignment); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3128 | else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3129 | FudgeInReg = |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3130 | LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3131 | DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 3132 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 3133 | MVT::f32, false, Alignment)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3136 | return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 3140 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 3141 | /// we promote it. At this point, we know that the result and operand types are |
| 3142 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 3143 | /// operation that takes a larger input. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3144 | SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, |
| 3145 | MVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3146 | bool isSigned, |
| 3147 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3148 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3149 | MVT NewInTy = LegalOp.getValueType(); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3150 | |
| 3151 | unsigned OpToUse = 0; |
| 3152 | |
| 3153 | // Scan for the appropriate larger type to use. |
| 3154 | while (1) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3155 | NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1); |
| 3156 | assert(NewInTy.isInteger() && "Ran out of possibilities!"); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3157 | |
| 3158 | // If the target supports SINT_TO_FP of this type, use it. |
| 3159 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 3160 | default: break; |
| 3161 | case TargetLowering::Legal: |
| 3162 | if (!TLI.isTypeLegal(NewInTy)) |
| 3163 | break; // Can't use this datatype. |
| 3164 | // FALL THROUGH. |
| 3165 | case TargetLowering::Custom: |
| 3166 | OpToUse = ISD::SINT_TO_FP; |
| 3167 | break; |
| 3168 | } |
| 3169 | if (OpToUse) break; |
| 3170 | if (isSigned) continue; |
| 3171 | |
| 3172 | // If the target supports UINT_TO_FP of this type, use it. |
| 3173 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 3174 | default: break; |
| 3175 | case TargetLowering::Legal: |
| 3176 | if (!TLI.isTypeLegal(NewInTy)) |
| 3177 | break; // Can't use this datatype. |
| 3178 | // FALL THROUGH. |
| 3179 | case TargetLowering::Custom: |
| 3180 | OpToUse = ISD::UINT_TO_FP; |
| 3181 | break; |
| 3182 | } |
| 3183 | if (OpToUse) break; |
| 3184 | |
| 3185 | // Otherwise, try a larger type. |
| 3186 | } |
| 3187 | |
| 3188 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 3189 | // desired type then run the operation on it. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3190 | return DAG.getNode(OpToUse, dl, DestVT, |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3191 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3192 | dl, NewInTy, LegalOp)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 3196 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 3197 | /// we promote it. At this point, we know that the result and operand types are |
| 3198 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 3199 | /// operation that returns a larger result. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3200 | SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, |
| 3201 | MVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3202 | bool isSigned, |
| 3203 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3204 | // First step, figure out the appropriate FP_TO*INT operation to use. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3205 | MVT NewOutTy = DestVT; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3206 | |
| 3207 | unsigned OpToUse = 0; |
| 3208 | |
| 3209 | // Scan for the appropriate larger type to use. |
| 3210 | while (1) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3211 | NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1); |
| 3212 | assert(NewOutTy.isInteger() && "Ran out of possibilities!"); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3213 | |
| 3214 | // If the target supports FP_TO_SINT returning this type, use it. |
| 3215 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 3216 | default: break; |
| 3217 | case TargetLowering::Legal: |
| 3218 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3219 | break; // Can't use this datatype. |
| 3220 | // FALL THROUGH. |
| 3221 | case TargetLowering::Custom: |
| 3222 | OpToUse = ISD::FP_TO_SINT; |
| 3223 | break; |
| 3224 | } |
| 3225 | if (OpToUse) break; |
| 3226 | |
| 3227 | // If the target supports FP_TO_UINT of this type, use it. |
| 3228 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 3229 | default: break; |
| 3230 | case TargetLowering::Legal: |
| 3231 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3232 | break; // Can't use this datatype. |
| 3233 | // FALL THROUGH. |
| 3234 | case TargetLowering::Custom: |
| 3235 | OpToUse = ISD::FP_TO_UINT; |
| 3236 | break; |
| 3237 | } |
| 3238 | if (OpToUse) break; |
| 3239 | |
| 3240 | // Otherwise, try a larger type. |
| 3241 | } |
| 3242 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3244 | // Okay, we found the operation and type to use. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3245 | SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); |
Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 3246 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3247 | // If the operation produces an invalid type, it must be custom lowered. Use |
| 3248 | // the target lowering hooks to expand it. Just keep the low part of the |
| 3249 | // expanded operation, we know that we're truncating anyway. |
| 3250 | if (getTypeAction(NewOutTy) == Expand) { |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 3251 | SmallVector<SDValue, 2> Results; |
| 3252 | TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG); |
| 3253 | assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!"); |
| 3254 | Operation = Results[0]; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3255 | } |
Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 3256 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 3257 | // Truncate the result of the extended FP_TO_*INT operation to the desired |
| 3258 | // size. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 3259 | return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 3263 | /// |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3264 | SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3265 | MVT VT = Op.getValueType(); |
| 3266 | MVT SHVT = TLI.getShiftAmountTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3267 | SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3268 | switch (VT.getSimpleVT()) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3269 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 3270 | case MVT::i16: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3271 | Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3272 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3273 | return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3274 | case MVT::i32: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3275 | Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 3276 | Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3277 | Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3278 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 3279 | Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 3280 | Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 3281 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); |
| 3282 | Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); |
| 3283 | return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3284 | case MVT::i64: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3285 | Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT)); |
| 3286 | Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT)); |
| 3287 | Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 3288 | Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3289 | Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 3290 | Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 3291 | Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT)); |
| 3292 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT)); |
| 3293 | Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 3294 | Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 3295 | Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 3296 | Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 3297 | Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 3298 | Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 3299 | Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); |
| 3300 | Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); |
| 3301 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); |
| 3302 | Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); |
| 3303 | Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6); |
| 3304 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); |
| 3305 | return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 3310 | /// |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3311 | SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op, |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3312 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3313 | switch (Opc) { |
| 3314 | default: assert(0 && "Cannot expand this yet!"); |
| 3315 | case ISD::CTPOP: { |
| 3316 | static const uint64_t mask[6] = { |
| 3317 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 3318 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 3319 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 3320 | }; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3321 | MVT VT = Op.getValueType(); |
| 3322 | MVT ShVT = TLI.getShiftAmountTy(); |
| 3323 | unsigned len = VT.getSizeInBits(); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3324 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 3325 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3326 | unsigned EltSize = VT.isVector() ? |
| 3327 | VT.getVectorElementType().getSizeInBits() : len; |
| 3328 | SDValue Tmp2 = DAG.getConstant(APInt(EltSize, mask[i]), VT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3329 | SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3330 | Op = DAG.getNode(ISD::ADD, dl, VT, |
Dale Johannesen | e72c596 | 2009-02-06 21:55:48 +0000 | [diff] [blame] | 3331 | DAG.getNode(ISD::AND, dl, VT, Op, Tmp2), |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3332 | DAG.getNode(ISD::AND, dl, VT, |
| 3333 | DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3), |
| 3334 | Tmp2)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3335 | } |
| 3336 | return Op; |
| 3337 | } |
| 3338 | case ISD::CTLZ: { |
| 3339 | // for now, we do this: |
| 3340 | // x = x | (x >> 1); |
| 3341 | // x = x | (x >> 2); |
| 3342 | // ... |
| 3343 | // x = x | (x >>16); |
| 3344 | // x = x | (x >>32); // for 64-bit input |
| 3345 | // return popcount(~x); |
| 3346 | // |
| 3347 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3348 | MVT VT = Op.getValueType(); |
| 3349 | MVT ShVT = TLI.getShiftAmountTy(); |
| 3350 | unsigned len = VT.getSizeInBits(); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3351 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3352 | SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3353 | Op = DAG.getNode(ISD::OR, dl, VT, Op, |
Dale Johannesen | e72c596 | 2009-02-06 21:55:48 +0000 | [diff] [blame] | 3354 | DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3355 | } |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3356 | Op = DAG.getNOT(dl, Op, VT); |
| 3357 | return DAG.getNode(ISD::CTPOP, dl, VT, Op); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3358 | } |
| 3359 | case ISD::CTTZ: { |
| 3360 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 3361 | // unless the target has ctlz but not ctpop, in which case we use: |
| 3362 | // { return 32 - nlz(~x & (x-1)); } |
| 3363 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3364 | MVT VT = Op.getValueType(); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3365 | SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT, |
| 3366 | DAG.getNOT(dl, Op, VT), |
| 3367 | DAG.getNode(ISD::SUB, dl, VT, Op, |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 3368 | DAG.getConstant(1, VT))); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3369 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 3370 | if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) && |
| 3371 | TLI.isOperationLegalOrCustom(ISD::CTLZ, VT)) |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3372 | return DAG.getNode(ISD::SUB, dl, VT, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3373 | DAG.getConstant(VT.getSizeInBits(), VT), |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 3374 | DAG.getNode(ISD::CTLZ, dl, VT, Tmp3)); |
| 3375 | return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3379 | |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3380 | void SelectionDAGLegalize::ExpandNode(SDNode *Node, |
| 3381 | SmallVectorImpl<SDValue> &Results) { |
| 3382 | DebugLoc dl = Node->getDebugLoc(); |
| 3383 | SDValue Tmp1, Tmp2; |
| 3384 | switch (Node->getOpcode()) { |
| 3385 | case ISD::CTPOP: |
| 3386 | case ISD::CTLZ: |
| 3387 | case ISD::CTTZ: |
| 3388 | Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl); |
| 3389 | Results.push_back(Tmp1); |
| 3390 | break; |
| 3391 | case ISD::BSWAP: |
| 3392 | Results.push_back(ExpandBSWAP(Node->getOperand(0), dl)); |
| 3393 | break; |
| 3394 | case ISD::FRAMEADDR: |
| 3395 | case ISD::RETURNADDR: |
| 3396 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 3397 | Results.push_back(DAG.getConstant(0, Node->getValueType(0))); |
| 3398 | break; |
| 3399 | case ISD::FLT_ROUNDS_: |
| 3400 | Results.push_back(DAG.getConstant(1, Node->getValueType(0))); |
| 3401 | break; |
| 3402 | case ISD::EH_RETURN: |
| 3403 | case ISD::DECLARE: |
| 3404 | case ISD::DBG_LABEL: |
| 3405 | case ISD::EH_LABEL: |
| 3406 | case ISD::PREFETCH: |
| 3407 | case ISD::MEMBARRIER: |
| 3408 | case ISD::VAEND: |
| 3409 | Results.push_back(Node->getOperand(0)); |
| 3410 | break; |
| 3411 | case ISD::MERGE_VALUES: |
| 3412 | for (unsigned i = 0; i < Node->getNumValues(); i++) |
| 3413 | Results.push_back(Node->getOperand(i)); |
| 3414 | break; |
| 3415 | case ISD::UNDEF: { |
| 3416 | MVT VT = Node->getValueType(0); |
| 3417 | if (VT.isInteger()) |
| 3418 | Results.push_back(DAG.getConstant(0, VT)); |
| 3419 | else if (VT.isFloatingPoint()) |
| 3420 | Results.push_back(DAG.getConstantFP(0, VT)); |
| 3421 | else |
| 3422 | assert(0 && "Unknown value type!"); |
| 3423 | break; |
| 3424 | } |
| 3425 | case ISD::TRAP: { |
| 3426 | // If this operation is not supported, lower it to 'abort()' call |
| 3427 | TargetLowering::ArgListTy Args; |
| 3428 | std::pair<SDValue, SDValue> CallResult = |
| 3429 | TLI.LowerCallTo(Node->getOperand(0), Type::VoidTy, |
| 3430 | false, false, false, false, CallingConv::C, false, |
| 3431 | DAG.getExternalSymbol("abort", TLI.getPointerTy()), |
| 3432 | Args, DAG, dl); |
| 3433 | Results.push_back(CallResult.second); |
| 3434 | break; |
| 3435 | } |
| 3436 | case ISD::FP_ROUND: |
| 3437 | case ISD::BIT_CONVERT: |
| 3438 | Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3439 | Node->getValueType(0), dl); |
| 3440 | Results.push_back(Tmp1); |
| 3441 | break; |
| 3442 | case ISD::FP_EXTEND: |
| 3443 | Tmp1 = EmitStackConvert(Node->getOperand(0), |
| 3444 | Node->getOperand(0).getValueType(), |
| 3445 | Node->getValueType(0), dl); |
| 3446 | Results.push_back(Tmp1); |
| 3447 | break; |
| 3448 | case ISD::SIGN_EXTEND_INREG: { |
| 3449 | // NOTE: we could fall back on load/store here too for targets without |
| 3450 | // SAR. However, it is doubtful that any exist. |
| 3451 | MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
| 3452 | unsigned BitsDiff = Node->getValueType(0).getSizeInBits() - |
| 3453 | ExtraVT.getSizeInBits(); |
| 3454 | SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
| 3455 | Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), |
| 3456 | Node->getOperand(0), ShiftCst); |
| 3457 | Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); |
| 3458 | Results.push_back(Tmp1); |
| 3459 | break; |
| 3460 | } |
| 3461 | case ISD::FP_ROUND_INREG: { |
| 3462 | // The only way we can lower this is to turn it into a TRUNCSTORE, |
| 3463 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3464 | |
| 3465 | // NOTE: there is a choice here between constantly creating new stack |
| 3466 | // slots and always reusing the same one. We currently always create |
| 3467 | // new ones, as reuse may inhibit scheduling. |
| 3468 | MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
| 3469 | Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT, |
| 3470 | Node->getValueType(0), dl); |
| 3471 | Results.push_back(Tmp1); |
| 3472 | break; |
| 3473 | } |
| 3474 | case ISD::SINT_TO_FP: |
| 3475 | case ISD::UINT_TO_FP: |
| 3476 | Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 3477 | Node->getOperand(0), Node->getValueType(0), dl); |
| 3478 | Results.push_back(Tmp1); |
| 3479 | break; |
| 3480 | case ISD::FP_TO_UINT: { |
| 3481 | SDValue True, False; |
| 3482 | MVT VT = Node->getOperand(0).getValueType(); |
| 3483 | MVT NVT = Node->getValueType(0); |
| 3484 | const uint64_t zero[] = {0, 0}; |
| 3485 | APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero)); |
| 3486 | APInt x = APInt::getSignBit(NVT.getSizeInBits()); |
| 3487 | (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven); |
| 3488 | Tmp1 = DAG.getConstantFP(apf, VT); |
| 3489 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), |
| 3490 | Node->getOperand(0), |
| 3491 | Tmp1, ISD::SETLT); |
| 3492 | True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0)); |
| 3493 | False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, |
| 3494 | DAG.getNode(ISD::FSUB, dl, VT, |
| 3495 | Node->getOperand(0), Tmp1)); |
| 3496 | False = DAG.getNode(ISD::XOR, dl, NVT, False, |
| 3497 | DAG.getConstant(x, NVT)); |
| 3498 | Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False); |
| 3499 | Results.push_back(Tmp1); |
| 3500 | break; |
| 3501 | } |
| 3502 | case ISD::VACOPY: { |
| 3503 | // This defaults to loading a pointer from the input and storing it to the |
| 3504 | // output, returning the chain. |
| 3505 | const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue(); |
| 3506 | const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue(); |
| 3507 | Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0), |
| 3508 | Node->getOperand(2), VS, 0); |
| 3509 | Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), VD, 0); |
| 3510 | Results.push_back(Tmp1); |
| 3511 | break; |
| 3512 | } |
| 3513 | case ISD::EXTRACT_VECTOR_ELT: |
| 3514 | if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) |
| 3515 | // This must be an access of the only element. Return it. |
| 3516 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, dl, Node->getValueType(0), |
| 3517 | Node->getOperand(0)); |
| 3518 | else |
| 3519 | Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); |
| 3520 | Results.push_back(Tmp1); |
| 3521 | break; |
| 3522 | case ISD::EXTRACT_SUBVECTOR: |
| 3523 | Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); |
| 3524 | break; |
| 3525 | case ISD::SCALAR_TO_VECTOR: |
| 3526 | Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); |
| 3527 | break; |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame^] | 3528 | case ISD::INSERT_VECTOR_ELT: |
| 3529 | Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), |
| 3530 | Node->getOperand(1), |
| 3531 | Node->getOperand(2), dl)); |
| 3532 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3533 | case ISD::EXTRACT_ELEMENT: { |
| 3534 | MVT OpTy = Node->getOperand(0).getValueType(); |
| 3535 | if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { |
| 3536 | // 1 -> Hi |
| 3537 | Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), |
| 3538 | DAG.getConstant(OpTy.getSizeInBits()/2, |
| 3539 | TLI.getShiftAmountTy())); |
| 3540 | Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); |
| 3541 | } else { |
| 3542 | // 0 -> Lo |
| 3543 | Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), |
| 3544 | Node->getOperand(0)); |
| 3545 | } |
| 3546 | Results.push_back(Tmp1); |
| 3547 | break; |
| 3548 | } |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame^] | 3549 | case ISD::STACKSAVE: |
| 3550 | // Expand to CopyFromReg if the target set |
| 3551 | // StackPointerRegisterToSaveRestore. |
| 3552 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 3553 | Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, |
| 3554 | Node->getValueType(0))); |
| 3555 | Results.push_back(Results[0].getValue(1)); |
| 3556 | } else { |
| 3557 | Results.push_back(DAG.getUNDEF(Node->getValueType(0))); |
| 3558 | Results.push_back(Node->getOperand(0)); |
| 3559 | } |
| 3560 | break; |
| 3561 | case ISD::STACKRESTORE: |
| 3562 | // Expand to CopyToReg if the target set |
| 3563 | // StackPointerRegisterToSaveRestore. |
| 3564 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 3565 | Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, |
| 3566 | Node->getOperand(1))); |
| 3567 | } else { |
| 3568 | Results.push_back(Node->getOperand(0)); |
| 3569 | } |
| 3570 | break; |
| 3571 | case ISD::GLOBAL_OFFSET_TABLE: |
| 3572 | case ISD::GlobalAddress: |
| 3573 | case ISD::GlobalTLSAddress: |
| 3574 | case ISD::ExternalSymbol: |
| 3575 | case ISD::ConstantPool: |
| 3576 | case ISD::JumpTable: |
| 3577 | case ISD::INTRINSIC_W_CHAIN: |
| 3578 | case ISD::INTRINSIC_WO_CHAIN: |
| 3579 | case ISD::INTRINSIC_VOID: |
| 3580 | // FIXME: Custom lowering for these operations shouldn't return null! |
| 3581 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 3582 | Results.push_back(SDValue(Node, i)); |
| 3583 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | void SelectionDAGLegalize::PromoteNode(SDNode *Node, |
| 3587 | SmallVectorImpl<SDValue> &Results) { |
| 3588 | MVT OVT = Node->getValueType(0); |
| 3589 | if (Node->getOpcode() == ISD::UINT_TO_FP || |
| 3590 | Node->getOpcode() == ISD::SINT_TO_FP) { |
| 3591 | OVT = Node->getOperand(0).getValueType(); |
| 3592 | } |
| 3593 | MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 3594 | DebugLoc dl = Node->getDebugLoc(); |
| 3595 | SDValue Tmp1, Tmp2; |
| 3596 | switch (Node->getOpcode()) { |
| 3597 | case ISD::CTTZ: |
| 3598 | case ISD::CTLZ: |
| 3599 | case ISD::CTPOP: |
| 3600 | // Zero extend the argument. |
| 3601 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); |
| 3602 | // Perform the larger operation. |
| 3603 | Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1); |
| 3604 | if (Node->getOpcode() == ISD::CTTZ) { |
| 3605 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
| 3606 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), |
| 3607 | Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT), |
| 3608 | ISD::SETEQ); |
| 3609 | Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, |
| 3610 | DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1); |
| 3611 | } else if (Node->getOpcode() == ISD::CTLZ) { |
| 3612 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
| 3613 | Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, |
| 3614 | DAG.getConstant(NVT.getSizeInBits() - |
| 3615 | OVT.getSizeInBits(), NVT)); |
| 3616 | } |
| 3617 | Results.push_back(Tmp1); |
| 3618 | break; |
| 3619 | case ISD::BSWAP: { |
| 3620 | unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); |
| 3621 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Tmp1); |
| 3622 | Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1); |
| 3623 | Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, |
| 3624 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 3625 | Results.push_back(Tmp1); |
| 3626 | break; |
| 3627 | } |
| 3628 | case ISD::FP_TO_UINT: |
| 3629 | case ISD::FP_TO_SINT: |
| 3630 | Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0), |
| 3631 | Node->getOpcode() == ISD::FP_TO_SINT, dl); |
| 3632 | Results.push_back(Tmp1); |
| 3633 | break; |
| 3634 | case ISD::UINT_TO_FP: |
| 3635 | case ISD::SINT_TO_FP: |
| 3636 | Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0), |
| 3637 | Node->getOpcode() == ISD::SINT_TO_FP, dl); |
| 3638 | Results.push_back(Tmp1); |
| 3639 | break; |
| 3640 | } |
| 3641 | } |
| 3642 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3643 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 3644 | // |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3645 | void SelectionDAG::Legalize(bool TypesNeedLegalizing, |
| 3646 | CodeGenOpt::Level OptLevel) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3647 | /// run - This is the main entry point to this class. |
| 3648 | /// |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 3649 | SelectionDAGLegalize(*this, OptLevel).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |