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