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