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