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