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