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