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