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