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