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