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