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