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