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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 615 | Op<-1>() = 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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 622 | Op<-1>() = IfTrue; |
| 623 | Op<-2>() = IfFalse; |
| 624 | Op<-3>() = 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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 635 | Op<-1>() = 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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 643 | Op<-1>() = IfTrue; |
| 644 | Op<-2>() = IfFalse; |
| 645 | Op<-3>() = 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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 656 | Op<-1>() = BI.Op<-1>(); |
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 | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 659 | Op<-3>() = BI.Op<-3>(); |
| 660 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 664 | |
| 665 | Use* Use::getPrefix() { |
| 666 | PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev); |
| 667 | if (PotentialPrefix.getOpaqueValue()) |
| 668 | return 0; |
| 669 | |
| 670 | return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1); |
| 671 | } |
| 672 | |
| 673 | BranchInst::~BranchInst() { |
| 674 | if (NumOperands == 1) { |
| 675 | if (Use *Prefix = OperandList->getPrefix()) { |
| 676 | Op<-1>() = 0; |
| 677 | // |
| 678 | // mark OperandList to have a special value for scrutiny |
| 679 | // by baseclass destructors and operator delete |
| 680 | OperandList = Prefix; |
| 681 | } else { |
| 682 | NumOperands = 3; |
| 683 | OperandList = op_begin(); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 689 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 690 | return getSuccessor(idx); |
| 691 | } |
| 692 | unsigned BranchInst::getNumSuccessorsV() const { |
| 693 | return getNumSuccessors(); |
| 694 | } |
| 695 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 696 | setSuccessor(idx, B); |
| 697 | } |
| 698 | |
| 699 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 700 | //===----------------------------------------------------------------------===// |
| 701 | // AllocationInst Implementation |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 704 | static Value *getAISize(Value *Amt) { |
| 705 | if (!Amt) |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 706 | Amt = ConstantInt::get(Type::Int32Ty, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 707 | else { |
| 708 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 709 | "Passed basic block into allocation size parameter! Use other ctor"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 710 | assert(Amt->getType() == Type::Int32Ty && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 711 | "Malloc/Allocation array size is not a 32-bit integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 712 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 713 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 716 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 717 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 718 | Instruction *InsertBefore) |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 719 | : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 720 | InsertBefore) { |
| 721 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 722 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 723 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 726 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 727 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 728 | BasicBlock *InsertAtEnd) |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 729 | : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 730 | InsertAtEnd) { |
| 731 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 732 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 733 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 736 | // Out of line virtual method, so the vtable, etc has a home. |
| 737 | AllocationInst::~AllocationInst() { |
| 738 | } |
| 739 | |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 740 | void AllocationInst::setAlignment(unsigned Align) { |
| 741 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 742 | SubclassData = Log2_32(Align) + 1; |
| 743 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 744 | } |
| 745 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 746 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 747 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
| 748 | return CI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 749 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | const Type *AllocationInst::getAllocatedType() const { |
| 753 | return getType()->getElementType(); |
| 754 | } |
| 755 | |
| 756 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 757 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 758 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 761 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 762 | /// function and is a constant size. If so, the code generator will fold it |
| 763 | /// into the prolog/epilog code, so it is basically free. |
| 764 | bool AllocaInst::isStaticAlloca() const { |
| 765 | // Must be constant size. |
| 766 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 767 | |
| 768 | // Must be in the entry block. |
| 769 | const BasicBlock *Parent = getParent(); |
| 770 | return Parent == &Parent->getParent()->front(); |
| 771 | } |
| 772 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 773 | MallocInst::MallocInst(const MallocInst &MI) |
| 774 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 775 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | //===----------------------------------------------------------------------===// |
| 779 | // FreeInst Implementation |
| 780 | //===----------------------------------------------------------------------===// |
| 781 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 782 | void FreeInst::AssertOK() { |
| 783 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 784 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 788 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 789 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 793 | : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 794 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | // LoadInst Implementation |
| 800 | //===----------------------------------------------------------------------===// |
| 801 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 802 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 803 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 804 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 808 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 809 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 810 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 811 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 812 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 813 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 817 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 818 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 819 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 820 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 821 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 822 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 826 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 827 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 828 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 829 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 830 | setAlignment(0); |
| 831 | AssertOK(); |
| 832 | setName(Name); |
| 833 | } |
| 834 | |
| 835 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 836 | unsigned Align, Instruction *InsertBef) |
| 837 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 838 | Load, Ptr, InsertBef) { |
| 839 | setVolatile(isVolatile); |
| 840 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 841 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 842 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 845 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 846 | unsigned Align, BasicBlock *InsertAE) |
| 847 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 848 | Load, Ptr, InsertAE) { |
| 849 | setVolatile(isVolatile); |
| 850 | setAlignment(Align); |
| 851 | AssertOK(); |
| 852 | setName(Name); |
| 853 | } |
| 854 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 855 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 856 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 857 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 858 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 859 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 860 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 861 | AssertOK(); |
| 862 | setName(Name); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | |
| 867 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 868 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 869 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 870 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 871 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 872 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 873 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 877 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 878 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 879 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 880 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 881 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 882 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 886 | Instruction *InsertBef) |
| 887 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 888 | Load, Ptr, InsertBef) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 889 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 890 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 891 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 892 | if (Name && Name[0]) setName(Name); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 896 | BasicBlock *InsertAE) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 897 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 898 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 899 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 900 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 901 | AssertOK(); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 902 | if (Name && Name[0]) setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 905 | void LoadInst::setAlignment(unsigned Align) { |
| 906 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 907 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 908 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 909 | |
| 910 | //===----------------------------------------------------------------------===// |
| 911 | // StoreInst Implementation |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 914 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 915 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 916 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 917 | "Ptr must have pointer type!"); |
| 918 | assert(getOperand(0)->getType() == |
| 919 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 920 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 921 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 922 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 923 | |
| 924 | StoreInst::StoreInst(Value *val, Value *addr, 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(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 932 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 933 | AssertOK(); |
| 934 | } |
| 935 | |
| 936 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 937 | : Instruction(Type::VoidTy, Store, |
| 938 | OperandTraits<StoreInst>::op_begin(this), |
| 939 | OperandTraits<StoreInst>::operands(this), |
| 940 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 941 | Op<0>() = val; |
| 942 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 943 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 944 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 945 | AssertOK(); |
| 946 | } |
| 947 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 948 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 949 | Instruction *InsertBefore) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 950 | : Instruction(Type::VoidTy, Store, |
| 951 | OperandTraits<StoreInst>::op_begin(this), |
| 952 | OperandTraits<StoreInst>::operands(this), |
| 953 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 954 | Op<0>() = val; |
| 955 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 956 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 957 | setAlignment(0); |
| 958 | AssertOK(); |
| 959 | } |
| 960 | |
| 961 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 962 | unsigned Align, Instruction *InsertBefore) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 963 | : Instruction(Type::VoidTy, Store, |
| 964 | OperandTraits<StoreInst>::op_begin(this), |
| 965 | OperandTraits<StoreInst>::operands(this), |
| 966 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 967 | Op<0>() = val; |
| 968 | Op<1>() = addr; |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 969 | setVolatile(isVolatile); |
| 970 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 971 | AssertOK(); |
| 972 | } |
| 973 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 974 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 975 | unsigned Align, BasicBlock *InsertAtEnd) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 976 | : Instruction(Type::VoidTy, Store, |
| 977 | OperandTraits<StoreInst>::op_begin(this), |
| 978 | OperandTraits<StoreInst>::operands(this), |
| 979 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 980 | Op<0>() = val; |
| 981 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 982 | setVolatile(isVolatile); |
| 983 | setAlignment(Align); |
| 984 | AssertOK(); |
| 985 | } |
| 986 | |
| 987 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 988 | BasicBlock *InsertAtEnd) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 989 | : Instruction(Type::VoidTy, Store, |
| 990 | OperandTraits<StoreInst>::op_begin(this), |
| 991 | OperandTraits<StoreInst>::operands(this), |
| 992 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 993 | Op<0>() = val; |
| 994 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 995 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 996 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 997 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1000 | void StoreInst::setAlignment(unsigned Align) { |
| 1001 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
| 1002 | SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); |
| 1003 | } |
| 1004 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1005 | //===----------------------------------------------------------------------===// |
| 1006 | // GetElementPtrInst Implementation |
| 1007 | //===----------------------------------------------------------------------===// |
| 1008 | |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1009 | static unsigned retrieveAddrSpace(const Value *Val) { |
| 1010 | return cast<PointerType>(Val->getType())->getAddressSpace(); |
| 1011 | } |
| 1012 | |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1013 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, |
Nate Begeman | 8a4afd9 | 2008-07-25 17:24:13 +0000 | [diff] [blame] | 1014 | const std::string &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1015 | assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); |
| 1016 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1017 | OL[0] = Ptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 1019 | for (unsigned i = 0; i != NumIdx; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1020 | OL[i+1] = Idx[i]; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1021 | |
| 1022 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1025 | void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1026 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1027 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1028 | OL[0] = Ptr; |
| 1029 | OL[1] = Idx; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1030 | |
| 1031 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1034 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1035 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1036 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1037 | - GEPI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1038 | GEPI.getNumOperands()) { |
| 1039 | Use *OL = OperandList; |
| 1040 | Use *GEPIOL = GEPI.OperandList; |
| 1041 | for (unsigned i = 0, E = NumOperands; i != E; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1042 | OL[i] = GEPIOL[i]; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1045 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 1046 | const std::string &Name, Instruction *InBe) |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1047 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1048 | retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1049 | GetElementPtr, |
| 1050 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1051 | 2, InBe) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1052 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 1056 | const std::string &Name, BasicBlock *IAE) |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1057 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1058 | retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1059 | GetElementPtr, |
| 1060 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1061 | 2, IAE) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1062 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1065 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1066 | /// a gep instruction with the specified parameters. |
| 1067 | /// |
| 1068 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1069 | /// indices, either as Value* or uint64_t. |
| 1070 | /// |
| 1071 | /// A null type is returned if the indices are invalid for the specified |
| 1072 | /// pointer type. |
| 1073 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1074 | template <typename IndexTy> |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1075 | static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs, |
| 1076 | unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1077 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1078 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1079 | const Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1080 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1081 | // 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] | 1082 | if (NumIdx == 0) |
| 1083 | return Agg; |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1084 | |
| 1085 | // 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] | 1086 | // it cannot be 'stepped over'. We explicitly allow abstract types (those |
| 1087 | // that contain opaque types) under the assumption that it will be resolved to |
| 1088 | // a sane type later. |
| 1089 | if (!Agg->isSized() && !Agg->isAbstract()) |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1090 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1091 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1092 | unsigned CurIdx = 1; |
| 1093 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1094 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
| 1095 | if (!CT || isa<PointerType>(CT)) return 0; |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1096 | IndexTy Index = Idxs[CurIdx]; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1097 | if (!CT->indexValid(Index)) return 0; |
| 1098 | Agg = CT->getTypeAtIndex(Index); |
| 1099 | |
| 1100 | // If the new type forwards to another type, then it is in the middle |
| 1101 | // of being refined to another type (and hence, may have dropped all |
| 1102 | // references to what it was using before). So, use the new forwarded |
| 1103 | // type. |
| 1104 | if (const Type *Ty = Agg->getForwardedType()) |
| 1105 | Agg = Ty; |
| 1106 | } |
| 1107 | return CurIdx == NumIdx ? Agg : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1110 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1111 | Value* const *Idxs, |
| 1112 | unsigned NumIdx) { |
| 1113 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1114 | } |
| 1115 | |
| 1116 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1117 | uint64_t const *Idxs, |
| 1118 | unsigned NumIdx) { |
| 1119 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1120 | } |
| 1121 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1122 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 1123 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1124 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1125 | |
| 1126 | // Check the pointer index. |
| 1127 | if (!PTy->indexValid(Idx)) return 0; |
| 1128 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 1129 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1132 | |
| 1133 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1134 | /// zeros. If so, the result pointer and the first operand have the same |
| 1135 | /// value, just potentially different types. |
| 1136 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1137 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1138 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1139 | if (!CI->isZero()) return false; |
| 1140 | } else { |
| 1141 | return false; |
| 1142 | } |
| 1143 | } |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1147 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1148 | /// constant integers. If so, the result pointer and the first operand have |
| 1149 | /// a constant offset between them. |
| 1150 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1151 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1152 | if (!isa<ConstantInt>(getOperand(i))) |
| 1153 | return false; |
| 1154 | } |
| 1155 | return true; |
| 1156 | } |
| 1157 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1158 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1159 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1160 | // ExtractElementInst Implementation |
| 1161 | //===----------------------------------------------------------------------===// |
| 1162 | |
| 1163 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1164 | const std::string &Name, |
| 1165 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1166 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1167 | ExtractElement, |
| 1168 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1169 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1170 | assert(isValidOperands(Val, Index) && |
| 1171 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1172 | Op<0>() = Val; |
| 1173 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1174 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1177 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1178 | const std::string &Name, |
| 1179 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1180 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1181 | ExtractElement, |
| 1182 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1183 | 2, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1184 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1185 | assert(isValidOperands(Val, Index) && |
| 1186 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1187 | Op<0>() = Val; |
| 1188 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1189 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1193 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1194 | const std::string &Name, |
| 1195 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1196 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1197 | ExtractElement, |
| 1198 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1199 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1200 | assert(isValidOperands(Val, Index) && |
| 1201 | "Invalid extractelement instruction operands!"); |
| 1202 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1203 | Op<0>() = Val; |
| 1204 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1205 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1208 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 1209 | const std::string &Name, |
| 1210 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1211 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1212 | ExtractElement, |
| 1213 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1214 | 2, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1215 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1216 | assert(isValidOperands(Val, Index) && |
| 1217 | "Invalid extractelement instruction operands!"); |
| 1218 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1219 | Op<0>() = Val; |
| 1220 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1221 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1225 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1226 | if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1227 | return false; |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1232 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1233 | // InsertElementInst Implementation |
| 1234 | //===----------------------------------------------------------------------===// |
| 1235 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1236 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1237 | : Instruction(IE.getType(), InsertElement, |
| 1238 | OperandTraits<InsertElementInst>::op_begin(this), 3) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1239 | Op<0>() = IE.Op<0>(); |
| 1240 | Op<1>() = IE.Op<1>(); |
| 1241 | Op<2>() = IE.Op<2>(); |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1242 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1243 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1244 | const std::string &Name, |
| 1245 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1246 | : Instruction(Vec->getType(), InsertElement, |
| 1247 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1248 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1249 | assert(isValidOperands(Vec, Elt, Index) && |
| 1250 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1251 | Op<0>() = Vec; |
| 1252 | Op<1>() = Elt; |
| 1253 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1254 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1257 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1258 | const std::string &Name, |
| 1259 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1260 | : Instruction(Vec->getType(), InsertElement, |
| 1261 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1262 | 3, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1263 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1264 | assert(isValidOperands(Vec, Elt, Index) && |
| 1265 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1266 | Op<0>() = Vec; |
| 1267 | Op<1>() = Elt; |
| 1268 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1269 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1273 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1274 | const std::string &Name, |
| 1275 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1276 | : Instruction(Vec->getType(), InsertElement, |
| 1277 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1278 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1279 | assert(isValidOperands(Vec, Elt, Index) && |
| 1280 | "Invalid insertelement instruction operands!"); |
| 1281 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1282 | Op<0>() = Vec; |
| 1283 | Op<1>() = Elt; |
| 1284 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1285 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1288 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 1289 | const std::string &Name, |
| 1290 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1291 | : Instruction(Vec->getType(), InsertElement, |
| 1292 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1293 | 3, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1294 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1295 | assert(isValidOperands(Vec, Elt, Index) && |
| 1296 | "Invalid insertelement instruction operands!"); |
| 1297 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1298 | Op<0>() = Vec; |
| 1299 | Op<1>() = Elt; |
| 1300 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1301 | setName(Name); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1304 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1305 | const Value *Index) { |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1306 | if (!isa<VectorType>(Vec->getType())) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1307 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1308 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1309 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1310 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1311 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1312 | if (Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1313 | return false; // Third operand of insertelement must be uint. |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1318 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1319 | // ShuffleVectorInst Implementation |
| 1320 | //===----------------------------------------------------------------------===// |
| 1321 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1322 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1323 | : Instruction(SV.getType(), ShuffleVector, |
| 1324 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1325 | OperandTraits<ShuffleVectorInst>::operands(this)) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1326 | Op<0>() = SV.Op<0>(); |
| 1327 | Op<1>() = SV.Op<1>(); |
| 1328 | Op<2>() = SV.Op<2>(); |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1331 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1332 | const std::string &Name, |
| 1333 | Instruction *InsertBefore) |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1334 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1335 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1336 | ShuffleVector, |
| 1337 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1338 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1339 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1340 | assert(isValidOperands(V1, V2, Mask) && |
| 1341 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1342 | Op<0>() = V1; |
| 1343 | Op<1>() = V2; |
| 1344 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1345 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1349 | const std::string &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1350 | BasicBlock *InsertAtEnd) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1351 | : Instruction(V1->getType(), ShuffleVector, |
| 1352 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1353 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1354 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1355 | assert(isValidOperands(V1, V2, Mask) && |
| 1356 | "Invalid shuffle vector instruction operands!"); |
| 1357 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1358 | Op<0>() = V1; |
| 1359 | Op<1>() = V2; |
| 1360 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1361 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1364 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1365 | const Value *Mask) { |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1366 | if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1367 | return false; |
| 1368 | |
| 1369 | const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
| 1370 | if (!isa<Constant>(Mask) || MaskTy == 0 || |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1371 | MaskTy->getElementType() != Type::Int32Ty) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1372 | return false; |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1376 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1377 | /// output result. This is either -1 if the element is undef or a number less |
| 1378 | /// than 2*numelements. |
| 1379 | int ShuffleVectorInst::getMaskValue(unsigned i) const { |
| 1380 | const Constant *Mask = cast<Constant>(getOperand(2)); |
| 1381 | if (isa<UndefValue>(Mask)) return -1; |
| 1382 | if (isa<ConstantAggregateZero>(Mask)) return 0; |
| 1383 | const ConstantVector *MaskCV = cast<ConstantVector>(Mask); |
| 1384 | assert(i < MaskCV->getNumOperands() && "Index out of range"); |
| 1385 | |
| 1386 | if (isa<UndefValue>(MaskCV->getOperand(i))) |
| 1387 | return -1; |
| 1388 | return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue(); |
| 1389 | } |
| 1390 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1391 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1392 | // InsertValueInst Class |
| 1393 | //===----------------------------------------------------------------------===// |
| 1394 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1395 | void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx, |
| 1396 | unsigned NumIdx, const std::string &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1397 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1398 | Op<0>() = Agg; |
| 1399 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1400 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1401 | Indices.insert(Indices.end(), Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1402 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1405 | void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx, |
| 1406 | const std::string &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1407 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1408 | Op<0>() = Agg; |
| 1409 | Op<1>() = Val; |
| 1410 | |
| 1411 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1412 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1416 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1417 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1418 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1419 | Op<0>() = IVI.getOperand(0); |
| 1420 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1423 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1424 | Value *Val, |
| 1425 | unsigned Idx, |
| 1426 | const std::string &Name, |
| 1427 | Instruction *InsertBefore) |
| 1428 | : Instruction(Agg->getType(), InsertValue, |
| 1429 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1430 | 2, InsertBefore) { |
| 1431 | init(Agg, Val, Idx, Name); |
| 1432 | } |
| 1433 | |
| 1434 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1435 | Value *Val, |
| 1436 | unsigned Idx, |
| 1437 | const std::string &Name, |
| 1438 | BasicBlock *InsertAtEnd) |
| 1439 | : Instruction(Agg->getType(), InsertValue, |
| 1440 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1441 | 2, InsertAtEnd) { |
| 1442 | init(Agg, Val, Idx, Name); |
| 1443 | } |
| 1444 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1445 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1446 | // ExtractValueInst Class |
| 1447 | //===----------------------------------------------------------------------===// |
| 1448 | |
Gabor Greif | cbcc495 | 2008-06-06 21:06:32 +0000 | [diff] [blame] | 1449 | void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx, |
Gabor Greif | 1b9921a | 2009-01-11 22:33:22 +0000 | [diff] [blame] | 1450 | const std::string &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1451 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1452 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1453 | Indices.insert(Indices.end(), Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1454 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Gabor Greif | cbcc495 | 2008-06-06 21:06:32 +0000 | [diff] [blame] | 1457 | void ExtractValueInst::init(unsigned Idx, const std::string &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1458 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1459 | |
| 1460 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1461 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1465 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1466 | Indices(EVI.Indices) { |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1469 | // getIndexedType - Returns the type of the element that would be extracted |
| 1470 | // with an extractvalue instruction with the specified parameters. |
| 1471 | // |
| 1472 | // A null type is returned if the indices are invalid for the specified |
| 1473 | // pointer type. |
| 1474 | // |
| 1475 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1476 | const unsigned *Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1477 | unsigned NumIdx) { |
| 1478 | unsigned CurIdx = 0; |
| 1479 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1480 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1481 | if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0; |
| 1482 | unsigned Index = Idxs[CurIdx]; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1483 | if (!CT->indexValid(Index)) return 0; |
| 1484 | Agg = CT->getTypeAtIndex(Index); |
| 1485 | |
| 1486 | // If the new type forwards to another type, then it is in the middle |
| 1487 | // of being refined to another type (and hence, may have dropped all |
| 1488 | // references to what it was using before). So, use the new forwarded |
| 1489 | // type. |
| 1490 | if (const Type *Ty = Agg->getForwardedType()) |
| 1491 | Agg = Ty; |
| 1492 | } |
| 1493 | return CurIdx == NumIdx ? Agg : 0; |
| 1494 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1495 | |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1496 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | d87e813 | 2008-06-20 00:47:44 +0000 | [diff] [blame] | 1497 | unsigned Idx) { |
| 1498 | return getIndexedType(Agg, &Idx, 1); |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1501 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1502 | // BinaryOperator Class |
| 1503 | //===----------------------------------------------------------------------===// |
| 1504 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1505 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1506 | const Type *Ty, const std::string &Name, |
| 1507 | Instruction *InsertBefore) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1508 | : Instruction(Ty, iType, |
| 1509 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1510 | OperandTraits<BinaryOperator>::operands(this), |
| 1511 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1512 | Op<0>() = S1; |
| 1513 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1514 | init(iType); |
| 1515 | setName(Name); |
| 1516 | } |
| 1517 | |
| 1518 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
| 1519 | const Type *Ty, const std::string &Name, |
| 1520 | BasicBlock *InsertAtEnd) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1521 | : Instruction(Ty, iType, |
| 1522 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1523 | OperandTraits<BinaryOperator>::operands(this), |
| 1524 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1525 | Op<0>() = S1; |
| 1526 | Op<1>() = S2; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1527 | init(iType); |
| 1528 | setName(Name); |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1533 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1534 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1535 | assert(LHS->getType() == RHS->getType() && |
| 1536 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1537 | #ifndef NDEBUG |
| 1538 | switch (iType) { |
| 1539 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1540 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1541 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1542 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1543 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1544 | isa<VectorType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1545 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1546 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1547 | case UDiv: |
| 1548 | case SDiv: |
| 1549 | assert(getType() == LHS->getType() && |
| 1550 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1551 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1552 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1553 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1554 | break; |
| 1555 | case FDiv: |
| 1556 | assert(getType() == LHS->getType() && |
| 1557 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1558 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1559 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1560 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1561 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1562 | case URem: |
| 1563 | case SRem: |
| 1564 | assert(getType() == LHS->getType() && |
| 1565 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1566 | assert((getType()->isInteger() || (isa<VectorType>(getType()) && |
| 1567 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1568 | "Incorrect operand type (not integer) for S/UREM"); |
| 1569 | break; |
| 1570 | case FRem: |
| 1571 | assert(getType() == LHS->getType() && |
| 1572 | "Arithmetic operation should return same type as operands!"); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1573 | assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) && |
| 1574 | cast<VectorType>(getType())->getElementType()->isFloatingPoint())) |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1575 | && "Incorrect operand type (not floating point) for FREM"); |
| 1576 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1577 | case Shl: |
| 1578 | case LShr: |
| 1579 | case AShr: |
| 1580 | assert(getType() == LHS->getType() && |
| 1581 | "Shift operation should return same type as operands!"); |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1582 | assert((getType()->isInteger() || |
| 1583 | (isa<VectorType>(getType()) && |
| 1584 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
| 1585 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1586 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1587 | case And: case Or: |
| 1588 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1589 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1590 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1591 | assert((getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1592 | (isa<VectorType>(getType()) && |
| 1593 | cast<VectorType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1594 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1595 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1596 | default: |
| 1597 | break; |
| 1598 | } |
| 1599 | #endif |
| 1600 | } |
| 1601 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1602 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1603 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1604 | Instruction *InsertBefore) { |
| 1605 | assert(S1->getType() == S2->getType() && |
| 1606 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1607 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1610 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1611 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1612 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1613 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1614 | InsertAtEnd->getInstList().push_back(Res); |
| 1615 | return Res; |
| 1616 | } |
| 1617 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1618 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1619 | Instruction *InsertBefore) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1620 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1621 | return new BinaryOperator(Instruction::Sub, |
| 1622 | zero, Op, |
| 1623 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1626 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1627 | BasicBlock *InsertAtEnd) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1628 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1629 | return new BinaryOperator(Instruction::Sub, |
| 1630 | zero, Op, |
| 1631 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1634 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1635 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1636 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1637 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1638 | C = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1639 | C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1640 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1641 | C = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1645 | Op->getType(), Name, InsertBefore); |
| 1646 | } |
| 1647 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1648 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1649 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1650 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1651 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1652 | // Create a vector of all ones values. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1653 | Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1654 | AllOnes = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1655 | ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1656 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1657 | AllOnes = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1661 | Op->getType(), Name, InsertAtEnd); |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | // isConstantAllOnes - Helper function for several functions below |
| 1666 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1667 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1668 | return CI->isAllOnesValue(); |
| 1669 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1670 | return CV->isAllOnesValue(); |
| 1671 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | bool BinaryOperator::isNeg(const Value *V) { |
| 1675 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1676 | if (Bop->getOpcode() == Instruction::Sub) |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1677 | return Bop->getOperand(0) == |
| 1678 | ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1679 | return false; |
| 1680 | } |
| 1681 | |
| 1682 | bool BinaryOperator::isNot(const Value *V) { |
| 1683 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1684 | return (Bop->getOpcode() == Instruction::Xor && |
| 1685 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1686 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1690 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1691 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1692 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1695 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1696 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1699 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1700 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1701 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1702 | Value *Op0 = BO->getOperand(0); |
| 1703 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1704 | if (isConstantAllOnes(Op0)) return Op1; |
| 1705 | |
| 1706 | assert(isConstantAllOnes(Op1)); |
| 1707 | return Op0; |
| 1708 | } |
| 1709 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1710 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1711 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | // swapOperands - Exchange the two operands to this instruction. This |
| 1716 | // instruction is safe to use on any binary instruction and does not |
| 1717 | // modify the semantics of the instruction. If the instruction is |
| 1718 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1719 | // |
| 1720 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1721 | if (!isCommutative()) |
| 1722 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 1723 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1724 | return false; |
| 1725 | } |
| 1726 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1727 | //===----------------------------------------------------------------------===// |
| 1728 | // CastInst Class |
| 1729 | //===----------------------------------------------------------------------===// |
| 1730 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1731 | // Just determine if this cast only deals with integral->integral conversion. |
| 1732 | bool CastInst::isIntegerCast() const { |
| 1733 | switch (getOpcode()) { |
| 1734 | default: return false; |
| 1735 | case Instruction::ZExt: |
| 1736 | case Instruction::SExt: |
| 1737 | case Instruction::Trunc: |
| 1738 | return true; |
| 1739 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1740 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1741 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1744 | bool CastInst::isLosslessCast() const { |
| 1745 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1746 | if (getOpcode() != Instruction::BitCast) |
| 1747 | return false; |
| 1748 | |
| 1749 | // Identity cast is always lossless |
| 1750 | const Type* SrcTy = getOperand(0)->getType(); |
| 1751 | const Type* DstTy = getType(); |
| 1752 | if (SrcTy == DstTy) |
| 1753 | return true; |
| 1754 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1755 | // Pointer to pointer is always lossless. |
| 1756 | if (isa<PointerType>(SrcTy)) |
| 1757 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1758 | return false; // Other types have no identity values |
| 1759 | } |
| 1760 | |
| 1761 | /// This function determines if the CastInst does not require any bits to be |
| 1762 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1763 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1764 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 1765 | /// # bitcast i32* %x to i8* |
| 1766 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 1767 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1768 | /// @brief Determine if a cast is a no-op. |
| 1769 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1770 | switch (getOpcode()) { |
| 1771 | default: |
| 1772 | assert(!"Invalid CastOp"); |
| 1773 | case Instruction::Trunc: |
| 1774 | case Instruction::ZExt: |
| 1775 | case Instruction::SExt: |
| 1776 | case Instruction::FPTrunc: |
| 1777 | case Instruction::FPExt: |
| 1778 | case Instruction::UIToFP: |
| 1779 | case Instruction::SIToFP: |
| 1780 | case Instruction::FPToUI: |
| 1781 | case Instruction::FPToSI: |
| 1782 | return false; // These always modify bits |
| 1783 | case Instruction::BitCast: |
| 1784 | return true; // BitCast never modifies bits. |
| 1785 | case Instruction::PtrToInt: |
| 1786 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1787 | getType()->getPrimitiveSizeInBits(); |
| 1788 | case Instruction::IntToPtr: |
| 1789 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1790 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /// This function determines if a pair of casts can be eliminated and what |
| 1795 | /// opcode should be used in the elimination. This assumes that there are two |
| 1796 | /// instructions like this: |
| 1797 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1798 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1799 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1800 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1801 | /// If no such cast is permited, the function returns 0. |
| 1802 | unsigned CastInst::isEliminableCastPair( |
| 1803 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1804 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1805 | { |
| 1806 | // Define the 144 possibilities for these two cast instructions. The values |
| 1807 | // in this matrix determine what to do in a given situation and select the |
| 1808 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1809 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1810 | // the following cast properties: |
| 1811 | // |
| 1812 | // Size Compare Source Destination |
| 1813 | // Operator Src ? Size Type Sign Type Sign |
| 1814 | // -------- ------------ ------------------- --------------------- |
| 1815 | // TRUNC > Integer Any Integral Any |
| 1816 | // ZEXT < Integral Unsigned Integer Any |
| 1817 | // SEXT < Integral Signed Integer Any |
| 1818 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1819 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1820 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1821 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1822 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1823 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1824 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1825 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1826 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1827 | // |
| 1828 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
| 1829 | // For example, we could merge "fptoui double to uint" + "zext uint to ulong", |
| 1830 | // into "fptoui double to ulong", but this loses information about the range |
| 1831 | // of the produced value (we no longer know the top-part is all zeros). |
| 1832 | // Further this conversion is often much more expensive for typical hardware, |
| 1833 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1834 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1835 | const unsigned numCastOps = |
| 1836 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1837 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1838 | // T F F U S F F P I B -+ |
| 1839 | // R Z S P P I I T P 2 N T | |
| 1840 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1841 | // N X X U S F F N X N 2 V | |
| 1842 | // C T T I I P P C T T P T -+ |
| 1843 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1844 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1845 | { 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] | 1846 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1847 | { 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] | 1848 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1849 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1850 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1851 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1852 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1853 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1854 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1855 | }; |
| 1856 | |
| 1857 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1858 | [secondOp-Instruction::CastOpsBegin]; |
| 1859 | switch (ElimCase) { |
| 1860 | case 0: |
| 1861 | // categorically disallowed |
| 1862 | return 0; |
| 1863 | case 1: |
| 1864 | // allowed, use first cast's opcode |
| 1865 | return firstOp; |
| 1866 | case 2: |
| 1867 | // allowed, use second cast's opcode |
| 1868 | return secondOp; |
| 1869 | case 3: |
| 1870 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1871 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1872 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1873 | return firstOp; |
| 1874 | return 0; |
| 1875 | case 4: |
| 1876 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1877 | // is floating point |
| 1878 | if (DstTy->isFloatingPoint()) |
| 1879 | return firstOp; |
| 1880 | return 0; |
| 1881 | case 5: |
| 1882 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1883 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1884 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1885 | return secondOp; |
| 1886 | return 0; |
| 1887 | case 6: |
| 1888 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1889 | // is a floating point |
| 1890 | if (SrcTy->isFloatingPoint()) |
| 1891 | return secondOp; |
| 1892 | return 0; |
| 1893 | case 7: { |
| 1894 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1895 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1896 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1897 | if (MidSize >= PtrSize) |
| 1898 | return Instruction::BitCast; |
| 1899 | return 0; |
| 1900 | } |
| 1901 | case 8: { |
| 1902 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1903 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1904 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1905 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1906 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1907 | if (SrcSize == DstSize) |
| 1908 | return Instruction::BitCast; |
| 1909 | else if (SrcSize < DstSize) |
| 1910 | return firstOp; |
| 1911 | return secondOp; |
| 1912 | } |
| 1913 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1914 | return Instruction::ZExt; |
| 1915 | case 10: |
| 1916 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1917 | // the same as the original, in which case its just a bitcast |
| 1918 | if (SrcTy == DstTy) |
| 1919 | return Instruction::BitCast; |
| 1920 | return 0; // If the types are not the same we can't eliminate it. |
| 1921 | case 11: |
| 1922 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1923 | // is a pointer to pointer cast. |
| 1924 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1925 | return secondOp; |
| 1926 | return 0; |
| 1927 | case 12: |
| 1928 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1929 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1930 | return firstOp; |
| 1931 | return 0; |
| 1932 | case 13: { |
| 1933 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1934 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1935 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1936 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1937 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1938 | return Instruction::BitCast; |
| 1939 | return 0; |
| 1940 | } |
| 1941 | case 99: |
| 1942 | // cast combination can't happen (error in input). This is for all cases |
| 1943 | // where the MidTy is not the same for the two cast instructions. |
| 1944 | assert(!"Invalid Cast Combination"); |
| 1945 | return 0; |
| 1946 | default: |
| 1947 | assert(!"Error in CastResults table!!!"); |
| 1948 | return 0; |
| 1949 | } |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1953 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1954 | const std::string &Name, Instruction *InsertBefore) { |
| 1955 | // Construct and return the appropriate CastInst subclass |
| 1956 | switch (op) { |
| 1957 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1958 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1959 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1960 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1961 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1962 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1963 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1964 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1965 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1966 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1967 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1968 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1969 | default: |
| 1970 | assert(!"Invalid opcode provided"); |
| 1971 | } |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1975 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1976 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1977 | // Construct and return the appropriate CastInst subclass |
| 1978 | switch (op) { |
| 1979 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1980 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1981 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1982 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1983 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1984 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1985 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1986 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1987 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1988 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1989 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1990 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1991 | default: |
| 1992 | assert(!"Invalid opcode provided"); |
| 1993 | } |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1997 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1998 | const std::string &Name, |
| 1999 | Instruction *InsertBefore) { |
| 2000 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2001 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2002 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2005 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2006 | const std::string &Name, |
| 2007 | BasicBlock *InsertAtEnd) { |
| 2008 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2009 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2010 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2013 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2014 | const std::string &Name, |
| 2015 | Instruction *InsertBefore) { |
| 2016 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2017 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2018 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2021 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2022 | const std::string &Name, |
| 2023 | BasicBlock *InsertAtEnd) { |
| 2024 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2025 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2026 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2029 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2030 | const std::string &Name, |
| 2031 | Instruction *InsertBefore) { |
| 2032 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2033 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2034 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2037 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2038 | const std::string &Name, |
| 2039 | BasicBlock *InsertAtEnd) { |
| 2040 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2041 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2042 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2045 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2046 | const std::string &Name, |
| 2047 | BasicBlock *InsertAtEnd) { |
| 2048 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2049 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2050 | "Invalid cast"); |
| 2051 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2052 | if (Ty->isInteger()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2053 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2054 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2058 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2059 | const std::string &Name, |
| 2060 | Instruction *InsertBefore) { |
| 2061 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2062 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2063 | "Invalid cast"); |
| 2064 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2065 | if (Ty->isInteger()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2066 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2067 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2070 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2071 | bool isSigned, const std::string &Name, |
| 2072 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2073 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2074 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 2075 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 2076 | Instruction::CastOps opcode = |
| 2077 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2078 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2079 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2080 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2083 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2084 | bool isSigned, const std::string &Name, |
| 2085 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2086 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2087 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 2088 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 2089 | Instruction::CastOps opcode = |
| 2090 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2091 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2092 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2093 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2096 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2097 | const std::string &Name, |
| 2098 | Instruction *InsertBefore) { |
| 2099 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 2100 | "Invalid cast"); |
| 2101 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 2102 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 2103 | Instruction::CastOps opcode = |
| 2104 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2105 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2106 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2109 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2110 | const std::string &Name, |
| 2111 | BasicBlock *InsertAtEnd) { |
| 2112 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 2113 | "Invalid cast"); |
| 2114 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 2115 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 2116 | Instruction::CastOps opcode = |
| 2117 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2118 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2119 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2122 | // Check whether it is valid to call getCastOpcode for these types. |
| 2123 | // This routine must be kept in sync with getCastOpcode. |
| 2124 | bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) { |
| 2125 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2126 | return false; |
| 2127 | |
| 2128 | if (SrcTy == DestTy) |
| 2129 | return true; |
| 2130 | |
| 2131 | // Get the bit sizes, we'll need these |
| 2132 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
| 2133 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
| 2134 | |
| 2135 | // Run through the possibilities ... |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2136 | if (DestTy->isInteger()) { // Casting to integral |
| 2137 | if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2138 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2139 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2140 | return true; |
| 2141 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2142 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2143 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2144 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2145 | return isa<PointerType>(SrcTy); |
| 2146 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2147 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
| 2148 | if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2149 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2150 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2151 | return true; |
| 2152 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2153 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2154 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2155 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2156 | return false; |
| 2157 | } |
| 2158 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2159 | // Casting to vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2160 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2161 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2162 | return DestPTy->getBitWidth() == SrcPTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2163 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2164 | return DestPTy->getBitWidth() == SrcBits; |
| 2165 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2166 | } else if (isa<PointerType>(DestTy)) { // Casting to pointer |
| 2167 | if (isa<PointerType>(SrcTy)) { // Casting from pointer |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2168 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2169 | } else if (SrcTy->isInteger()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2170 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2171 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2172 | return false; |
| 2173 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2174 | } else { // Casting to something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2175 | return false; |
| 2176 | } |
| 2177 | } |
| 2178 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2179 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2180 | // 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] | 2181 | // logic in the castIsValid function below. This axiom should hold: |
| 2182 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2183 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2184 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2185 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2186 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2187 | CastInst::getCastOpcode( |
| 2188 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2189 | // Get the bit sizes, we'll need these |
| 2190 | const Type *SrcTy = Src->getType(); |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2191 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
| 2192 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2193 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2194 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2195 | "Only first class types are castable!"); |
| 2196 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2197 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2198 | if (DestTy->isInteger()) { // Casting to integral |
| 2199 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2200 | if (DestBits < SrcBits) |
| 2201 | return Trunc; // int -> smaller int |
| 2202 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2203 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2204 | return SExt; // signed -> SEXT |
| 2205 | else |
| 2206 | return ZExt; // unsigned -> ZEXT |
| 2207 | } else { |
| 2208 | return BitCast; // Same size, No-op cast |
| 2209 | } |
| 2210 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2211 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2212 | return FPToSI; // FP -> sint |
| 2213 | else |
| 2214 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2215 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2216 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2217 | "Casting vector to integer of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2218 | PTy = NULL; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2219 | return BitCast; // Same size, no-op cast |
| 2220 | } else { |
| 2221 | assert(isa<PointerType>(SrcTy) && |
| 2222 | "Casting from a value that is not first-class type"); |
| 2223 | return PtrToInt; // ptr -> int |
| 2224 | } |
| 2225 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2226 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2227 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2228 | return SIToFP; // sint -> FP |
| 2229 | else |
| 2230 | return UIToFP; // uint -> FP |
| 2231 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 2232 | if (DestBits < SrcBits) { |
| 2233 | return FPTrunc; // FP -> smaller FP |
| 2234 | } else if (DestBits > SrcBits) { |
| 2235 | return FPExt; // FP -> larger FP |
| 2236 | } else { |
| 2237 | return BitCast; // same size, no-op cast |
| 2238 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2239 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2240 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2241 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2242 | PTy = NULL; |
| 2243 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2244 | } else { |
| 2245 | assert(0 && "Casting pointer or non-first class to float"); |
| 2246 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2247 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 2248 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2249 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2250 | "Casting vector to vector of different widths"); |
Devang Patel | cb181bb | 2008-11-21 20:00:59 +0000 | [diff] [blame] | 2251 | SrcPTy = NULL; |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2252 | return BitCast; // vector -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2253 | } else if (DestPTy->getBitWidth() == SrcBits) { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2254 | return BitCast; // float/int -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2255 | } else { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2256 | assert(!"Illegal cast to vector (wrong type or size)"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2257 | } |
| 2258 | } else if (isa<PointerType>(DestTy)) { |
| 2259 | if (isa<PointerType>(SrcTy)) { |
| 2260 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2261 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2262 | return IntToPtr; // int -> ptr |
| 2263 | } else { |
| 2264 | assert(!"Casting pointer to other than pointer or int"); |
| 2265 | } |
| 2266 | } else { |
| 2267 | assert(!"Casting to type that is not first-class"); |
| 2268 | } |
| 2269 | |
| 2270 | // If we fall through to here we probably hit an assertion cast above |
| 2271 | // and assertions are not turned on. Anything we return is an error, so |
| 2272 | // BitCast is as good a choice as any. |
| 2273 | return BitCast; |
| 2274 | } |
| 2275 | |
| 2276 | //===----------------------------------------------------------------------===// |
| 2277 | // CastInst SubClass Constructors |
| 2278 | //===----------------------------------------------------------------------===// |
| 2279 | |
| 2280 | /// Check that the construction parameters for a CastInst are correct. This |
| 2281 | /// could be broken out into the separate constructors but it is useful to have |
| 2282 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2283 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2284 | bool |
| 2285 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2286 | |
| 2287 | // Check for type sanity on the arguments |
| 2288 | const Type *SrcTy = S->getType(); |
| 2289 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 2290 | return false; |
| 2291 | |
| 2292 | // Get the size of the types in bits, we'll need this later |
| 2293 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 2294 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 2295 | |
| 2296 | // Switch on the opcode provided |
| 2297 | switch (op) { |
| 2298 | default: return false; // This is an input error |
| 2299 | case Instruction::Trunc: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2300 | return SrcTy->isIntOrIntVector() && |
| 2301 | DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2302 | case Instruction::ZExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2303 | return SrcTy->isIntOrIntVector() && |
| 2304 | DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2305 | case Instruction::SExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2306 | return SrcTy->isIntOrIntVector() && |
| 2307 | DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2308 | case Instruction::FPTrunc: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2309 | return SrcTy->isFPOrFPVector() && |
| 2310 | DstTy->isFPOrFPVector() && |
| 2311 | SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2312 | case Instruction::FPExt: |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2313 | return SrcTy->isFPOrFPVector() && |
| 2314 | DstTy->isFPOrFPVector() && |
| 2315 | SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2316 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2317 | case Instruction::SIToFP: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2318 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2319 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2320 | return SVTy->getElementType()->isIntOrIntVector() && |
| 2321 | DVTy->getElementType()->isFPOrFPVector() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2322 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2323 | } |
| 2324 | } |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2325 | return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2326 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2327 | case Instruction::FPToSI: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2328 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2329 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2330 | return SVTy->getElementType()->isFPOrFPVector() && |
| 2331 | DVTy->getElementType()->isIntOrIntVector() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2332 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2333 | } |
| 2334 | } |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2335 | return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2336 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2337 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2338 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 2339 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2340 | case Instruction::BitCast: |
| 2341 | // BitCast implies a no-op cast of type only. No bits change. |
| 2342 | // However, you can't cast pointers to anything but pointers. |
| 2343 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 2344 | return false; |
| 2345 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2346 | // 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] | 2347 | // these cases, the cast is okay if the source and destination bit widths |
| 2348 | // are identical. |
| 2349 | return SrcBitSize == DstBitSize; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | TruncInst::TruncInst( |
| 2354 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2355 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2356 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | TruncInst::TruncInst( |
| 2360 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2361 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2362 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | ZExtInst::ZExtInst( |
| 2366 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2367 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2368 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | ZExtInst::ZExtInst( |
| 2372 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2373 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2374 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2375 | } |
| 2376 | SExtInst::SExtInst( |
| 2377 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2378 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2379 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2382 | SExtInst::SExtInst( |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2383 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2384 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2385 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | FPTruncInst::FPTruncInst( |
| 2389 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2390 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2391 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | FPTruncInst::FPTruncInst( |
| 2395 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2396 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2397 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | FPExtInst::FPExtInst( |
| 2401 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2402 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2403 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | FPExtInst::FPExtInst( |
| 2407 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2408 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2409 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | UIToFPInst::UIToFPInst( |
| 2413 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2414 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2415 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | UIToFPInst::UIToFPInst( |
| 2419 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2420 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2421 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | SIToFPInst::SIToFPInst( |
| 2425 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2426 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2427 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | SIToFPInst::SIToFPInst( |
| 2431 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2432 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2433 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | FPToUIInst::FPToUIInst( |
| 2437 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2438 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2439 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | FPToUIInst::FPToUIInst( |
| 2443 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2444 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2445 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | FPToSIInst::FPToSIInst( |
| 2449 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2450 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2451 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | FPToSIInst::FPToSIInst( |
| 2455 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2456 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2457 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | PtrToIntInst::PtrToIntInst( |
| 2461 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2462 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2463 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | PtrToIntInst::PtrToIntInst( |
| 2467 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2468 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2469 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | IntToPtrInst::IntToPtrInst( |
| 2473 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2474 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2475 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | IntToPtrInst::IntToPtrInst( |
| 2479 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2480 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2481 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | BitCastInst::BitCastInst( |
| 2485 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 2486 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2487 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | BitCastInst::BitCastInst( |
| 2491 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 2492 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2493 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2494 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2495 | |
| 2496 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2497 | // CmpInst Classes |
| 2498 | //===----------------------------------------------------------------------===// |
| 2499 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2500 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
| 2501 | Value *LHS, Value *RHS, const std::string &Name, |
| 2502 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2503 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2504 | OperandTraits<CmpInst>::op_begin(this), |
| 2505 | OperandTraits<CmpInst>::operands(this), |
| 2506 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2507 | Op<0>() = LHS; |
| 2508 | Op<1>() = RHS; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2509 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2510 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2511 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2512 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2513 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
| 2514 | Value *LHS, Value *RHS, const std::string &Name, |
| 2515 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2516 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2517 | OperandTraits<CmpInst>::op_begin(this), |
| 2518 | OperandTraits<CmpInst>::operands(this), |
| 2519 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2520 | Op<0>() = LHS; |
| 2521 | Op<1>() = RHS; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2522 | SubclassData = predicate; |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2523 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2527 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2528 | const std::string &Name, Instruction *InsertBefore) { |
| 2529 | if (Op == Instruction::ICmp) { |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2530 | return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2531 | InsertBefore); |
| 2532 | } |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2533 | if (Op == Instruction::FCmp) { |
| 2534 | return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2535 | InsertBefore); |
| 2536 | } |
| 2537 | if (Op == Instruction::VICmp) { |
| 2538 | return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2539 | InsertBefore); |
| 2540 | } |
| 2541 | return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2542 | InsertBefore); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2546 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2547 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 2548 | if (Op == Instruction::ICmp) { |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2549 | return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2550 | InsertAtEnd); |
| 2551 | } |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2552 | if (Op == Instruction::FCmp) { |
| 2553 | return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2554 | InsertAtEnd); |
| 2555 | } |
| 2556 | if (Op == Instruction::VICmp) { |
| 2557 | return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2558 | InsertAtEnd); |
| 2559 | } |
| 2560 | return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, |
| 2561 | InsertAtEnd); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | void CmpInst::swapOperands() { |
| 2565 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2566 | IC->swapOperands(); |
| 2567 | else |
| 2568 | cast<FCmpInst>(this)->swapOperands(); |
| 2569 | } |
| 2570 | |
| 2571 | bool CmpInst::isCommutative() { |
| 2572 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2573 | return IC->isCommutative(); |
| 2574 | return cast<FCmpInst>(this)->isCommutative(); |
| 2575 | } |
| 2576 | |
| 2577 | bool CmpInst::isEquality() { |
| 2578 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2579 | return IC->isEquality(); |
| 2580 | return cast<FCmpInst>(this)->isEquality(); |
| 2581 | } |
| 2582 | |
| 2583 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2584 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2585 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2586 | default: assert(!"Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2587 | case ICMP_EQ: return ICMP_NE; |
| 2588 | case ICMP_NE: return ICMP_EQ; |
| 2589 | case ICMP_UGT: return ICMP_ULE; |
| 2590 | case ICMP_ULT: return ICMP_UGE; |
| 2591 | case ICMP_UGE: return ICMP_ULT; |
| 2592 | case ICMP_ULE: return ICMP_UGT; |
| 2593 | case ICMP_SGT: return ICMP_SLE; |
| 2594 | case ICMP_SLT: return ICMP_SGE; |
| 2595 | case ICMP_SGE: return ICMP_SLT; |
| 2596 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2597 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2598 | case FCMP_OEQ: return FCMP_UNE; |
| 2599 | case FCMP_ONE: return FCMP_UEQ; |
| 2600 | case FCMP_OGT: return FCMP_ULE; |
| 2601 | case FCMP_OLT: return FCMP_UGE; |
| 2602 | case FCMP_OGE: return FCMP_ULT; |
| 2603 | case FCMP_OLE: return FCMP_UGT; |
| 2604 | case FCMP_UEQ: return FCMP_ONE; |
| 2605 | case FCMP_UNE: return FCMP_OEQ; |
| 2606 | case FCMP_UGT: return FCMP_OLE; |
| 2607 | case FCMP_ULT: return FCMP_OGE; |
| 2608 | case FCMP_UGE: return FCMP_OLT; |
| 2609 | case FCMP_ULE: return FCMP_OGT; |
| 2610 | case FCMP_ORD: return FCMP_UNO; |
| 2611 | case FCMP_UNO: return FCMP_ORD; |
| 2612 | case FCMP_TRUE: return FCMP_FALSE; |
| 2613 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2614 | } |
| 2615 | } |
| 2616 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2617 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2618 | switch (pred) { |
| 2619 | default: assert(! "Unknown icmp predicate!"); |
| 2620 | case ICMP_EQ: case ICMP_NE: |
| 2621 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2622 | return pred; |
| 2623 | case ICMP_UGT: return ICMP_SGT; |
| 2624 | case ICMP_ULT: return ICMP_SLT; |
| 2625 | case ICMP_UGE: return ICMP_SGE; |
| 2626 | case ICMP_ULE: return ICMP_SLE; |
| 2627 | } |
| 2628 | } |
| 2629 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2630 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 2631 | switch (pred) { |
| 2632 | default: assert(! "Unknown icmp predicate!"); |
| 2633 | case ICMP_EQ: case ICMP_NE: |
| 2634 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 2635 | return pred; |
| 2636 | case ICMP_SGT: return ICMP_UGT; |
| 2637 | case ICMP_SLT: return ICMP_ULT; |
| 2638 | case ICMP_SGE: return ICMP_UGE; |
| 2639 | case ICMP_SLE: return ICMP_ULE; |
| 2640 | } |
| 2641 | } |
| 2642 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2643 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2644 | switch (pred) { |
| 2645 | default: assert(! "Unknown icmp predicate!"); |
| 2646 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2647 | return true; |
| 2648 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2649 | case ICMP_UGE: case ICMP_ULE: |
| 2650 | return false; |
| 2651 | } |
| 2652 | } |
| 2653 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2654 | /// Initialize a set of values that all satisfy the condition with C. |
| 2655 | /// |
| 2656 | ConstantRange |
| 2657 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2658 | APInt Lower(C); |
| 2659 | APInt Upper(C); |
| 2660 | uint32_t BitWidth = C.getBitWidth(); |
| 2661 | switch (pred) { |
| 2662 | default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!"); |
| 2663 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2664 | case ICmpInst::ICMP_NE: Lower++; break; |
| 2665 | case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break; |
| 2666 | case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break; |
| 2667 | case ICmpInst::ICMP_UGT: |
| 2668 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2669 | break; |
| 2670 | case ICmpInst::ICMP_SGT: |
| 2671 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2672 | break; |
| 2673 | case ICmpInst::ICMP_ULE: |
| 2674 | Lower = APInt::getMinValue(BitWidth); Upper++; |
| 2675 | break; |
| 2676 | case ICmpInst::ICMP_SLE: |
| 2677 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
| 2678 | break; |
| 2679 | case ICmpInst::ICMP_UGE: |
| 2680 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
| 2681 | break; |
| 2682 | case ICmpInst::ICMP_SGE: |
| 2683 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
| 2684 | break; |
| 2685 | } |
| 2686 | return ConstantRange(Lower, Upper); |
| 2687 | } |
| 2688 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2689 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2690 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2691 | default: assert(!"Unknown cmp predicate!"); |
| 2692 | case ICMP_EQ: case ICMP_NE: |
| 2693 | return pred; |
| 2694 | case ICMP_SGT: return ICMP_SLT; |
| 2695 | case ICMP_SLT: return ICMP_SGT; |
| 2696 | case ICMP_SGE: return ICMP_SLE; |
| 2697 | case ICMP_SLE: return ICMP_SGE; |
| 2698 | case ICMP_UGT: return ICMP_ULT; |
| 2699 | case ICMP_ULT: return ICMP_UGT; |
| 2700 | case ICMP_UGE: return ICMP_ULE; |
| 2701 | case ICMP_ULE: return ICMP_UGE; |
| 2702 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2703 | case FCMP_FALSE: case FCMP_TRUE: |
| 2704 | case FCMP_OEQ: case FCMP_ONE: |
| 2705 | case FCMP_UEQ: case FCMP_UNE: |
| 2706 | case FCMP_ORD: case FCMP_UNO: |
| 2707 | return pred; |
| 2708 | case FCMP_OGT: return FCMP_OLT; |
| 2709 | case FCMP_OLT: return FCMP_OGT; |
| 2710 | case FCMP_OGE: return FCMP_OLE; |
| 2711 | case FCMP_OLE: return FCMP_OGE; |
| 2712 | case FCMP_UGT: return FCMP_ULT; |
| 2713 | case FCMP_ULT: return FCMP_UGT; |
| 2714 | case FCMP_UGE: return FCMP_ULE; |
| 2715 | case FCMP_ULE: return FCMP_UGE; |
| 2716 | } |
| 2717 | } |
| 2718 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2719 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2720 | switch (predicate) { |
| 2721 | default: return false; |
| 2722 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2723 | case ICmpInst::ICMP_UGE: return true; |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2728 | switch (predicate) { |
| 2729 | default: return false; |
| 2730 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2731 | case ICmpInst::ICMP_SGE: return true; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2736 | switch (predicate) { |
| 2737 | default: return false; |
| 2738 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2739 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2740 | case FCmpInst::FCMP_ORD: return true; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2745 | switch (predicate) { |
| 2746 | default: return false; |
| 2747 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2748 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2749 | case FCmpInst::FCMP_UNO: return true; |
| 2750 | } |
| 2751 | } |
| 2752 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2753 | //===----------------------------------------------------------------------===// |
| 2754 | // SwitchInst Implementation |
| 2755 | //===----------------------------------------------------------------------===// |
| 2756 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2757 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2758 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2759 | ReservedSpace = 2+NumCases*2; |
| 2760 | NumOperands = 2; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2761 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2762 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2763 | OperandList[0] = Value; |
| 2764 | OperandList[1] = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2767 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2768 | /// switch on and a default destination. The number of additional cases can |
| 2769 | /// be specified here to make memory allocation more efficient. This |
| 2770 | /// constructor can also autoinsert before another instruction. |
| 2771 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2772 | Instruction *InsertBefore) |
| 2773 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) { |
| 2774 | init(Value, Default, NumCases); |
| 2775 | } |
| 2776 | |
| 2777 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2778 | /// switch on and a default destination. The number of additional cases can |
| 2779 | /// be specified here to make memory allocation more efficient. This |
| 2780 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 2781 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2782 | BasicBlock *InsertAtEnd) |
| 2783 | : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) { |
| 2784 | init(Value, Default, NumCases); |
| 2785 | } |
| 2786 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2787 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2788 | : TerminatorInst(Type::VoidTy, Instruction::Switch, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2789 | allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2790 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2791 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2792 | OL[i] = InOL[i]; |
| 2793 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 2797 | SwitchInst::~SwitchInst() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2798 | dropHungoffUses(OperandList); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2802 | /// addCase - Add an entry to the switch instruction... |
| 2803 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2804 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2805 | unsigned OpNo = NumOperands; |
| 2806 | if (OpNo+2 > ReservedSpace) |
| 2807 | resizeOperands(0); // Get more space! |
| 2808 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2809 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2810 | NumOperands = OpNo+2; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2811 | OperandList[OpNo] = OnVal; |
| 2812 | OperandList[OpNo+1] = Dest; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | /// removeCase - This method removes the specified successor from the switch |
| 2816 | /// instruction. Note that this cannot be used to remove the default |
| 2817 | /// destination (successor #0). |
| 2818 | /// |
| 2819 | void SwitchInst::removeCase(unsigned idx) { |
| 2820 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2821 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2822 | |
| 2823 | unsigned NumOps = getNumOperands(); |
| 2824 | Use *OL = OperandList; |
| 2825 | |
| 2826 | // Move everything after this operand down. |
| 2827 | // |
| 2828 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2829 | // client might not expect this to happen. The code as it is thrashes the |
| 2830 | // use/def lists, which is kinda lame. |
| 2831 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2832 | OL[i-2] = OL[i]; |
| 2833 | OL[i-2+1] = OL[i+1]; |
| 2834 | } |
| 2835 | |
| 2836 | // Nuke the last value. |
| 2837 | OL[NumOps-2].set(0); |
| 2838 | OL[NumOps-2+1].set(0); |
| 2839 | NumOperands = NumOps-2; |
| 2840 | } |
| 2841 | |
| 2842 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2843 | /// list according to the following behavior: |
| 2844 | /// 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] | 2845 | /// of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2846 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2847 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2848 | /// |
| 2849 | void SwitchInst::resizeOperands(unsigned NumOps) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2850 | unsigned e = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2851 | if (NumOps == 0) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2852 | NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2853 | } else if (NumOps*2 > NumOperands) { |
| 2854 | // No resize needed. |
| 2855 | if (ReservedSpace >= NumOps) return; |
| 2856 | } else if (NumOps == NumOperands) { |
| 2857 | if (ReservedSpace == NumOps) return; |
| 2858 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2859 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | ReservedSpace = NumOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2863 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2864 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2865 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2866 | NewOps[i] = OldOps[i]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2867 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2868 | OperandList = NewOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2869 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | |
| 2873 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2874 | return getSuccessor(idx); |
| 2875 | } |
| 2876 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2877 | return getNumSuccessors(); |
| 2878 | } |
| 2879 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2880 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2881 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2882 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2883 | // Define these methods here so vtables don't get emitted into every translation |
| 2884 | // unit that uses these classes. |
| 2885 | |
| 2886 | GetElementPtrInst *GetElementPtrInst::clone() const { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2887 | return new(getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | BinaryOperator *BinaryOperator::clone() const { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2891 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Chris Lattner | 0b490b0 | 2007-08-24 20:48:18 +0000 | [diff] [blame] | 2894 | FCmpInst* FCmpInst::clone() const { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2895 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Chris Lattner | 0b490b0 | 2007-08-24 20:48:18 +0000 | [diff] [blame] | 2896 | } |
| 2897 | ICmpInst* ICmpInst::clone() const { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2898 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2901 | VFCmpInst* VFCmpInst::clone() const { |
| 2902 | return new VFCmpInst(getPredicate(), Op<0>(), Op<1>()); |
| 2903 | } |
| 2904 | VICmpInst* VICmpInst::clone() const { |
| 2905 | return new VICmpInst(getPredicate(), Op<0>(), Op<1>()); |
| 2906 | } |
| 2907 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2908 | ExtractValueInst *ExtractValueInst::clone() const { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2909 | return new ExtractValueInst(*this); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2910 | } |
| 2911 | InsertValueInst *InsertValueInst::clone() const { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 2912 | return new InsertValueInst(*this); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2916 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2917 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2918 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2919 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2920 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2921 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2922 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2923 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2924 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2925 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2926 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2927 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2928 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2929 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2930 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2931 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2932 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2933 | CallInst *CallInst::clone() const { |
| 2934 | return new(getNumOperands()) CallInst(*this); |
| 2935 | } |
| 2936 | SelectInst *SelectInst::clone() const { |
| 2937 | return new(getNumOperands()) SelectInst(*this); |
| 2938 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2939 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2940 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2941 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2942 | return new ExtractElementInst(*this); |
| 2943 | } |
| 2944 | InsertElementInst *InsertElementInst::clone() const { |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2945 | return InsertElementInst::Create(*this); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2946 | } |
| 2947 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2948 | return new ShuffleVectorInst(*this); |
| 2949 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2950 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2951 | ReturnInst *ReturnInst::clone() const { |
| 2952 | return new(getNumOperands()) ReturnInst(*this); |
| 2953 | } |
| 2954 | BranchInst *BranchInst::clone() const { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame^] | 2955 | unsigned Ops(getNumOperands()); |
| 2956 | return new(Ops, Ops == 1) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2957 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2958 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2959 | InvokeInst *InvokeInst::clone() const { |
| 2960 | return new(getNumOperands()) InvokeInst(*this); |
| 2961 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2962 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2963 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |