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