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