Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Instructions.h" |
Dan Gohman | d2a251f | 2009-07-17 21:33:58 +0000 | [diff] [blame] | 19 | #include "llvm/Operator.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // CallSite Class |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 30 | #define CALLSITE_DELEGATE_GETTER(METHOD) \ |
| 31 | Instruction *II(getInstruction()); \ |
| 32 | return isCall() \ |
| 33 | ? cast<CallInst>(II)->METHOD \ |
| 34 | : cast<InvokeInst>(II)->METHOD |
| 35 | |
| 36 | #define CALLSITE_DELEGATE_SETTER(METHOD) \ |
| 37 | Instruction *II(getInstruction()); \ |
| 38 | if (isCall()) \ |
| 39 | cast<CallInst>(II)->METHOD; \ |
| 40 | else \ |
| 41 | cast<InvokeInst>(II)->METHOD |
| 42 | |
Duncan Sands | 85fab3a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 43 | CallSite::CallSite(Instruction *C) { |
| 44 | assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!"); |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 45 | I.setPointer(C); |
| 46 | I.setInt(isa<CallInst>(C)); |
Duncan Sands | 85fab3a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 47 | } |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 48 | CallingConv::ID CallSite::getCallingConv() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 49 | CALLSITE_DELEGATE_GETTER(getCallingConv()); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 50 | } |
Sandeep Patel | 68c5f47 | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 51 | void CallSite::setCallingConv(CallingConv::ID CC) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 52 | CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 53 | } |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 54 | const AttrListPtr &CallSite::getAttributes() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 55 | CALLSITE_DELEGATE_GETTER(getAttributes()); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 56 | } |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 57 | void CallSite::setAttributes(const AttrListPtr &PAL) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 58 | CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 59 | } |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 60 | bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 61 | CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr)); |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 62 | } |
Dale Johannesen | eabc5f3 | 2008-02-22 17:49:45 +0000 | [diff] [blame] | 63 | uint16_t CallSite::getParamAlignment(uint16_t i) const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 64 | CALLSITE_DELEGATE_GETTER(getParamAlignment(i)); |
Dale Johannesen | eabc5f3 | 2008-02-22 17:49:45 +0000 | [diff] [blame] | 65 | } |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 66 | bool CallSite::doesNotAccessMemory() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 67 | CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 68 | } |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 69 | void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 70 | CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory)); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 71 | } |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 72 | bool CallSite::onlyReadsMemory() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 73 | CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 74 | } |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 75 | void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 76 | CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory)); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 77 | } |
| 78 | bool CallSite::doesNotReturn() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 79 | CALLSITE_DELEGATE_GETTER(doesNotReturn()); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 80 | } |
| 81 | void CallSite::setDoesNotReturn(bool doesNotReturn) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 82 | CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn)); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 83 | } |
Duncan Sands | 3353ed0 | 2007-12-18 09:59:50 +0000 | [diff] [blame] | 84 | bool CallSite::doesNotThrow() const { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 85 | CALLSITE_DELEGATE_GETTER(doesNotThrow()); |
Duncan Sands | 8e4847e | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 86 | } |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 87 | void CallSite::setDoesNotThrow(bool doesNotThrow) { |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 88 | CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow)); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 89 | } |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 90 | |
Matthijs Kooijman | b0dffd6 | 2008-06-05 08:04:58 +0000 | [diff] [blame] | 91 | bool CallSite::hasArgument(const Value *Arg) const { |
Matthijs Kooijman | d901e58 | 2008-06-04 16:31:12 +0000 | [diff] [blame] | 92 | for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI) |
| 93 | if (AI->get() == Arg) |
| 94 | return true; |
| 95 | return false; |
| 96 | } |
| 97 | |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 98 | #undef CALLSITE_DELEGATE_GETTER |
| 99 | #undef CALLSITE_DELEGATE_SETTER |
| 100 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
| 102 | // TerminatorInst Class |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
| 105 | // Out of line virtual method, so the vtable, etc has a home. |
| 106 | TerminatorInst::~TerminatorInst() { |
| 107 | } |
| 108 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | // UnaryInstruction Class |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 113 | // Out of line virtual method, so the vtable, etc has a home. |
| 114 | UnaryInstruction::~UnaryInstruction() { |
| 115 | } |
| 116 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 118 | // SelectInst Class |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
| 121 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 122 | /// for a select operation, otherwise return null. |
| 123 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 124 | if (Op1->getType() != Op2->getType()) |
| 125 | return "both values to select must have same type"; |
| 126 | |
| 127 | if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
| 128 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 129 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 130 | return "vector select condition element type must be i1"; |
| 131 | const VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
| 132 | if (ET == 0) |
| 133 | return "selected values for vector select must be vectors"; |
| 134 | if (ET->getNumElements() != VT->getNumElements()) |
| 135 | return "vector select requires selected vectors to have " |
| 136 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 137 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 138 | return "select condition must be i1 or <n x i1>"; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 145 | // PHINode Class |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | |
| 148 | PHINode::PHINode(const PHINode &PN) |
| 149 | : Instruction(PN.getType(), Instruction::PHI, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 150 | allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 151 | ReservedSpace(PN.getNumOperands()) { |
| 152 | Use *OL = OperandList; |
| 153 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 154 | OL[i] = PN.getOperand(i); |
| 155 | OL[i+1] = PN.getOperand(i+1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 156 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 157 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 160 | PHINode::~PHINode() { |
Chris Lattner | 7d6aac8 | 2008-06-16 04:02:40 +0000 | [diff] [blame] | 161 | if (OperandList) |
| 162 | dropHungoffUses(OperandList); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 166 | // predecessor basic block is deleted. |
| 167 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 168 | unsigned NumOps = getNumOperands(); |
| 169 | Use *OL = OperandList; |
| 170 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 171 | Value *Removed = OL[Idx*2]; |
| 172 | |
| 173 | // Move everything after this operand down. |
| 174 | // |
| 175 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 176 | // client might not expect this to happen. The code as it is thrashes the |
| 177 | // use/def lists, which is kinda lame. |
| 178 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 179 | OL[i-2] = OL[i]; |
| 180 | OL[i-2+1] = OL[i+1]; |
| 181 | } |
| 182 | |
| 183 | // Nuke the last value. |
| 184 | OL[NumOps-2].set(0); |
| 185 | OL[NumOps-2+1].set(0); |
| 186 | NumOperands = NumOps-2; |
| 187 | |
| 188 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 189 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 190 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 191 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 192 | eraseFromParent(); |
| 193 | } |
| 194 | return Removed; |
| 195 | } |
| 196 | |
| 197 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 198 | /// list according to the following behavior: |
| 199 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 200 | /// of operation. This grows the number of ops by 1.5 times. |
| 201 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 202 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 203 | /// |
| 204 | void PHINode::resizeOperands(unsigned NumOps) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 205 | unsigned e = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 206 | if (NumOps == 0) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 207 | NumOps = e*3/2; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 208 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 209 | } else if (NumOps*2 > NumOperands) { |
| 210 | // No resize needed. |
| 211 | if (ReservedSpace >= NumOps) return; |
| 212 | } else if (NumOps == NumOperands) { |
| 213 | if (ReservedSpace == NumOps) return; |
| 214 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 215 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | ReservedSpace = NumOps; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 219 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 220 | Use *NewOps = allocHungoffUses(NumOps); |
Dan Gohman | 031f0bb | 2008-06-23 16:45:24 +0000 | [diff] [blame] | 221 | std::copy(OldOps, OldOps + e, NewOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 222 | OperandList = NewOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 223 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 226 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 227 | /// value, return the value, otherwise return null. |
| 228 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 229 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 230 | // If the PHI node only has one incoming value, eliminate the PHI node... |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 231 | if (getNumIncomingValues() == 1) { |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 232 | if (getIncomingValue(0) != this) // not X = phi X |
| 233 | return getIncomingValue(0); |
| 234 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 235 | return UndefValue::get(getType()); // Self cycle is dead. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 237 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 238 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 239 | // the PHI node with the incoming value. |
| 240 | // |
| 241 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 242 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 243 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 244 | if (isa<UndefValue>(getIncomingValue(i))) { |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 245 | HasUndefInput = true; |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 246 | } else if (getIncomingValue(i) != this) { // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 247 | if (InVal && getIncomingValue(i) != InVal) |
| 248 | return 0; // Not the same, bail out. |
| 249 | else |
| 250 | InVal = getIncomingValue(i); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 251 | } |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 252 | |
| 253 | // The only case that could cause InVal to be null is if we have a PHI node |
| 254 | // that only has entries for itself. In this case, there is no entry into the |
| 255 | // loop, so kill the PHI. |
| 256 | // |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 257 | if (InVal == 0) InVal = UndefValue::get(getType()); |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 258 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 259 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 260 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 261 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 262 | // or if the client is prepared to deal with this possibility. |
| 263 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 264 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 265 | // If it's in the entry block, it dominates everything. |
Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 266 | if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 267 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 268 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 269 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 270 | // All of the incoming values are the same, return the value now. |
| 271 | return InVal; |
| 272 | } |
| 273 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 274 | |
| 275 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 276 | // CallInst Implementation |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 279 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 282 | void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 283 | assert(NumOperands == NumParams+1 && "NumOperands not set up?"); |
| 284 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 285 | OL[0] = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 286 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 287 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 288 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 289 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 291 | assert((NumParams == FTy->getNumParams() || |
| 292 | (FTy->isVarArg() && NumParams > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 293 | "Calling a function with bad signature!"); |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 294 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 295 | assert((i >= FTy->getNumParams() || |
| 296 | FTy->getParamType(i) == Params[i]->getType()) && |
| 297 | "Calling a function with a bad signature!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 298 | OL[i+1] = Params[i]; |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 299 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 302 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 303 | assert(NumOperands == 3 && "NumOperands not set up?"); |
| 304 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 305 | OL[0] = Func; |
| 306 | OL[1] = Actual1; |
| 307 | OL[2] = Actual2; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 308 | |
| 309 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 310 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 311 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 313 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 314 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 315 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 316 | assert((0 >= FTy->getNumParams() || |
| 317 | FTy->getParamType(0) == Actual1->getType()) && |
| 318 | "Calling a function with a bad signature!"); |
| 319 | assert((1 >= FTy->getNumParams() || |
| 320 | FTy->getParamType(1) == Actual2->getType()) && |
| 321 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 324 | void CallInst::init(Value *Func, Value *Actual) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 325 | assert(NumOperands == 2 && "NumOperands not set up?"); |
| 326 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 327 | OL[0] = Func; |
| 328 | OL[1] = Actual; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 329 | |
| 330 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 331 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 332 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 333 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 334 | assert((FTy->getNumParams() == 1 || |
| 335 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 336 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 337 | assert((0 == FTy->getNumParams() || |
| 338 | FTy->getParamType(0) == Actual->getType()) && |
| 339 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 342 | void CallInst::init(Value *Func) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 343 | assert(NumOperands == 1 && "NumOperands not set up?"); |
| 344 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 345 | OL[0] = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 346 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 347 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 348 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 349 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 350 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 351 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 354 | CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 355 | Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 356 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 357 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 358 | Instruction::Call, |
| 359 | OperandTraits<CallInst>::op_end(this) - 2, |
| 360 | 2, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 361 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 362 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 365 | CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 366 | BasicBlock *InsertAtEnd) |
| 367 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 368 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 369 | Instruction::Call, |
| 370 | OperandTraits<CallInst>::op_end(this) - 2, |
| 371 | 2, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 372 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 373 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 374 | } |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 375 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 376 | Instruction *InsertBefore) |
| 377 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 378 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 379 | Instruction::Call, |
| 380 | OperandTraits<CallInst>::op_end(this) - 1, |
| 381 | 1, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 382 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 383 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 386 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 387 | BasicBlock *InsertAtEnd) |
| 388 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 389 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 390 | Instruction::Call, |
| 391 | OperandTraits<CallInst>::op_end(this) - 1, |
| 392 | 1, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 393 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 394 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 397 | CallInst::CallInst(const CallInst &CI) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 398 | : Instruction(CI.getType(), Instruction::Call, |
| 399 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 400 | CI.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 401 | setAttributes(CI.getAttributes()); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 402 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 403 | Use *OL = OperandList; |
| 404 | Use *InOL = CI.OperandList; |
| 405 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 406 | OL[i] = InOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 407 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 410 | void CallInst::addAttribute(unsigned i, Attributes attr) { |
| 411 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 412 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 413 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 416 | void CallInst::removeAttribute(unsigned i, Attributes attr) { |
| 417 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 418 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 419 | setAttributes(PAL); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 422 | bool CallInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 423 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 424 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 425 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 426 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 427 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 430 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 431 | //===----------------------------------------------------------------------===// |
| 432 | // InvokeInst Implementation |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | |
| 435 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 436 | Value* const *Args, unsigned NumArgs) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 437 | assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); |
Gabor Greif | 2d60e1e | 2009-09-03 02:02:59 +0000 | [diff] [blame] | 438 | Use *OL = OperandList; |
| 439 | OL[0] = Fn; |
| 440 | OL[1] = IfNormal; |
| 441 | OL[2] = IfException; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 442 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 443 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 444 | FTy = FTy; // silence warning. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 445 | |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 446 | assert(((NumArgs == FTy->getNumParams()) || |
| 447 | (FTy->isVarArg() && NumArgs > FTy->getNumParams())) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 448 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 449 | |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 450 | for (unsigned i = 0, e = NumArgs; i != e; i++) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 451 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 452 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 453 | "Invoking a function with a bad signature!"); |
| 454 | |
Gabor Greif | 2d60e1e | 2009-09-03 02:02:59 +0000 | [diff] [blame] | 455 | OL[i+3] = Args[i]; |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 456 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 459 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 460 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 461 | OperandTraits<InvokeInst>::op_end(this) |
| 462 | - II.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 463 | II.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 464 | setAttributes(II.getAttributes()); |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 465 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 466 | Use *OL = OperandList, *InOL = II.OperandList; |
| 467 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 468 | OL[i] = InOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 469 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 472 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 473 | return getSuccessor(idx); |
| 474 | } |
| 475 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 476 | return getNumSuccessors(); |
| 477 | } |
| 478 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 479 | return setSuccessor(idx, B); |
| 480 | } |
| 481 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 482 | bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 483 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 484 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 485 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 486 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 487 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 490 | void InvokeInst::addAttribute(unsigned i, Attributes attr) { |
| 491 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 492 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 493 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 496 | void InvokeInst::removeAttribute(unsigned i, Attributes attr) { |
| 497 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 498 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 499 | setAttributes(PAL); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 502 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 503 | //===----------------------------------------------------------------------===// |
| 504 | // ReturnInst Implementation |
| 505 | //===----------------------------------------------------------------------===// |
| 506 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 507 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 508 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 509 | OperandTraits<ReturnInst>::op_end(this) - |
| 510 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 511 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 512 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 513 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 514 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 517 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 518 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 519 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 520 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 521 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 522 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 523 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 524 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 525 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 526 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 527 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 528 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 529 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 530 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 531 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 532 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 533 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 536 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 537 | return getNumSuccessors(); |
| 538 | } |
| 539 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 540 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 541 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 542 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 543 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 546 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 547 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 551 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 552 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 553 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 554 | //===----------------------------------------------------------------------===// |
| 555 | // UnwindInst Implementation |
| 556 | //===----------------------------------------------------------------------===// |
| 557 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 558 | UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore) |
| 559 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 560 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 561 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 562 | UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 563 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 564 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 568 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 569 | return getNumSuccessors(); |
| 570 | } |
| 571 | |
| 572 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 573 | llvm_unreachable("UnwindInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 576 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 577 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 581 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 582 | // UnreachableInst Implementation |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 585 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 586 | Instruction *InsertBefore) |
| 587 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 588 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 589 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 590 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 591 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 592 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 595 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 596 | return getNumSuccessors(); |
| 597 | } |
| 598 | |
| 599 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 600 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 604 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 605 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | // BranchInst Implementation |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 612 | void BranchInst::AssertOK() { |
| 613 | if (isConditional()) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 614 | assert(getCondition()->getType() == Type::getInt1Ty(getContext()) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 615 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 618 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 619 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 620 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 621 | 1, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 622 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 623 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 624 | } |
| 625 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 626 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 627 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 628 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 629 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 630 | Op<-1>() = IfTrue; |
| 631 | Op<-2>() = IfFalse; |
| 632 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 633 | #ifndef NDEBUG |
| 634 | AssertOK(); |
| 635 | #endif |
| 636 | } |
| 637 | |
| 638 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 639 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 640 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 641 | 1, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 642 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 643 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 647 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 648 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 649 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 650 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 651 | Op<-1>() = IfTrue; |
| 652 | Op<-2>() = IfFalse; |
| 653 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 654 | #ifndef NDEBUG |
| 655 | AssertOK(); |
| 656 | #endif |
| 657 | } |
| 658 | |
| 659 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 660 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 661 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 662 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 663 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 664 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 665 | if (BI.getNumOperands() != 1) { |
| 666 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 667 | Op<-3>() = BI.Op<-3>(); |
| 668 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 669 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 670 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 673 | |
| 674 | Use* Use::getPrefix() { |
| 675 | PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev); |
| 676 | if (PotentialPrefix.getOpaqueValue()) |
| 677 | return 0; |
| 678 | |
| 679 | return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1); |
| 680 | } |
| 681 | |
| 682 | BranchInst::~BranchInst() { |
| 683 | if (NumOperands == 1) { |
| 684 | if (Use *Prefix = OperandList->getPrefix()) { |
| 685 | Op<-1>() = 0; |
| 686 | // |
| 687 | // mark OperandList to have a special value for scrutiny |
| 688 | // by baseclass destructors and operator delete |
| 689 | OperandList = Prefix; |
| 690 | } else { |
| 691 | NumOperands = 3; |
| 692 | OperandList = op_begin(); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 698 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 699 | return getSuccessor(idx); |
| 700 | } |
| 701 | unsigned BranchInst::getNumSuccessorsV() const { |
| 702 | return getNumSuccessors(); |
| 703 | } |
| 704 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 705 | setSuccessor(idx, B); |
| 706 | } |
| 707 | |
| 708 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
| 710 | // AllocationInst Implementation |
| 711 | //===----------------------------------------------------------------------===// |
| 712 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 713 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 714 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 715 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 716 | else { |
| 717 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 718 | "Passed basic block into allocation size parameter! Use other ctor"); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 719 | assert(Amt->getType() == Type::getInt32Ty(Context) && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 720 | "Malloc/Allocation array size is not a 32-bit integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 721 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 722 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 725 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 726 | unsigned Align, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 727 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 728 | : UnaryInstruction(PointerType::getUnqual(Ty), iTy, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 729 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 730 | setAlignment(Align); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 731 | assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 732 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 735 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 736 | unsigned Align, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 737 | BasicBlock *InsertAtEnd) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 738 | : UnaryInstruction(PointerType::getUnqual(Ty), iTy, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 739 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 740 | setAlignment(Align); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 741 | assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 742 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 745 | // Out of line virtual method, so the vtable, etc has a home. |
| 746 | AllocationInst::~AllocationInst() { |
| 747 | } |
| 748 | |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 749 | void AllocationInst::setAlignment(unsigned Align) { |
| 750 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 751 | SubclassData = Log2_32(Align) + 1; |
| 752 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 753 | } |
| 754 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 755 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 756 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
| 757 | return CI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 758 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | const Type *AllocationInst::getAllocatedType() const { |
| 762 | return getType()->getElementType(); |
| 763 | } |
| 764 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 765 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 766 | /// function and is a constant size. If so, the code generator will fold it |
| 767 | /// into the prolog/epilog code, so it is basically free. |
| 768 | bool AllocaInst::isStaticAlloca() const { |
| 769 | // Must be constant size. |
| 770 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 771 | |
| 772 | // Must be in the entry block. |
| 773 | const BasicBlock *Parent = getParent(); |
| 774 | return Parent == &Parent->getParent()->front(); |
| 775 | } |
| 776 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 777 | //===----------------------------------------------------------------------===// |
| 778 | // FreeInst Implementation |
| 779 | //===----------------------------------------------------------------------===// |
| 780 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 781 | void FreeInst::AssertOK() { |
| 782 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 783 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 787 | : UnaryInstruction(Type::getVoidTy(Ptr->getContext()), |
| 788 | Free, Ptr, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 789 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 793 | : UnaryInstruction(Type::getVoidTy(Ptr->getContext()), |
| 794 | Free, Ptr, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 795 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
| 799 | //===----------------------------------------------------------------------===// |
| 800 | // LoadInst Implementation |
| 801 | //===----------------------------------------------------------------------===// |
| 802 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 803 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 804 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 805 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 808 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 809 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 810 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 811 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 812 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 813 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 814 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 817 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 818 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 819 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 820 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 821 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 822 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 823 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 826 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 827 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 828 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 829 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 830 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 831 | setAlignment(0); |
| 832 | AssertOK(); |
| 833 | setName(Name); |
| 834 | } |
| 835 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 836 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 837 | unsigned Align, Instruction *InsertBef) |
| 838 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 839 | Load, Ptr, InsertBef) { |
| 840 | setVolatile(isVolatile); |
| 841 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 842 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 843 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 846 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 847 | unsigned Align, BasicBlock *InsertAE) |
| 848 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 849 | Load, Ptr, InsertAE) { |
| 850 | setVolatile(isVolatile); |
| 851 | setAlignment(Align); |
| 852 | AssertOK(); |
| 853 | setName(Name); |
| 854 | } |
| 855 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 856 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 857 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 858 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 859 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 860 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 861 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 862 | AssertOK(); |
| 863 | setName(Name); |
| 864 | } |
| 865 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 866 | |
| 867 | |
| 868 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 869 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 870 | Load, Ptr, InsertBef) { |
| 871 | setVolatile(false); |
| 872 | setAlignment(0); |
| 873 | AssertOK(); |
| 874 | if (Name && Name[0]) setName(Name); |
| 875 | } |
| 876 | |
| 877 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 878 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 879 | Load, Ptr, InsertAE) { |
| 880 | setVolatile(false); |
| 881 | setAlignment(0); |
| 882 | AssertOK(); |
| 883 | if (Name && Name[0]) setName(Name); |
| 884 | } |
| 885 | |
| 886 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 887 | Instruction *InsertBef) |
| 888 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 889 | Load, Ptr, InsertBef) { |
| 890 | setVolatile(isVolatile); |
| 891 | setAlignment(0); |
| 892 | AssertOK(); |
| 893 | if (Name && Name[0]) setName(Name); |
| 894 | } |
| 895 | |
| 896 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 897 | BasicBlock *InsertAE) |
| 898 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 899 | Load, Ptr, InsertAE) { |
| 900 | setVolatile(isVolatile); |
| 901 | setAlignment(0); |
| 902 | AssertOK(); |
| 903 | if (Name && Name[0]) setName(Name); |
| 904 | } |
| 905 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 906 | void LoadInst::setAlignment(unsigned Align) { |
| 907 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 908 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 909 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 910 | |
| 911 | //===----------------------------------------------------------------------===// |
| 912 | // StoreInst Implementation |
| 913 | //===----------------------------------------------------------------------===// |
| 914 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 915 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 916 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 917 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 918 | "Ptr must have pointer type!"); |
| 919 | assert(getOperand(0)->getType() == |
| 920 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 921 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 922 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 923 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 924 | |
| 925 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 926 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 927 | OperandTraits<StoreInst>::op_begin(this), |
| 928 | OperandTraits<StoreInst>::operands(this), |
| 929 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 930 | Op<0>() = val; |
| 931 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 932 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 933 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 934 | AssertOK(); |
| 935 | } |
| 936 | |
| 937 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 938 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 939 | OperandTraits<StoreInst>::op_begin(this), |
| 940 | OperandTraits<StoreInst>::operands(this), |
| 941 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 942 | Op<0>() = val; |
| 943 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 944 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 945 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 946 | AssertOK(); |
| 947 | } |
| 948 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 949 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 950 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 951 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 952 | OperandTraits<StoreInst>::op_begin(this), |
| 953 | OperandTraits<StoreInst>::operands(this), |
| 954 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 955 | Op<0>() = val; |
| 956 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 957 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 958 | setAlignment(0); |
| 959 | AssertOK(); |
| 960 | } |
| 961 | |
| 962 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 963 | unsigned Align, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 964 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 965 | OperandTraits<StoreInst>::op_begin(this), |
| 966 | OperandTraits<StoreInst>::operands(this), |
| 967 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 968 | Op<0>() = val; |
| 969 | Op<1>() = addr; |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 970 | setVolatile(isVolatile); |
| 971 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 972 | AssertOK(); |
| 973 | } |
| 974 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 975 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 976 | unsigned Align, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 977 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 978 | OperandTraits<StoreInst>::op_begin(this), |
| 979 | OperandTraits<StoreInst>::operands(this), |
| 980 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 981 | Op<0>() = val; |
| 982 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 983 | setVolatile(isVolatile); |
| 984 | setAlignment(Align); |
| 985 | AssertOK(); |
| 986 | } |
| 987 | |
| 988 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 989 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 990 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 991 | OperandTraits<StoreInst>::op_begin(this), |
| 992 | OperandTraits<StoreInst>::operands(this), |
| 993 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 994 | Op<0>() = val; |
| 995 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 996 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 997 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 998 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1001 | void StoreInst::setAlignment(unsigned Align) { |
| 1002 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 1003 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 1004 | } |
| 1005 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1006 | //===----------------------------------------------------------------------===// |
| 1007 | // GetElementPtrInst Implementation |
| 1008 | //===----------------------------------------------------------------------===// |
| 1009 | |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1010 | static unsigned retrieveAddrSpace(const Value *Val) { |
| 1011 | return cast<PointerType>(Val->getType())->getAddressSpace(); |
| 1012 | } |
| 1013 | |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1014 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1015 | const Twine &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1016 | assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); |
| 1017 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1018 | OL[0] = Ptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 1020 | for (unsigned i = 0; i != NumIdx; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1021 | OL[i+1] = Idx[i]; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1022 | |
| 1023 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1026 | void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1027 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1028 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1029 | OL[0] = Ptr; |
| 1030 | OL[1] = Idx; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1031 | |
| 1032 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1035 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1036 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1037 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1038 | - GEPI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1039 | GEPI.getNumOperands()) { |
| 1040 | Use *OL = OperandList; |
| 1041 | Use *GEPIOL = GEPI.OperandList; |
| 1042 | for (unsigned i = 0, E = NumOperands; i != E; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1043 | OL[i] = GEPIOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1044 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1047 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1048 | const Twine &Name, Instruction *InBe) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1049 | : Instruction(PointerType::get( |
Owen Anderson | d420fd4 | 2009-07-16 00:03:07 +0000 | [diff] [blame] | 1050 | checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1051 | GetElementPtr, |
| 1052 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1053 | 2, InBe) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1054 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1058 | const Twine &Name, BasicBlock *IAE) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1059 | : Instruction(PointerType::get( |
Owen Anderson | d420fd4 | 2009-07-16 00:03:07 +0000 | [diff] [blame] | 1060 | checkType(getIndexedType(Ptr->getType(),Idx)), |
| 1061 | retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1062 | GetElementPtr, |
| 1063 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1064 | 2, IAE) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1065 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1068 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1069 | /// a gep instruction with the specified parameters. |
| 1070 | /// |
| 1071 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1072 | /// indices, either as Value* or uint64_t. |
| 1073 | /// |
| 1074 | /// A null type is returned if the indices are invalid for the specified |
| 1075 | /// pointer type. |
| 1076 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1077 | template <typename IndexTy> |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1078 | static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs, |
| 1079 | unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1080 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1081 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1082 | const Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1084 | // Handle the special case of the empty set index set, which is always valid. |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1085 | if (NumIdx == 0) |
| 1086 | return Agg; |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1087 | |
| 1088 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | 4a48815 | 2009-03-09 04:56:22 +0000 | [diff] [blame] | 1089 | // it cannot be 'stepped over'. We explicitly allow abstract types (those |
| 1090 | // that contain opaque types) under the assumption that it will be resolved to |
| 1091 | // a sane type later. |
| 1092 | if (!Agg->isSized() && !Agg->isAbstract()) |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1093 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1094 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1095 | unsigned CurIdx = 1; |
| 1096 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1097 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
| 1098 | if (!CT || isa<PointerType>(CT)) return 0; |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1099 | IndexTy Index = Idxs[CurIdx]; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1100 | if (!CT->indexValid(Index)) return 0; |
| 1101 | Agg = CT->getTypeAtIndex(Index); |
| 1102 | |
| 1103 | // If the new type forwards to another type, then it is in the middle |
| 1104 | // of being refined to another type (and hence, may have dropped all |
| 1105 | // references to what it was using before). So, use the new forwarded |
| 1106 | // type. |
| 1107 | if (const Type *Ty = Agg->getForwardedType()) |
| 1108 | Agg = Ty; |
| 1109 | } |
| 1110 | return CurIdx == NumIdx ? Agg : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1113 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1114 | Value* const *Idxs, |
| 1115 | unsigned NumIdx) { |
| 1116 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1117 | } |
| 1118 | |
| 1119 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1120 | uint64_t const *Idxs, |
| 1121 | unsigned NumIdx) { |
| 1122 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1123 | } |
| 1124 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1125 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 1126 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1127 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1128 | |
| 1129 | // Check the pointer index. |
| 1130 | if (!PTy->indexValid(Idx)) return 0; |
| 1131 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 1132 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1135 | |
| 1136 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1137 | /// zeros. If so, the result pointer and the first operand have the same |
| 1138 | /// value, just potentially different types. |
| 1139 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1140 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1141 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1142 | if (!CI->isZero()) return false; |
| 1143 | } else { |
| 1144 | return false; |
| 1145 | } |
| 1146 | } |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1150 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1151 | /// constant integers. If so, the result pointer and the first operand have |
| 1152 | /// a constant offset between them. |
| 1153 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1154 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1155 | if (!isa<ConstantInt>(getOperand(i))) |
| 1156 | return false; |
| 1157 | } |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1161 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1162 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1163 | // ExtractElementInst Implementation |
| 1164 | //===----------------------------------------------------------------------===// |
| 1165 | |
| 1166 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1167 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1168 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1169 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1170 | ExtractElement, |
| 1171 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1172 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1173 | assert(isValidOperands(Val, Index) && |
| 1174 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1175 | Op<0>() = Val; |
| 1176 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1177 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1181 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1182 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1183 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1184 | ExtractElement, |
| 1185 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1186 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1187 | assert(isValidOperands(Val, Index) && |
| 1188 | "Invalid extractelement instruction operands!"); |
| 1189 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1190 | Op<0>() = Val; |
| 1191 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1192 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1196 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1197 | if (!isa<VectorType>(Val->getType()) || |
| 1198 | Index->getType() != Type::getInt32Ty(Val->getContext())) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1199 | return false; |
| 1200 | return true; |
| 1201 | } |
| 1202 | |
| 1203 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1204 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1205 | // InsertElementInst Implementation |
| 1206 | //===----------------------------------------------------------------------===// |
| 1207 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1208 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1209 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1210 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1211 | : Instruction(Vec->getType(), InsertElement, |
| 1212 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1213 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1214 | assert(isValidOperands(Vec, Elt, Index) && |
| 1215 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1216 | Op<0>() = Vec; |
| 1217 | Op<1>() = Elt; |
| 1218 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1219 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1222 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1223 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1224 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1225 | : Instruction(Vec->getType(), InsertElement, |
| 1226 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1227 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1228 | assert(isValidOperands(Vec, Elt, Index) && |
| 1229 | "Invalid insertelement instruction operands!"); |
| 1230 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1231 | Op<0>() = Vec; |
| 1232 | Op<1>() = Elt; |
| 1233 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1234 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1237 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1238 | const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1239 | if (!isa<VectorType>(Vec->getType())) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1240 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1241 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1242 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1243 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1244 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1245 | if (Index->getType() != Type::getInt32Ty(Vec->getContext())) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1246 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1247 | return true; |
| 1248 | } |
| 1249 | |
| 1250 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1251 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1252 | // ShuffleVectorInst Implementation |
| 1253 | //===----------------------------------------------------------------------===// |
| 1254 | |
| 1255 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1256 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1257 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1258 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1259 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1260 | ShuffleVector, |
| 1261 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1262 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1263 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1264 | assert(isValidOperands(V1, V2, Mask) && |
| 1265 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1266 | Op<0>() = V1; |
| 1267 | Op<1>() = V2; |
| 1268 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1269 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1273 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1274 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1275 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1276 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1277 | ShuffleVector, |
| 1278 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1279 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1280 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1281 | assert(isValidOperands(V1, V2, Mask) && |
| 1282 | "Invalid shuffle vector instruction operands!"); |
| 1283 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1284 | Op<0>() = V1; |
| 1285 | Op<1>() = V2; |
| 1286 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1287 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1290 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1291 | const Value *Mask) { |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1292 | if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1293 | return false; |
| 1294 | |
| 1295 | const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
| 1296 | if (!isa<Constant>(Mask) || MaskTy == 0 || |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1297 | MaskTy->getElementType() != Type::getInt32Ty(V1->getContext())) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | return true; |
| 1300 | } |
| 1301 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1302 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1303 | /// output result. This is either -1 if the element is undef or a number less |
| 1304 | /// than 2*numelements. |
| 1305 | int ShuffleVectorInst::getMaskValue(unsigned i) const { |
| 1306 | const Constant *Mask = cast<Constant>(getOperand(2)); |
| 1307 | if (isa<UndefValue>(Mask)) return -1; |
| 1308 | if (isa<ConstantAggregateZero>(Mask)) return 0; |
| 1309 | const ConstantVector *MaskCV = cast<ConstantVector>(Mask); |
| 1310 | assert(i < MaskCV->getNumOperands() && "Index out of range"); |
| 1311 | |
| 1312 | if (isa<UndefValue>(MaskCV->getOperand(i))) |
| 1313 | return -1; |
| 1314 | return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue(); |
| 1315 | } |
| 1316 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1317 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1318 | // InsertValueInst Class |
| 1319 | //===----------------------------------------------------------------------===// |
| 1320 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1321 | void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1322 | unsigned NumIdx, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1323 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1324 | Op<0>() = Agg; |
| 1325 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1326 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1327 | Indices.insert(Indices.end(), Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1328 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1331 | void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1332 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1333 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1334 | Op<0>() = Agg; |
| 1335 | Op<1>() = Val; |
| 1336 | |
| 1337 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1338 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1342 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1343 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1344 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1345 | Op<0>() = IVI.getOperand(0); |
| 1346 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1347 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1350 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1351 | Value *Val, |
| 1352 | unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1353 | const Twine &Name, |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1354 | Instruction *InsertBefore) |
| 1355 | : Instruction(Agg->getType(), InsertValue, |
| 1356 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1357 | 2, InsertBefore) { |
| 1358 | init(Agg, Val, Idx, Name); |
| 1359 | } |
| 1360 | |
| 1361 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1362 | Value *Val, |
| 1363 | unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1364 | const Twine &Name, |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1365 | BasicBlock *InsertAtEnd) |
| 1366 | : Instruction(Agg->getType(), InsertValue, |
| 1367 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1368 | 2, InsertAtEnd) { |
| 1369 | init(Agg, Val, Idx, Name); |
| 1370 | } |
| 1371 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1372 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1373 | // ExtractValueInst Class |
| 1374 | //===----------------------------------------------------------------------===// |
| 1375 | |
Gabor Greif | cbcc495 | 2008-06-06 21:06:32 +0000 | [diff] [blame] | 1376 | void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1377 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1378 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1379 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1380 | Indices.insert(Indices.end(), Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1381 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1384 | void ExtractValueInst::init(unsigned Idx, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1385 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1386 | |
| 1387 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1388 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1392 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1393 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1394 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1397 | // getIndexedType - Returns the type of the element that would be extracted |
| 1398 | // with an extractvalue instruction with the specified parameters. |
| 1399 | // |
| 1400 | // A null type is returned if the indices are invalid for the specified |
| 1401 | // pointer type. |
| 1402 | // |
| 1403 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1404 | const unsigned *Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1405 | unsigned NumIdx) { |
| 1406 | unsigned CurIdx = 0; |
| 1407 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1408 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1409 | if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0; |
| 1410 | unsigned Index = Idxs[CurIdx]; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1411 | if (!CT->indexValid(Index)) return 0; |
| 1412 | Agg = CT->getTypeAtIndex(Index); |
| 1413 | |
| 1414 | // If the new type forwards to another type, then it is in the middle |
| 1415 | // of being refined to another type (and hence, may have dropped all |
| 1416 | // references to what it was using before). So, use the new forwarded |
| 1417 | // type. |
| 1418 | if (const Type *Ty = Agg->getForwardedType()) |
| 1419 | Agg = Ty; |
| 1420 | } |
| 1421 | return CurIdx == NumIdx ? Agg : 0; |
| 1422 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1423 | |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1424 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | d87e813 | 2008-06-20 00:47:44 +0000 | [diff] [blame] | 1425 | unsigned Idx) { |
| 1426 | return getIndexedType(Agg, &Idx, 1); |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1429 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1430 | // BinaryOperator Class |
| 1431 | //===----------------------------------------------------------------------===// |
| 1432 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1433 | /// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the |
| 1434 | /// type is floating-point, to help provide compatibility with an older API. |
| 1435 | /// |
| 1436 | static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType, |
| 1437 | const Type *Ty) { |
| 1438 | // API compatibility: Adjust integer opcodes to floating-point opcodes. |
| 1439 | if (Ty->isFPOrFPVector()) { |
| 1440 | if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd; |
| 1441 | else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub; |
| 1442 | else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul; |
| 1443 | } |
| 1444 | return iType; |
| 1445 | } |
| 1446 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1447 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1448 | const Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1449 | Instruction *InsertBefore) |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1450 | : Instruction(Ty, AdjustIType(iType, Ty), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1451 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1452 | OperandTraits<BinaryOperator>::operands(this), |
| 1453 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1454 | Op<0>() = S1; |
| 1455 | Op<1>() = S2; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1456 | init(AdjustIType(iType, Ty)); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1457 | setName(Name); |
| 1458 | } |
| 1459 | |
| 1460 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1461 | const Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1462 | BasicBlock *InsertAtEnd) |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1463 | : Instruction(Ty, AdjustIType(iType, Ty), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1464 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1465 | OperandTraits<BinaryOperator>::operands(this), |
| 1466 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1467 | Op<0>() = S1; |
| 1468 | Op<1>() = S2; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1469 | init(AdjustIType(iType, Ty)); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1470 | setName(Name); |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1475 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1476 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1477 | assert(LHS->getType() == RHS->getType() && |
| 1478 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1479 | #ifndef NDEBUG |
| 1480 | switch (iType) { |
| 1481 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1482 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1483 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1484 | "Arithmetic operation should return same type as operands!"); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1485 | assert(getType()->isIntOrIntVector() && |
| 1486 | "Tried to create an integer operation on a non-integer type!"); |
| 1487 | break; |
| 1488 | case FAdd: case FSub: |
| 1489 | case FMul: |
| 1490 | assert(getType() == LHS->getType() && |
| 1491 | "Arithmetic operation should return same type as operands!"); |
| 1492 | assert(getType()->isFPOrFPVector() && |
| 1493 | "Tried to create a floating-point operation on a " |
| 1494 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1495 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1496 | case UDiv: |
| 1497 | case SDiv: |
| 1498 | assert(getType() == LHS->getType() && |
| 1499 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1500 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1501 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1502 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1503 | break; |
| 1504 | case FDiv: |
| 1505 | assert(getType() == LHS->getType() && |
| 1506 | "Arithmetic operation should return same type as operands!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1507 | assert(getType()->isFPOrFPVector() && |
| 1508 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1509 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1510 | case URem: |
| 1511 | case SRem: |
| 1512 | assert(getType() == LHS->getType() && |
| 1513 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1514 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1515 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1516 | "Incorrect operand type (not integer) for S/UREM"); |
| 1517 | break; |
| 1518 | case FRem: |
| 1519 | assert(getType() == LHS->getType() && |
| 1520 | "Arithmetic operation should return same type as operands!"); |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1521 | assert(getType()->isFPOrFPVector() && |
| 1522 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1523 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1524 | case Shl: |
| 1525 | case LShr: |
| 1526 | case AShr: |
| 1527 | assert(getType() == LHS->getType() && |
| 1528 | "Shift operation should return same type as operands!"); |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1529 | assert((getType()->isInteger() || |
| 1530 | (isa<VectorType>(getType()) && |
| 1531 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
| 1532 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1533 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1534 | case And: case Or: |
| 1535 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1536 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1537 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1538 | assert((getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1539 | (isa<VectorType>(getType()) && |
| 1540 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1541 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1542 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1543 | default: |
| 1544 | break; |
| 1545 | } |
| 1546 | #endif |
| 1547 | } |
| 1548 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1549 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1550 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1551 | Instruction *InsertBefore) { |
| 1552 | assert(S1->getType() == S2->getType() && |
| 1553 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1554 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1557 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1558 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1559 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1560 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1561 | InsertAtEnd->getInstList().push_back(Res); |
| 1562 | return Res; |
| 1563 | } |
| 1564 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1565 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1566 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1567 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1568 | return new BinaryOperator(Instruction::Sub, |
| 1569 | zero, Op, |
| 1570 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1573 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1574 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1575 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1576 | return new BinaryOperator(Instruction::Sub, |
| 1577 | zero, Op, |
| 1578 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1581 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1582 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1583 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1584 | return new BinaryOperator(Instruction::FSub, |
| 1585 | zero, Op, |
| 1586 | Op->getType(), Name, InsertBefore); |
| 1587 | } |
| 1588 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1589 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1590 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1591 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1592 | return new BinaryOperator(Instruction::FSub, |
| 1593 | zero, Op, |
| 1594 | Op->getType(), Name, InsertAtEnd); |
| 1595 | } |
| 1596 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1597 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1598 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1599 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1600 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1601 | C = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1602 | C = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1603 | std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1604 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1605 | C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1609 | Op->getType(), Name, InsertBefore); |
| 1610 | } |
| 1611 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1612 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1613 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1614 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1615 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1616 | // Create a vector of all ones values. |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1617 | Constant *Elt = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1618 | AllOnes = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1619 | std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1620 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1621 | AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1625 | Op->getType(), Name, InsertAtEnd); |
| 1626 | } |
| 1627 | |
| 1628 | |
| 1629 | // isConstantAllOnes - Helper function for several functions below |
| 1630 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1631 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1632 | return CI->isAllOnesValue(); |
| 1633 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1634 | return CV->isAllOnesValue(); |
| 1635 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1638 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1639 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1640 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1641 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 1642 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
| 1645 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1646 | bool BinaryOperator::isFNeg(const Value *V) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1647 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1648 | if (Bop->getOpcode() == Instruction::FSub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1649 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 1650 | return C->isNegativeZeroValue(); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1651 | return false; |
| 1652 | } |
| 1653 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1654 | bool BinaryOperator::isNot(const Value *V) { |
| 1655 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1656 | return (Bop->getOpcode() == Instruction::Xor && |
| 1657 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1658 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1662 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1663 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1666 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1667 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1670 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1671 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 1672 | } |
| 1673 | |
| 1674 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 1675 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 1676 | } |
| 1677 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1678 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1679 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1680 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1681 | Value *Op0 = BO->getOperand(0); |
| 1682 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1683 | if (isConstantAllOnes(Op0)) return Op1; |
| 1684 | |
| 1685 | assert(isConstantAllOnes(Op1)); |
| 1686 | return Op0; |
| 1687 | } |
| 1688 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1689 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1690 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | // swapOperands - Exchange the two operands to this instruction. This |
| 1695 | // instruction is safe to use on any binary instruction and does not |
| 1696 | // modify the semantics of the instruction. If the instruction is |
| 1697 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1698 | // |
| 1699 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1700 | if (!isCommutative()) |
| 1701 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 1702 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1703 | return false; |
| 1704 | } |
| 1705 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1706 | //===----------------------------------------------------------------------===// |
| 1707 | // CastInst Class |
| 1708 | //===----------------------------------------------------------------------===// |
| 1709 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1710 | // Just determine if this cast only deals with integral->integral conversion. |
| 1711 | bool CastInst::isIntegerCast() const { |
| 1712 | switch (getOpcode()) { |
| 1713 | default: return false; |
| 1714 | case Instruction::ZExt: |
| 1715 | case Instruction::SExt: |
| 1716 | case Instruction::Trunc: |
| 1717 | return true; |
| 1718 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1719 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1720 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1723 | bool CastInst::isLosslessCast() const { |
| 1724 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1725 | if (getOpcode() != Instruction::BitCast) |
| 1726 | return false; |
| 1727 | |
| 1728 | // Identity cast is always lossless |
| 1729 | const Type* SrcTy = getOperand(0)->getType(); |
| 1730 | const Type* DstTy = getType(); |
| 1731 | if (SrcTy == DstTy) |
| 1732 | return true; |
| 1733 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1734 | // Pointer to pointer is always lossless. |
| 1735 | if (isa<PointerType>(SrcTy)) |
| 1736 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1737 | return false; // Other types have no identity values |
| 1738 | } |
| 1739 | |
| 1740 | /// This function determines if the CastInst does not require any bits to be |
| 1741 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1742 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1743 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 1744 | /// # bitcast i32* %x to i8* |
| 1745 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 1746 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1747 | /// @brief Determine if a cast is a no-op. |
| 1748 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1749 | switch (getOpcode()) { |
| 1750 | default: |
| 1751 | assert(!"Invalid CastOp"); |
| 1752 | case Instruction::Trunc: |
| 1753 | case Instruction::ZExt: |
| 1754 | case Instruction::SExt: |
| 1755 | case Instruction::FPTrunc: |
| 1756 | case Instruction::FPExt: |
| 1757 | case Instruction::UIToFP: |
| 1758 | case Instruction::SIToFP: |
| 1759 | case Instruction::FPToUI: |
| 1760 | case Instruction::FPToSI: |
| 1761 | return false; // These always modify bits |
| 1762 | case Instruction::BitCast: |
| 1763 | return true; // BitCast never modifies bits. |
| 1764 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1765 | return IntPtrTy->getScalarSizeInBits() == |
| 1766 | getType()->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1767 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1768 | return IntPtrTy->getScalarSizeInBits() == |
| 1769 | getOperand(0)->getType()->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | /// This function determines if a pair of casts can be eliminated and what |
| 1774 | /// opcode should be used in the elimination. This assumes that there are two |
| 1775 | /// instructions like this: |
| 1776 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1777 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1778 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1779 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1780 | /// If no such cast is permited, the function returns 0. |
| 1781 | unsigned CastInst::isEliminableCastPair( |
| 1782 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1783 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1784 | { |
| 1785 | // Define the 144 possibilities for these two cast instructions. The values |
| 1786 | // in this matrix determine what to do in a given situation and select the |
| 1787 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1788 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1789 | // the following cast properties: |
| 1790 | // |
| 1791 | // Size Compare Source Destination |
| 1792 | // Operator Src ? Size Type Sign Type Sign |
| 1793 | // -------- ------------ ------------------- --------------------- |
| 1794 | // TRUNC > Integer Any Integral Any |
| 1795 | // ZEXT < Integral Unsigned Integer Any |
| 1796 | // SEXT < Integral Signed Integer Any |
| 1797 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1798 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1799 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1800 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1801 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1802 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1803 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1804 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1805 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1806 | // |
| 1807 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1808 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 1809 | // into "fptoui double to i64", but this loses information about the range |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1810 | // of the produced value (we no longer know the top-part is all zeros). |
| 1811 | // Further this conversion is often much more expensive for typical hardware, |
| 1812 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1813 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1814 | const unsigned numCastOps = |
| 1815 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1816 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1817 | // T F F U S F F P I B -+ |
| 1818 | // R Z S P P I I T P 2 N T | |
| 1819 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1820 | // N X X U S F F N X N 2 V | |
| 1821 | // C T T I I P P C T T P T -+ |
| 1822 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1823 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1824 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1825 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1826 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1827 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1828 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1829 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1830 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1831 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1832 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1833 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1834 | }; |
| 1835 | |
| 1836 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1837 | [secondOp-Instruction::CastOpsBegin]; |
| 1838 | switch (ElimCase) { |
| 1839 | case 0: |
| 1840 | // categorically disallowed |
| 1841 | return 0; |
| 1842 | case 1: |
| 1843 | // allowed, use first cast's opcode |
| 1844 | return firstOp; |
| 1845 | case 2: |
| 1846 | // allowed, use second cast's opcode |
| 1847 | return secondOp; |
| 1848 | case 3: |
| 1849 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1850 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1851 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1852 | return firstOp; |
| 1853 | return 0; |
| 1854 | case 4: |
| 1855 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1856 | // is floating point |
| 1857 | if (DstTy->isFloatingPoint()) |
| 1858 | return firstOp; |
| 1859 | return 0; |
| 1860 | case 5: |
| 1861 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1862 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1863 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1864 | return secondOp; |
| 1865 | return 0; |
| 1866 | case 6: |
| 1867 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1868 | // is a floating point |
| 1869 | if (SrcTy->isFloatingPoint()) |
| 1870 | return secondOp; |
| 1871 | return 0; |
| 1872 | case 7: { |
| 1873 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 1874 | if (!IntPtrTy) |
| 1875 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1876 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 1877 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1878 | if (MidSize >= PtrSize) |
| 1879 | return Instruction::BitCast; |
| 1880 | return 0; |
| 1881 | } |
| 1882 | case 8: { |
| 1883 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1884 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1885 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1886 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 1887 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1888 | if (SrcSize == DstSize) |
| 1889 | return Instruction::BitCast; |
| 1890 | else if (SrcSize < DstSize) |
| 1891 | return firstOp; |
| 1892 | return secondOp; |
| 1893 | } |
| 1894 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1895 | return Instruction::ZExt; |
| 1896 | case 10: |
| 1897 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1898 | // the same as the original, in which case its just a bitcast |
| 1899 | if (SrcTy == DstTy) |
| 1900 | return Instruction::BitCast; |
| 1901 | return 0; // If the types are not the same we can't eliminate it. |
| 1902 | case 11: |
| 1903 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1904 | // is a pointer to pointer cast. |
| 1905 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1906 | return secondOp; |
| 1907 | return 0; |
| 1908 | case 12: |
| 1909 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1910 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1911 | return firstOp; |
| 1912 | return 0; |
| 1913 | case 13: { |
| 1914 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 1915 | if (!IntPtrTy) |
| 1916 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1917 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 1918 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 1919 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1920 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1921 | return Instruction::BitCast; |
| 1922 | return 0; |
| 1923 | } |
| 1924 | case 99: |
| 1925 | // cast combination can't happen (error in input). This is for all cases |
| 1926 | // where the MidTy is not the same for the two cast instructions. |
| 1927 | assert(!"Invalid Cast Combination"); |
| 1928 | return 0; |
| 1929 | default: |
| 1930 | assert(!"Error in CastResults table!!!"); |
| 1931 | return 0; |
| 1932 | } |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1936 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1937 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1938 | // Construct and return the appropriate CastInst subclass |
| 1939 | switch (op) { |
| 1940 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1941 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1942 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1943 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1944 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1945 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1946 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1947 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1948 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1949 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1950 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1951 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1952 | default: |
| 1953 | assert(!"Invalid opcode provided"); |
| 1954 | } |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1958 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1959 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1960 | // Construct and return the appropriate CastInst subclass |
| 1961 | switch (op) { |
| 1962 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1963 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1964 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1965 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1966 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1967 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1968 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1969 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1970 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1971 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1972 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1973 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1974 | default: |
| 1975 | assert(!"Invalid opcode provided"); |
| 1976 | } |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1980 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1981 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1982 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1983 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1984 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1985 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1988 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1989 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1990 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1991 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1992 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1993 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1996 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1997 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1998 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1999 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2000 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2001 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2004 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2005 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2006 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2007 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2008 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2009 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2012 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2013 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2014 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2015 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2016 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2017 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2020 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2021 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2022 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2023 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2024 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2025 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2028 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2029 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2030 | BasicBlock *InsertAtEnd) { |
| 2031 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2032 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2033 | "Invalid cast"); |
| 2034 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2035 | if (Ty->isInteger()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2036 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2037 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2041 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2042 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2043 | Instruction *InsertBefore) { |
| 2044 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2045 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2046 | "Invalid cast"); |
| 2047 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2048 | if (Ty->isInteger()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2049 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2050 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2053 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2054 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2055 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2056 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2057 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2058 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2059 | Instruction::CastOps opcode = |
| 2060 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2061 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2062 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2063 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2066 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2067 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2068 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2069 | assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() && |
| 2070 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2071 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2072 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2073 | Instruction::CastOps opcode = |
| 2074 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2075 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2076 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2077 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2080 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2081 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2082 | Instruction *InsertBefore) { |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2083 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2084 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2085 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2086 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2087 | Instruction::CastOps opcode = |
| 2088 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2089 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2090 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2093 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2094 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2095 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2096 | assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2097 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2098 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2099 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2100 | Instruction::CastOps opcode = |
| 2101 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2102 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2103 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2106 | // Check whether it is valid to call getCastOpcode for these types. |
| 2107 | // This routine must be kept in sync with getCastOpcode. |
| 2108 | bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) { |
| 2109 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2110 | return false; |
| 2111 | |
| 2112 | if (SrcTy == DestTy) |
| 2113 | return true; |
| 2114 | |
| 2115 | // Get the bit sizes, we'll need these |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2116 | unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr |
| 2117 | unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Run through the possibilities ... |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2120 | if (DestTy->isInteger()) { // Casting to integral |
| 2121 | if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2122 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2123 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2124 | return true; |
| 2125 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2126 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2127 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2128 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2129 | return isa<PointerType>(SrcTy); |
| 2130 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2131 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
| 2132 | if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2133 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2134 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2135 | return true; |
| 2136 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2137 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2138 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2139 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2140 | return false; |
| 2141 | } |
| 2142 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2143 | // Casting to vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2144 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2145 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2146 | return DestPTy->getBitWidth() == SrcPTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2147 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2148 | return DestPTy->getBitWidth() == SrcBits; |
| 2149 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2150 | } else if (isa<PointerType>(DestTy)) { // Casting to pointer |
| 2151 | if (isa<PointerType>(SrcTy)) { // Casting from pointer |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2152 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2153 | } else if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2154 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2155 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2156 | return false; |
| 2157 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2158 | } else { // Casting to something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2159 | return false; |
| 2160 | } |
| 2161 | } |
| 2162 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2163 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2164 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2165 | // logic in the castIsValid function below. This axiom should hold: |
| 2166 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2167 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2168 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2169 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2170 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2171 | CastInst::getCastOpcode( |
| 2172 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2173 | // Get the bit sizes, we'll need these |
| 2174 | const Type *SrcTy = Src->getType(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2175 | unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr |
| 2176 | unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2177 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2178 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2179 | "Only first class types are castable!"); |
| 2180 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2181 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2182 | if (DestTy->isInteger()) { // Casting to integral |
| 2183 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2184 | if (DestBits < SrcBits) |
| 2185 | return Trunc; // int -> smaller int |
| 2186 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2187 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2188 | return SExt; // signed -> SEXT |
| 2189 | else |
| 2190 | return ZExt; // unsigned -> ZEXT |
| 2191 | } else { |
| 2192 | return BitCast; // Same size, No-op cast |
| 2193 | } |
| 2194 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2195 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2196 | return FPToSI; // FP -> sint |
| 2197 | else |
| 2198 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2199 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2200 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2201 | "Casting vector to integer of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2202 | PTy = NULL; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2203 | return BitCast; // Same size, no-op cast |
| 2204 | } else { |
| 2205 | assert(isa<PointerType>(SrcTy) && |
| 2206 | "Casting from a value that is not first-class type"); |
| 2207 | return PtrToInt; // ptr -> int |
| 2208 | } |
| 2209 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2210 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2211 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2212 | return SIToFP; // sint -> FP |
| 2213 | else |
| 2214 | return UIToFP; // uint -> FP |
| 2215 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 2216 | if (DestBits < SrcBits) { |
| 2217 | return FPTrunc; // FP -> smaller FP |
| 2218 | } else if (DestBits > SrcBits) { |
| 2219 | return FPExt; // FP -> larger FP |
| 2220 | } else { |
| 2221 | return BitCast; // same size, no-op cast |
| 2222 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2223 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2224 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2225 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2226 | PTy = NULL; |
| 2227 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2228 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2229 | llvm_unreachable("Casting pointer or non-first class to float"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2230 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2231 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 2232 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2233 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2234 | "Casting vector to vector of different widths"); |
Devang Patel | cb181bb | 2008-11-21 20:00:59 +0000 | [diff] [blame] | 2235 | SrcPTy = NULL; |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2236 | return BitCast; // vector -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2237 | } else if (DestPTy->getBitWidth() == SrcBits) { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2238 | return BitCast; // float/int -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2239 | } else { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2240 | assert(!"Illegal cast to vector (wrong type or size)"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } else if (isa<PointerType>(DestTy)) { |
| 2243 | if (isa<PointerType>(SrcTy)) { |
| 2244 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2245 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2246 | return IntToPtr; // int -> ptr |
| 2247 | } else { |
| 2248 | assert(!"Casting pointer to other than pointer or int"); |
| 2249 | } |
| 2250 | } else { |
| 2251 | assert(!"Casting to type that is not first-class"); |
| 2252 | } |
| 2253 | |
| 2254 | // If we fall through to here we probably hit an assertion cast above |
| 2255 | // and assertions are not turned on. Anything we return is an error, so |
| 2256 | // BitCast is as good a choice as any. |
| 2257 | return BitCast; |
| 2258 | } |
| 2259 | |
| 2260 | //===----------------------------------------------------------------------===// |
| 2261 | // CastInst SubClass Constructors |
| 2262 | //===----------------------------------------------------------------------===// |
| 2263 | |
| 2264 | /// Check that the construction parameters for a CastInst are correct. This |
| 2265 | /// could be broken out into the separate constructors but it is useful to have |
| 2266 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2267 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2268 | bool |
| 2269 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Check for type sanity on the arguments |
| 2272 | const Type *SrcTy = S->getType(); |
| 2273 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 2274 | return false; |
| 2275 | |
| 2276 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2277 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2278 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2279 | |
| 2280 | // Switch on the opcode provided |
| 2281 | switch (op) { |
| 2282 | default: return false; // This is an input error |
| 2283 | case Instruction::Trunc: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2284 | return SrcTy->isIntOrIntVector() && |
| 2285 | DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2286 | case Instruction::ZExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2287 | return SrcTy->isIntOrIntVector() && |
| 2288 | DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2289 | case Instruction::SExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2290 | return SrcTy->isIntOrIntVector() && |
| 2291 | DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2292 | case Instruction::FPTrunc: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2293 | return SrcTy->isFPOrFPVector() && |
| 2294 | DstTy->isFPOrFPVector() && |
| 2295 | SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2296 | case Instruction::FPExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2297 | return SrcTy->isFPOrFPVector() && |
| 2298 | DstTy->isFPOrFPVector() && |
| 2299 | SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2300 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2301 | case Instruction::SIToFP: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2302 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2303 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2304 | return SVTy->getElementType()->isIntOrIntVector() && |
| 2305 | DVTy->getElementType()->isFPOrFPVector() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2306 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2307 | } |
| 2308 | } |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2309 | return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2310 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2311 | case Instruction::FPToSI: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2312 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2313 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2314 | return SVTy->getElementType()->isFPOrFPVector() && |
| 2315 | DVTy->getElementType()->isIntOrIntVector() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2316 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2317 | } |
| 2318 | } |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2319 | return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2320 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2321 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2322 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2323 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2324 | case Instruction::BitCast: |
| 2325 | // BitCast implies a no-op cast of type only. No bits change. |
| 2326 | // However, you can't cast pointers to anything but pointers. |
| 2327 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 2328 | return false; |
| 2329 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2330 | // Now we know we're not dealing with a pointer/non-pointer mismatch. In all |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2331 | // these cases, the cast is okay if the source and destination bit widths |
| 2332 | // are identical. |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2333 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | TruncInst::TruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2338 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2339 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2340 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | TruncInst::TruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2344 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2345 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2346 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | ZExtInst::ZExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2350 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2351 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2352 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | ZExtInst::ZExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2356 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2357 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2358 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2359 | } |
| 2360 | SExtInst::SExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2361 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2362 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2363 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2366 | SExtInst::SExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2367 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2368 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2369 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | FPTruncInst::FPTruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2373 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2374 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2375 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | FPTruncInst::FPTruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2379 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2380 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2381 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | FPExtInst::FPExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2385 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2386 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2387 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | FPExtInst::FPExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2391 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2392 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2393 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | UIToFPInst::UIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2397 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2398 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2399 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | UIToFPInst::UIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2403 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2404 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2405 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | SIToFPInst::SIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2409 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2410 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2411 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | SIToFPInst::SIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2415 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2416 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2417 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | FPToUIInst::FPToUIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2421 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2422 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2423 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | FPToUIInst::FPToUIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2427 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2428 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2429 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | FPToSIInst::FPToSIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2433 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2434 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2435 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | FPToSIInst::FPToSIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2439 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2440 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2441 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | PtrToIntInst::PtrToIntInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2445 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2446 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2447 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | PtrToIntInst::PtrToIntInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2451 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2452 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2453 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | IntToPtrInst::IntToPtrInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2457 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2458 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2459 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | IntToPtrInst::IntToPtrInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2463 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2464 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2465 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | BitCastInst::BitCastInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2469 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2470 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2471 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | BitCastInst::BitCastInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2475 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2476 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2477 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2478 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2479 | |
| 2480 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2481 | // CmpInst Classes |
| 2482 | //===----------------------------------------------------------------------===// |
| 2483 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2484 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2485 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2486 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2487 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2488 | OperandTraits<CmpInst>::op_begin(this), |
| 2489 | OperandTraits<CmpInst>::operands(this), |
| 2490 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2491 | Op<0>() = LHS; |
| 2492 | Op<1>() = RHS; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2493 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2494 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2495 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2496 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2497 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2498 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2499 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2500 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2501 | OperandTraits<CmpInst>::op_begin(this), |
| 2502 | OperandTraits<CmpInst>::operands(this), |
| 2503 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2504 | Op<0>() = LHS; |
| 2505 | Op<1>() = RHS; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2506 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2507 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | CmpInst * |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2511 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2512 | Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2513 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2514 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2515 | if (InsertBefore) |
| 2516 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2517 | S1, S2, Name); |
| 2518 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2519 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2520 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2521 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2522 | |
| 2523 | if (InsertBefore) |
| 2524 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2525 | S1, S2, Name); |
| 2526 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2527 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2528 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2532 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2533 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2534 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2535 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2536 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2537 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2538 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2539 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | void CmpInst::swapOperands() { |
| 2543 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2544 | IC->swapOperands(); |
| 2545 | else |
| 2546 | cast<FCmpInst>(this)->swapOperands(); |
| 2547 | } |
| 2548 | |
| 2549 | bool CmpInst::isCommutative() { |
| 2550 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2551 | return IC->isCommutative(); |
| 2552 | return cast<FCmpInst>(this)->isCommutative(); |
| 2553 | } |
| 2554 | |
| 2555 | bool CmpInst::isEquality() { |
| 2556 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2557 | return IC->isEquality(); |
| 2558 | return cast<FCmpInst>(this)->isEquality(); |
| 2559 | } |
| 2560 | |
| 2561 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2562 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2563 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2564 | default: assert(!"Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2565 | case ICMP_EQ: return ICMP_NE; |
| 2566 | case ICMP_NE: return ICMP_EQ; |
| 2567 | case ICMP_UGT: return ICMP_ULE; |
| 2568 | case ICMP_ULT: return ICMP_UGE; |
| 2569 | case ICMP_UGE: return ICMP_ULT; |
| 2570 | case ICMP_ULE: return ICMP_UGT; |
| 2571 | case ICMP_SGT: return ICMP_SLE; |
| 2572 | case ICMP_SLT: return ICMP_SGE; |
| 2573 | case ICMP_SGE: return ICMP_SLT; |
| 2574 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2575 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2576 | case FCMP_OEQ: return FCMP_UNE; |
| 2577 | case FCMP_ONE: return FCMP_UEQ; |
| 2578 | case FCMP_OGT: return FCMP_ULE; |
| 2579 | case FCMP_OLT: return FCMP_UGE; |
| 2580 | case FCMP_OGE: return FCMP_ULT; |
| 2581 | case FCMP_OLE: return FCMP_UGT; |
| 2582 | case FCMP_UEQ: return FCMP_ONE; |
| 2583 | case FCMP_UNE: return FCMP_OEQ; |
| 2584 | case FCMP_UGT: return FCMP_OLE; |
| 2585 | case FCMP_ULT: return FCMP_OGE; |
| 2586 | case FCMP_UGE: return FCMP_OLT; |
| 2587 | case FCMP_ULE: return FCMP_OGT; |
| 2588 | case FCMP_ORD: return FCMP_UNO; |
| 2589 | case FCMP_UNO: return FCMP_ORD; |
| 2590 | case FCMP_TRUE: return FCMP_FALSE; |
| 2591 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2592 | } |
| 2593 | } |
| 2594 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2595 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2596 | switch (pred) { |
| 2597 | default: assert(! "Unknown icmp predicate!"); |
| 2598 | case ICMP_EQ: case ICMP_NE: |
| 2599 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2600 | return pred; |
| 2601 | case ICMP_UGT: return ICMP_SGT; |
| 2602 | case ICMP_ULT: return ICMP_SLT; |
| 2603 | case ICMP_UGE: return ICMP_SGE; |
| 2604 | case ICMP_ULE: return ICMP_SLE; |
| 2605 | } |
| 2606 | } |
| 2607 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2608 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 2609 | switch (pred) { |
| 2610 | default: assert(! "Unknown icmp predicate!"); |
| 2611 | case ICMP_EQ: case ICMP_NE: |
| 2612 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 2613 | return pred; |
| 2614 | case ICMP_SGT: return ICMP_UGT; |
| 2615 | case ICMP_SLT: return ICMP_ULT; |
| 2616 | case ICMP_SGE: return ICMP_UGE; |
| 2617 | case ICMP_SLE: return ICMP_ULE; |
| 2618 | } |
| 2619 | } |
| 2620 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2621 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2622 | switch (pred) { |
| 2623 | default: assert(! "Unknown icmp predicate!"); |
| 2624 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2625 | return true; |
| 2626 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2627 | case ICMP_UGE: case ICMP_ULE: |
| 2628 | return false; |
| 2629 | } |
| 2630 | } |
| 2631 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2632 | /// Initialize a set of values that all satisfy the condition with C. |
| 2633 | /// |
| 2634 | ConstantRange |
| 2635 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2636 | APInt Lower(C); |
| 2637 | APInt Upper(C); |
| 2638 | uint32_t BitWidth = C.getBitWidth(); |
| 2639 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2640 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2641 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2642 | case ICmpInst::ICMP_NE: Lower++; break; |
| 2643 | case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break; |
| 2644 | case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break; |
| 2645 | case ICmpInst::ICMP_UGT: |
| 2646 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2647 | break; |
| 2648 | case ICmpInst::ICMP_SGT: |
| 2649 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2650 | break; |
| 2651 | case ICmpInst::ICMP_ULE: |
| 2652 | Lower = APInt::getMinValue(BitWidth); Upper++; |
| 2653 | break; |
| 2654 | case ICmpInst::ICMP_SLE: |
| 2655 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
| 2656 | break; |
| 2657 | case ICmpInst::ICMP_UGE: |
| 2658 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2659 | break; |
| 2660 | case ICmpInst::ICMP_SGE: |
| 2661 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2662 | break; |
| 2663 | } |
| 2664 | return ConstantRange(Lower, Upper); |
| 2665 | } |
| 2666 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2667 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2668 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2669 | default: assert(!"Unknown cmp predicate!"); |
| 2670 | case ICMP_EQ: case ICMP_NE: |
| 2671 | return pred; |
| 2672 | case ICMP_SGT: return ICMP_SLT; |
| 2673 | case ICMP_SLT: return ICMP_SGT; |
| 2674 | case ICMP_SGE: return ICMP_SLE; |
| 2675 | case ICMP_SLE: return ICMP_SGE; |
| 2676 | case ICMP_UGT: return ICMP_ULT; |
| 2677 | case ICMP_ULT: return ICMP_UGT; |
| 2678 | case ICMP_UGE: return ICMP_ULE; |
| 2679 | case ICMP_ULE: return ICMP_UGE; |
| 2680 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2681 | case FCMP_FALSE: case FCMP_TRUE: |
| 2682 | case FCMP_OEQ: case FCMP_ONE: |
| 2683 | case FCMP_UEQ: case FCMP_UNE: |
| 2684 | case FCMP_ORD: case FCMP_UNO: |
| 2685 | return pred; |
| 2686 | case FCMP_OGT: return FCMP_OLT; |
| 2687 | case FCMP_OLT: return FCMP_OGT; |
| 2688 | case FCMP_OGE: return FCMP_OLE; |
| 2689 | case FCMP_OLE: return FCMP_OGE; |
| 2690 | case FCMP_UGT: return FCMP_ULT; |
| 2691 | case FCMP_ULT: return FCMP_UGT; |
| 2692 | case FCMP_UGE: return FCMP_ULE; |
| 2693 | case FCMP_ULE: return FCMP_UGE; |
| 2694 | } |
| 2695 | } |
| 2696 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2697 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2698 | switch (predicate) { |
| 2699 | default: return false; |
| 2700 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2701 | case ICmpInst::ICMP_UGE: return true; |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2706 | switch (predicate) { |
| 2707 | default: return false; |
| 2708 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2709 | case ICmpInst::ICMP_SGE: return true; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2714 | switch (predicate) { |
| 2715 | default: return false; |
| 2716 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2717 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2718 | case FCmpInst::FCMP_ORD: return true; |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2723 | switch (predicate) { |
| 2724 | default: return false; |
| 2725 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2726 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2727 | case FCmpInst::FCMP_UNO: return true; |
| 2728 | } |
| 2729 | } |
| 2730 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2731 | //===----------------------------------------------------------------------===// |
| 2732 | // SwitchInst Implementation |
| 2733 | //===----------------------------------------------------------------------===// |
| 2734 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2735 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2736 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2737 | ReservedSpace = 2+NumCases*2; |
| 2738 | NumOperands = 2; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2739 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2740 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2741 | OperandList[0] = Value; |
| 2742 | OperandList[1] = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2745 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2746 | /// switch on and a default destination. The number of additional cases can |
| 2747 | /// be specified here to make memory allocation more efficient. This |
| 2748 | /// constructor can also autoinsert before another instruction. |
| 2749 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2750 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2751 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 2752 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2753 | init(Value, Default, NumCases); |
| 2754 | } |
| 2755 | |
| 2756 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2757 | /// switch on and a default destination. The number of additional cases can |
| 2758 | /// be specified here to make memory allocation more efficient. This |
| 2759 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 2760 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2761 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2762 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 2763 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2764 | init(Value, Default, NumCases); |
| 2765 | } |
| 2766 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2767 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2768 | : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2769 | allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2770 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2771 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2772 | OL[i] = InOL[i]; |
| 2773 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2774 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2775 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 2778 | SwitchInst::~SwitchInst() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2779 | dropHungoffUses(OperandList); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2783 | /// addCase - Add an entry to the switch instruction... |
| 2784 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2785 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2786 | unsigned OpNo = NumOperands; |
| 2787 | if (OpNo+2 > ReservedSpace) |
| 2788 | resizeOperands(0); // Get more space! |
| 2789 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2790 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2791 | NumOperands = OpNo+2; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2792 | OperandList[OpNo] = OnVal; |
| 2793 | OperandList[OpNo+1] = Dest; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /// removeCase - This method removes the specified successor from the switch |
| 2797 | /// instruction. Note that this cannot be used to remove the default |
| 2798 | /// destination (successor #0). |
| 2799 | /// |
| 2800 | void SwitchInst::removeCase(unsigned idx) { |
| 2801 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2802 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2803 | |
| 2804 | unsigned NumOps = getNumOperands(); |
| 2805 | Use *OL = OperandList; |
| 2806 | |
| 2807 | // Move everything after this operand down. |
| 2808 | // |
| 2809 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2810 | // client might not expect this to happen. The code as it is thrashes the |
| 2811 | // use/def lists, which is kinda lame. |
| 2812 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2813 | OL[i-2] = OL[i]; |
| 2814 | OL[i-2+1] = OL[i+1]; |
| 2815 | } |
| 2816 | |
| 2817 | // Nuke the last value. |
| 2818 | OL[NumOps-2].set(0); |
| 2819 | OL[NumOps-2+1].set(0); |
| 2820 | NumOperands = NumOps-2; |
| 2821 | } |
| 2822 | |
| 2823 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2824 | /// list according to the following behavior: |
| 2825 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2826 | /// of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2827 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2828 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2829 | /// |
| 2830 | void SwitchInst::resizeOperands(unsigned NumOps) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2831 | unsigned e = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2832 | if (NumOps == 0) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2833 | NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2834 | } else if (NumOps*2 > NumOperands) { |
| 2835 | // No resize needed. |
| 2836 | if (ReservedSpace >= NumOps) return; |
| 2837 | } else if (NumOps == NumOperands) { |
| 2838 | if (ReservedSpace == NumOps) return; |
| 2839 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2840 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | ReservedSpace = NumOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2844 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2845 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2846 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2847 | NewOps[i] = OldOps[i]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2848 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2849 | OperandList = NewOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2850 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | |
| 2854 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2855 | return getSuccessor(idx); |
| 2856 | } |
| 2857 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2858 | return getNumSuccessors(); |
| 2859 | } |
| 2860 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2861 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2862 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2863 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2864 | // Define these methods here so vtables don't get emitted into every translation |
| 2865 | // unit that uses these classes. |
| 2866 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2867 | GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2868 | GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this); |
| 2869 | New->SubclassOptionalData = SubclassOptionalData; |
| 2870 | return New; |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2873 | BinaryOperator *BinaryOperator::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2874 | BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>()); |
| 2875 | New->SubclassOptionalData = SubclassOptionalData; |
| 2876 | return New; |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2879 | FCmpInst* FCmpInst::clone(LLVMContext &Context) const { |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2880 | FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2881 | New->SubclassOptionalData = SubclassOptionalData; |
| 2882 | return New; |
Chris Lattner | 0b490b0 | 2007-08-24 20:48:18 +0000 | [diff] [blame] | 2883 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2884 | ICmpInst* ICmpInst::clone(LLVMContext &Context) const { |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2885 | ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2886 | New->SubclassOptionalData = SubclassOptionalData; |
| 2887 | return New; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2890 | ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2891 | ExtractValueInst *New = new ExtractValueInst(*this); |
| 2892 | New->SubclassOptionalData = SubclassOptionalData; |
| 2893 | return New; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2894 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2895 | InsertValueInst *InsertValueInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2896 | InsertValueInst *New = new InsertValueInst(*this); |
| 2897 | New->SubclassOptionalData = SubclassOptionalData; |
| 2898 | return New; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2901 | MallocInst *MallocInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2902 | MallocInst *New = new MallocInst(getAllocatedType(), |
| 2903 | (Value*)getOperand(0), |
| 2904 | getAlignment()); |
| 2905 | New->SubclassOptionalData = SubclassOptionalData; |
| 2906 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2907 | } |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2908 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2909 | AllocaInst *AllocaInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2910 | AllocaInst *New = new AllocaInst(getAllocatedType(), |
| 2911 | (Value*)getOperand(0), |
| 2912 | getAlignment()); |
| 2913 | New->SubclassOptionalData = SubclassOptionalData; |
| 2914 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | FreeInst *FreeInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2918 | FreeInst *New = new FreeInst(getOperand(0)); |
| 2919 | New->SubclassOptionalData = SubclassOptionalData; |
| 2920 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | LoadInst *LoadInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2924 | LoadInst *New = new LoadInst(getOperand(0), |
| 2925 | Twine(), isVolatile(), |
| 2926 | getAlignment()); |
| 2927 | New->SubclassOptionalData = SubclassOptionalData; |
| 2928 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | StoreInst *StoreInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2932 | StoreInst *New = new StoreInst(getOperand(0), getOperand(1), |
| 2933 | isVolatile(), getAlignment()); |
| 2934 | New->SubclassOptionalData = SubclassOptionalData; |
| 2935 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2938 | TruncInst *TruncInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2939 | TruncInst *New = new TruncInst(getOperand(0), getType()); |
| 2940 | New->SubclassOptionalData = SubclassOptionalData; |
| 2941 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2944 | ZExtInst *ZExtInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2945 | ZExtInst *New = new ZExtInst(getOperand(0), getType()); |
| 2946 | New->SubclassOptionalData = SubclassOptionalData; |
| 2947 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2950 | SExtInst *SExtInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2951 | SExtInst *New = new SExtInst(getOperand(0), getType()); |
| 2952 | New->SubclassOptionalData = SubclassOptionalData; |
| 2953 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2956 | FPTruncInst *FPTruncInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2957 | FPTruncInst *New = new FPTruncInst(getOperand(0), getType()); |
| 2958 | New->SubclassOptionalData = SubclassOptionalData; |
| 2959 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2962 | FPExtInst *FPExtInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2963 | FPExtInst *New = new FPExtInst(getOperand(0), getType()); |
| 2964 | New->SubclassOptionalData = SubclassOptionalData; |
| 2965 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2968 | UIToFPInst *UIToFPInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2969 | UIToFPInst *New = new UIToFPInst(getOperand(0), getType()); |
| 2970 | New->SubclassOptionalData = SubclassOptionalData; |
| 2971 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2974 | SIToFPInst *SIToFPInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2975 | SIToFPInst *New = new SIToFPInst(getOperand(0), getType()); |
| 2976 | New->SubclassOptionalData = SubclassOptionalData; |
| 2977 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2980 | FPToUIInst *FPToUIInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2981 | FPToUIInst *New = new FPToUIInst(getOperand(0), getType()); |
| 2982 | New->SubclassOptionalData = SubclassOptionalData; |
| 2983 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2986 | FPToSIInst *FPToSIInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2987 | FPToSIInst *New = new FPToSIInst(getOperand(0), getType()); |
| 2988 | New->SubclassOptionalData = SubclassOptionalData; |
| 2989 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2992 | PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2993 | PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType()); |
| 2994 | New->SubclassOptionalData = SubclassOptionalData; |
| 2995 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 2998 | IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2999 | IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType()); |
| 3000 | New->SubclassOptionalData = SubclassOptionalData; |
| 3001 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
Dan Gohman | faf516f | 2009-08-25 22:29:08 +0000 | [diff] [blame] | 3004 | BitCastInst *BitCastInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3005 | BitCastInst *New = new BitCastInst(getOperand(0), getType()); |
| 3006 | New->SubclassOptionalData = SubclassOptionalData; |
| 3007 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | CallInst *CallInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3011 | CallInst *New = new(getNumOperands()) CallInst(*this); |
| 3012 | New->SubclassOptionalData = SubclassOptionalData; |
| 3013 | return New; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3014 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3015 | |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3016 | SelectInst *SelectInst::clone(LLVMContext&) const { |
| 3017 | SelectInst *New = SelectInst::Create(getOperand(0), |
| 3018 | getOperand(1), |
| 3019 | getOperand(2)); |
| 3020 | New->SubclassOptionalData = SubclassOptionalData; |
| 3021 | return New; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3022 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3023 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3024 | VAArgInst *VAArgInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3025 | VAArgInst *New = new VAArgInst(getOperand(0), getType()); |
| 3026 | New->SubclassOptionalData = SubclassOptionalData; |
| 3027 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3031 | ExtractElementInst *New = ExtractElementInst::Create(getOperand(0), |
| 3032 | getOperand(1)); |
| 3033 | New->SubclassOptionalData = SubclassOptionalData; |
| 3034 | return New; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3035 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3036 | |
| 3037 | InsertElementInst *InsertElementInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3038 | InsertElementInst *New = InsertElementInst::Create(getOperand(0), |
| 3039 | getOperand(1), |
| 3040 | getOperand(2)); |
| 3041 | New->SubclassOptionalData = SubclassOptionalData; |
| 3042 | return New; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3043 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3044 | |
| 3045 | ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3046 | ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0), |
| 3047 | getOperand(1), |
| 3048 | getOperand(2)); |
| 3049 | New->SubclassOptionalData = SubclassOptionalData; |
| 3050 | return New; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3051 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3052 | |
| 3053 | PHINode *PHINode::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3054 | PHINode *New = new PHINode(*this); |
| 3055 | New->SubclassOptionalData = SubclassOptionalData; |
| 3056 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | ReturnInst *ReturnInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3060 | ReturnInst *New = new(getNumOperands()) ReturnInst(*this); |
| 3061 | New->SubclassOptionalData = SubclassOptionalData; |
| 3062 | return New; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3063 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3064 | |
| 3065 | BranchInst *BranchInst::clone(LLVMContext&) const { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 3066 | unsigned Ops(getNumOperands()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3067 | BranchInst *New = new(Ops, Ops == 1) BranchInst(*this); |
| 3068 | New->SubclassOptionalData = SubclassOptionalData; |
| 3069 | return New; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3070 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3071 | |
| 3072 | SwitchInst *SwitchInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3073 | SwitchInst *New = new SwitchInst(*this); |
| 3074 | New->SubclassOptionalData = SubclassOptionalData; |
| 3075 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | InvokeInst *InvokeInst::clone(LLVMContext&) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3079 | InvokeInst *New = new(getNumOperands()) InvokeInst(*this); |
| 3080 | New->SubclassOptionalData = SubclassOptionalData; |
| 3081 | return New; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3082 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3083 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3084 | UnwindInst *UnwindInst::clone(LLVMContext &C) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3085 | UnwindInst *New = new UnwindInst(C); |
| 3086 | New->SubclassOptionalData = SubclassOptionalData; |
| 3087 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3090 | UnreachableInst *UnreachableInst::clone(LLVMContext &C) const { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3091 | UnreachableInst *New = new UnreachableInst(C); |
| 3092 | New->SubclassOptionalData = SubclassOptionalData; |
| 3093 | return New; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3094 | } |